Actual combat String Series in C + +--16 binary string to hexadecimal integer (usually color code used)

Source: Internet
Author: User
Tags hex code

It's been a long time since I wrote a blog about string. Because the writing almost the same. But the recent deal with string, so the hormone of the brain, small tadpoles restless.

In the program, assume that the use of color code, is generally hexadecimal, that is, Hex.

But the server returns you a color string. That is, hex string

How do you convert this hex string to hex and use it in your code?

Further, how do you convert a hex string, such as "#ffceed", to RGB?

The first question in Java is this:

 Public Static int Parsecolor(@Size(min=1) String colorstring) {if(Colorstring.charat (0) ==' # ') {//Use a long to avoid rollovers on #ffXXXXXXLongcolor = Long.parselong (colorstring.substring (1), -);if(colorstring.length () = =7) {//Set the alpha valueColor |=0x00000000ff000000;}Else if(Colorstring.length ()! =9) {Throw NewIllegalArgumentException ("Unknown Color");}return(int) color;}Else{Integer color = scolornamemap.Get(Colorstring.tolowercase (Locale.root));if(Color! =NULL) {returnColor;}}Throw NewIllegalArgumentException ("Unknown Color");}

In C + +, however, we can use the flow, which is more concise:

 auto Color_string_iter = Hex_string_color.begin  ()     Hex_string_color.erase  (color_string_iter)     hex_string_color=  "FF"  + hex_string_color;     DWORD color;     std :: StringStream ss;     SS << std :: Hex << hex_string_color;     SS >> std :: Hex >> color;     Btn1->setbkcolor (color)  Btn2->setbkcolor (0xff123456 )   

There is another way to use: Std::strtoul
is primarily the third parameter:
Numerical base (radix) that determines the valid Characters and their interpretation.
If This is 0, the base used are determined by the format of the sequence

#include <cstdlib>#include <iostream> // for std::coutint main(){    char"0xbeef";    unsignedlong hex_value        std016);    std::cout"hex value: "std::endl;    return0;}

Next look at how to turn hex string RGB:

#include <iostream>#include <sstream>intMain () {STD::stringHexcode;STD::cout<<"Please enter the hex code:";STD::Cin>> Hexcode;intR, G, B;if(Hexcode.at (0) ==' # ') {Hexcode = Hexcode.erase (0,1); }//... and extract the RGB values.   STD::Istringstream(Hexcode.substr (0,2)) >>STD:: Hex >> R;STD::Istringstream(Hexcode.substr (2,2)) >>STD:: Hex >> G;STD::Istringstream(Hexcode.substr (4,2)) >>STD:: Hex >> B;//Finally Dump the result.   STD::cout<<STD::d EC <<"Parsing #"<< Hexcode <<"as Hex gives ("<< R <<", "<< g <<", "<< b <<")"<<' \ n ';}

Here is a bit of advice and advice.
We start with learning programming, and the most familiar is print or cout to see the results. But in the actual work is very rarely used.

For example, there is a decimal number 100. Why we passed cout<

Actual combat String Series in C + +--16 binary string to hexadecimal integer (usually color code used)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.