Re: String to HEX & BIN Conversion?
- From: Dave Thompson <david.thompson1@xxxxxxxxxxxxxxxx>
- Date: Mon, 17 Oct 2005 02:17:20 GMT
On 12 Oct 2005 11:06:19 -0700, "David Resnick" <lndresnick@xxxxxxxxx>
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
.
- Prev by Date: Re: null function pointer?
- Next by Date: Re: fopen problem
- Previous by thread: Re: String to HEX & BIN Conversion?
- Next by thread: How to determine checksum calculation method
- Index(es):
Relevant Pages
|
|