String to HEX & BIN Conversion?

Discussion in 'C Programming' started by salsipius, Oct 12, 2005.

  1. salsipius

    salsipius Guest

    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?
     
    salsipius, Oct 12, 2005
    #1
    1. Advertising

  2. 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
     
    David Resnick, Oct 12, 2005
    #2
    1. Advertising

  3. salsipius

    salsipius Guest

    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
     
    salsipius, Oct 12, 2005
    #3
  4. 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
     
    Peter Nilsson, Oct 13, 2005
    #4
  5. On 12 Oct 2005 11:06:19 -0700, "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 <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
     
    Dave Thompson, Oct 17, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,271
    Neredbojias
    Aug 19, 2005
  2. GIR

    Convert HEX string to bin

    GIR, Nov 30, 2003, in forum: C Programming
    Replies:
    53
    Views:
    16,910
    goose
    Dec 10, 2003
  3. Bengt Richter
    Replies:
    6
    Views:
    488
    Juha Autero
    Aug 19, 2003
  4. dominolog
    Replies:
    4
    Views:
    5,626
    dominolog
    Sep 2, 2008
  5. anne001
    Replies:
    1
    Views:
    520
Loading...

Share This Page