Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String to HEX & BIN Conversion?

Reply
Thread Tools

String to HEX & BIN Conversion?

 
 
salsipius
Guest
Posts: n/a
 
      10-12-2005
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?

 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      10-12-2005
salsipius wrote:
> 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

 
Reply With Quote
 
 
 
 
salsipius
Guest
Posts: n/a
 
      10-12-2005
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

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-13-2005
David Resnick wrote:
> salsipius wrote:
> > 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);

> 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.


--
Peter

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      10-17-2005
On 12 Oct 2005 11:06:19 -0700, "David Resnick" <(E-Mail Removed)>
wrote:

> salsipius wrote:
> > 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.

> > 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.
>

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
hex string to hex value tim Python 8 11-23-2005 06:27 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments