ASCII -> long (hex)

T

Thomas Baier

Hi there,

I've got a little problem. My aim is to send a hex file to a
microcontroller. So I'm reading the hex-File into a buffer and then have to
convert it to send the hex numbers. So now I want to know how I can get a
long out of something like
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);
but this only returns 0, instead of 255. So how to do this?

Thanks for any help

Thomas
 
S

Siemel Naran

Thomas Baier said:
I've got a little problem. My aim is to send a hex file to a
microcontroller. So I'm reading the hex-File into a buffer and then have to
convert it to send the hex numbers. So now I want to know how I can get a
long out of something like
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);
but this only returns 0, instead of 255. So how to do this?

Thanks for any help

Does this work?

std::istringstream in(buf);
long number;
in >> std::hex >> number;
 
D

David Harmon

On Sun, 20 Jun 2004 22:34:11 +0200 in comp.lang.c++, Thomas Baier
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);

long number = strtol(buf, buf+strlen(buf), 0);

Did you even look up atoi(). Did you have any reason to suspect that it
might do what you want? Of course it returns zero; the first character
if what you passed to it is zero and the character after that is not a
digit.
 
S

Siemel Naran

red floyd said:
also look at std::strtol()

That's a good function too, though I didn't know it before.

Evidently if you pass the third argument (the radix) as 0, then the function
will determine the base from the first 2 letters, and "0x" means
hexadecimal. The second argument will store a pointer to the character that
stopped the scan, and we can pass NULL if we don't care to know this
information. It returns a long. So

long number = strltol("0xFF", NULL, 0);

I looked for a function strtoi to convert a string to an integer, but failed
to find one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top