For example I have string that contains hexadecimal numbers:
std::string hexStr = "E110A3" and I want to convert it to char.
So that char hex[3] = {0xE1, 0x10, 0xA3}
How I can accomplish this?
I tried to search but I didn't find answer to this.

For example I have string that contains hexadecimal numbers:
std::string hexStr = "E110A3" and I want to convert it to char.
So that char hex[3] = {0xE1, 0x10, 0xA3}
How I can accomplish this?
I tried to search but I didn't find answer to this.

You need to first extract the two digit pairs from the string. Then you need to write a function that takes a two character string, representing a hexadecimal byte value and turn it into a number from 0 to 255 (does a char with a value over 127 have meaning?)

char Convert (string hexNumber)
// assumes 2 character string with legal hex digits
{
     char aChar;
     char highOrderDig = hexNumber[0];
     char lowOrderDig  = hexNumber[1];
     int lowOrderValue = //;  convert lowOrderDig to number from 0 to 15
     int highOrderValue = //; convert highOrderDig to number from 0 to 15
     aChar = lowOrderValue + 16 * highOrderValue;
     return aChar;
}

That's how I'd do it.

Thanks for quick reply.
I try this as I get home.

What is the best way to convert char to int (as you described low order to int 0-15)?
First thing that comes in my mind is to use if statements but that seems pretty stupid way.
Eg.
if(lowOrder = 'A') number = 10;

Thanks for quick reply.
I try this as I get home.

What is the best way to convert char to int (as you described low order to int 0-15)?
First thing that comes in my mind is to use if statements but that seems pretty stupid way.
Eg.
if(lowOrder = 'A') number = 10;

You would want == rather than = in the line above. The above would work but it would require that you have at least 16 if statement comparisons possibly, which is excessive. Take a look at the ASCII table.

http://www.asciitable.com/

You have three possibilities (assuming legal data). One, the character is 0 through 9. Two, the character is A - F (upper case). Three, the character is a - f (lower case). Anything else is illegal. Regarding the upper versus lower case, you can also either assume the character is upper case, or you can convert to upper case. The cctype library can be useful for this:

http://www.cplusplus.com/reference/clibrary/cctype/

In particular, for your case, the following functions could be useful : toupper, isupper, isdigit, isalpha.

You do not NEED to use any of these functions however. You can instead convert directly using the ASCII code. To do so, figure out what the ascii offset would be. In the case of 'A' through 'F', the ascii offset would be 55. If you do use the cctype functions, you would not necessarily need to hard-code in these ASCII values.

int GetDigitValue (char digit)
{
     int asciiOffset, digitValue;
     if (digit >= 48 && digit <= 57)
     {
           // code for '0' through '9'
     }
     else if (digit >= 65 && digit <= 70)
     {
          // digit is 'A' through 'F'
          asciiOffset = 55;
          digitValue = digit - asciiOffset;
          return digitValue;
     }
     else if (digit >= ? && digit <= ?)
     {
           // code for 'a' through 'f'
     }
     else
     {
           // illegal digit
     }
}

You can also use an if-statement like this:

if (digit >= 'A' && digit <= 'F')

Thanks it's working perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.