String to HEX & BIN Conversion?

S

salsipius

I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long so I can test individual bits of the number. using

sscanf(in,"%x", &l1);

the result is 4, this is great but I also want the other half of the
char array . How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10

Can anyone help me out on this please?
 
D

David Resnick

salsipius said:
I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long so I can test individual bits of the number. using

sscanf(in,"%x", &l1);

the result is 4, this is great but I also want the other half of the
char array . How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10

Can anyone help me out on this please?

int ret = sscanf(in, "%x %x", &l1, &l2);

Of course you should make sure sscanf returns 2 before
acting as if it worked. If you want to do it with multiple
statements or with better understanding/error handling of what
is being converted, I'd suggest using strtoul with base 16
and an endpointer.

-David
 
S

salsipius

Thanks alot, I broke down and used a for loop to copy in[j+5] to copy
into a new array, but your solutiion works much better. Thanks
 
P

Peter Nilsson

David said:
salsipius said:
I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long so I can test individual bits of the number. using

sscanf(in,"%x", &l1);

the result is 4, this is great but I also want the other half of the
char array . How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10

Can anyone help me out on this please?

int ret = sscanf(in, "%x %x", &l1, &l2);

Or just...

int ret = sscanf(in, "%x%x", &l1, &l2);
 
D

Dave Thompson

salsipius said:
I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long <snip> How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10
If you really use a long you should use %lx. In fact for that you
should use _unsigned_ long, which is better for bitbashing anyway.
%x expects (& of) unsigned int, which may happen to be the same as
unsigned long on some systems but not all, which in turn is guaranteed
the same as signed (default) long only for positive values.
int ret = sscanf(in, "%x %x", &l1, &l2);

Of course you should make sure sscanf returns 2 before
acting as if it worked. If you want to do it with multiple
statements or with better understanding/error handling of what
is being converted, I'd suggest using strtoul with base 16
and an endpointer.
Agree with both, but for completeness another possibility:

int ret, off,
ret = sscanf (in, "%lx%n", &l1, &off);
if( ret != 1 ) error;
ret = sscanf (in+off, "%lx", &l2)
if( ret != 1 ) error;

- David.Thompson1 at worldnet.att.net
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top