# Help with Hex To Int

This topic is 5345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I convert a 2 character string such as '1F' and convert it into an integer? I looked around at a listing of the standard functions, but I didn't see anythign that appeared to do this. :S

##### Share on other sites
Quote:
 Original post by Vampyre_DarkHow do I convert a 2 character string such as '1F' and convert it into an integer? I looked around at a listing of the standard functions, but I didn't see anythign that appeared to do this. :S
Try strtoul, it allows you to specify the integer's base.
int result = strtoul(string, NULL, 16);
scanf("%x") is another possibility, but I tend to avoid it if possible due to its error handling issues.

##### Share on other sites
I think std::hex can be used to read/write hexidecimal numbers from streams in C++, and scanf should work in C.

Oh, strtol. I should have thought of that, I used to use that a lot.

##### Share on other sites
Okay, Thanks alot. I did this... it compiles, but I won't be able to test it until the rest of my map parser is done. :)

//grab the ambient color
//and convert the hex value back
//to 3 floats
fMap.get(ca,2); //alpha
fMap.get(ca,2); //r
m_LightAmbient[0] = ((float)strtoul(ca,NULL,16) /255);
fMap.get(ca,2); //g
m_LightAmbient[1] = ((float)strtoul(ca,NULL,16) /255);
fMap.get(ca,2); //b
m_LightAmbient[2] = ((float)strtoul(ca,NULL,16) /255);
fMap.get(ca,256,';');

##### Share on other sites
I just wrote my own function:
BYTE FromHex(char ch){   switch(tolower(ch))   {   case '0': return 0;   case '1': return 1;   case '2': return 2;   case '3': return 3;   case '4': return 4;   case '5': return 5;   case '6': return 6;   case '7': return 7;   case '8': return 8;   case '9': return 9;   case 'a': return 10;   case 'b': return 11;   case 'c': return 12;   case 'd': return 13;   case 'e': return 14;   case 'f': return 15;   default: return 0;   }}BYTE FromHex(char ch1, char ch2){   return (FromHex(ch1)<<4) | FromHex(ch2);}

That's just off the top of my head though.

##### Share on other sites
Because I have a large hammer and like to use it to pound small nails:

#include <boost/spirit.hpp>#include <iostream>#include <string>using namespace std;using namespace boost::spirit;int main ( void ){	string hex="1F";	unsigned int value;	parse( hex.c_str() , hex_p[assign_a(value)] ); //converts to native number	cout << "Decimal value: " << value << endl; //cout defaults to decimal	string buffer;	getline( cin , buffer );}

##### Share on other sites
Couldn't you just typecast it?

int HexValue = (int)CharString;

I've tried assigning hex values to ints and it just 'translates' them for you.

int Value = 0xF;

Makes Value == 15.

##### Share on other sites
Nope. Then hexvalue would contain the address of the string (as that is what is stored in CharString).

Note the difference between:

int Value = 0xF;

and

int Value = (int)"0xF";

##### Share on other sites
Here's how I did it. Hopefully the code works, 'cause I didn't test it recently...just pulled it from an old project.

typedef unsigned char UCHAR;typedef unsigned int UINT;char hexToNibble(char n){	assert( (n >= '0') && (n <= '9') || (n >= 'A') && (n <= 'F') || (n >= 'a') && (n <= 'f') );  // make sure it's a hex digit!	if ( (n >= '0') && (n <= '9') ) return( n & 0x0F );	if ( (n >= 'A') && (n <= 'F') ) return( n - 55 );	if ( (n >= 'a') && (n <= 'f') ) return( n - 87 );		return 0;  // should never be here assuming assert worked in debug mode}UCHAR hexToByte(const char *b){	return (UCHAR)((hexToNibble(b[0]) << 4) | hexToNibble((b[1])));}int hexToInt (const char *s){	int j = 0;	char *p = (char *)s;	while (*p)	{		j <<= 4;		j |= hexToNibble(*(p++));	}	return j;}

hexToInt is the one you want

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26