Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hex to Decimal

Status
Not open for further replies.

Skute

Programmer
Joined
Jul 21, 2003
Messages
272
Location
GB
Hi,

i have a Hex string which i would like to convert into decimal (non MFC).

char m_cBodyAddress[9] = "80100000";

void* pAddVoid = (void *)&m_cBodyAddress[0];
unsigned char* pAddChar = (unsigned char *)pAddVoid;
unsigned long* pAddLong = (unsigned long *)pAddVoid;

Now, the pointers all point to the same memory (0x0012f8d1). But the values of the long dont match what i have calculated the hex to be.

pAddChar = "80100000" - correct - hex string
pAddLong = 808529976 - incorrect - long version of hex string

The correct output according to my hex calculator should be:
2148532224 (0x80100000)


Can anyone help me out?
Thanks.
 
Casting a pointer to something doesn't perform a convertion on the data type being pointed at

Code:
#include <stdlib.h>
long AddLong = atol( m_cBodyAddress );
 
yeah but that will just give me the hex representation. i need to convert that to a decimal.

AddLong = 80100000

i need to have the value &quot;2148532224&quot;
 
Prepend the string with &quot;0x&quot; and use the strtol function.

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
strtol takes a base

char* endPtr = NULL;
int value = strtol(cBodyAddress,&endPtr,16);

Matt
 
cheers thanks guys,

got it working now, been using the unsigned long version strtoul()
 
>strtol takes a base

Hmmm, yes....if you wanna do it the EASY way...



/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top