Help with Hex To Int

Started by
7 comments, last by vEEcEE 19 years, 1 month ago
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
Advertisement
Quote:Original post by Vampyre_Dark
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
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.
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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,';');
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.
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 );}

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.
-Conrad
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";
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

This topic is closed to new replies.

Advertisement