How to convert a 4 byte character string to its equivalent 4 byte integer value?

Discussion in 'C Programming' started by Polaris431, Dec 3, 2006.

  1. Polaris431

    Polaris431 Guest

    I have a buffer that holds characters. Four characters in a row
    represent an unsigned 32 bit value. I want to convert these characters
    to a 32 bit value. For example:

    char buffer[3];

    buffer = "aabbccdd";

    where aa is the LSB and dd is the MSB.

    If I define a unsigned 32 variable:

    uint value;

    I want value to be equal to 0xaabbccdd.

    I thought of making a pointer to point to the address of the value
    variable and copy each character into the location but I can't figure
    out how to do that, if it is even possible at all.

    I had another solution of converting each character to a byte and
    shifting it into the value variable but I want to avoid shifting due to
    the overhead required for shifting on my target system.

    Any suggestions?

    Thanks
     
    Polaris431, Dec 3, 2006
    #1
    1. Advertising

  2. Polaris431

    Ico Guest

    Polaris431 <> wrote:
    > I have a buffer that holds characters. Four characters in a row
    > represent an unsigned 32 bit value. I want to convert these characters
    > to a 32 bit value. For example:
    >
    > char buffer[3];
    >
    > buffer = "aabbccdd";


    No, this is not going to work in any way.

    Do you mean something like

    char buffer[] = "aabbccdd";

    or more like

    char buffer[] = { 0xaa, 0xbb, 0xcc, 0xdd };

    ?

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, Dec 3, 2006
    #2
    1. Advertising

  3. Polaris431

    pete Guest

    Polaris431 wrote:
    >
    > I have a buffer that holds characters. Four characters in a row
    > represent an unsigned 32 bit value. I want to convert these characters
    > to a 32 bit value. For example:
    >
    > char buffer[3];
    >
    > buffer = "aabbccdd";
    >
    > where aa is the LSB and dd is the MSB.
    >
    > If I define a unsigned 32 variable:
    >
    > uint value;
    >
    > I want value to be equal to 0xaabbccdd.
    >
    > I thought of making a pointer to point to the address of the value
    > variable and copy each character into the location but I can't figure
    > out how to do that, if it is even possible at all.
    >
    > I had another solution of converting each character to a byte and
    > shifting it into the value variable
    > but I want to avoid shifting due to
    > the overhead required for shifting on my target system.
    >
    > Any suggestions?
    >
    > Thanks


    /* BEGIN new.c */

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    char buffer[] = "0xabcd";
    long unsigned value = strtol(buffer, NULL, 16);

    printf("value is %lx\n", value);
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Dec 3, 2006
    #3
  4. Polaris431

    pete Guest

    pete wrote:
    >
    > Polaris431 wrote:
    > >
    > > I have a buffer that holds characters. Four characters in a row
    > > represent an unsigned 32 bit value. I want to convert these characters
    > > to a 32 bit value. For example:
    > >
    > > char buffer[3];
    > >
    > > buffer = "aabbccdd";
    > >
    > > where aa is the LSB and dd is the MSB.
    > >
    > > If I define a unsigned 32 variable:
    > >
    > > uint value;
    > >
    > > I want value to be equal to 0xaabbccdd.
    > >
    > > I thought of making a pointer to point to the address of the value
    > > variable and copy each character into the location but I can't figure
    > > out how to do that, if it is even possible at all.
    > >
    > > I had another solution of converting each character to a byte and
    > > shifting it into the value variable
    > > but I want to avoid shifting due to
    > > the overhead required for shifting on my target system.
    > >
    > > Any suggestions?


    > char buffer[] = "0xabcd";
    > long unsigned value = strtol(buffer, NULL, 16);


    It will also work this way:

    char buffer[] = "abcd";
    long unsigned value = strtol(buffer, NULL, 0x10);

    --
    pete
     
    pete, Dec 3, 2006
    #4
  5. Polaris431

    Ernie Wright Guest

    Re: How to convert a 4 byte character string to its equivalent 4byte integer value?

    Polaris431 wrote:

    > I have a buffer that holds characters. Four characters in a row
    > represent an unsigned 32 bit value. I want to convert these characters
    > to a 32 bit value.


    Do the four characters contain text representing a value, or do they
    contain the bit image of a value?

    char numstr[] = "1234";
    unsigned char bitbuf[] = { 0x01, 0x02, 0x03, 0x04 };
    unsigned long val1, val2;

    val1 = strtoul( numstr, NULL, 10 ); /* val1 = 1234 */

    val2 = bitbuf[ 3 ] << 24 | /* val2 = 0x04030201 */
    bitbuf[ 2 ] << 16 | /* = 67305985 */
    bitbuf[ 1 ] << 8 |
    bitbuf[ 0 ];

    > For example:
    >
    > char buffer[3];
    >
    > buffer = "aabbccdd";
    >
    > where aa is the LSB and dd is the MSB.


    This is going to hopelessly confuse people here, who will interpret what
    you've written as literal C code: You create a char array of length 3,
    not 4, and then try to put 8 chars in it, using an invalid assignment.

    Correct me if I'm wrong, but I think you meant "[4]", and that you're
    trying to say that buffer[] contains the bit image of a 32-bit integer
    in little-endian byte order.

    > I thought of making a pointer to point to the address of the value
    > variable and copy each character into the location but I can't figure
    > out how to do that, if it is even possible at all.


    It's not uncommon to see something that works the other way around,
    forming a pointer to the character array and reinterpreting what it
    points to:

    val2 = *(( unsigned long * ) &bitbuf );

    This takes the address of bitbuf, interprets it as a pointer to
    unsigned long, then dereferences that pointer and assigns the value.

    This sometimes works, but it's not portable. It assumes that the byte
    order of long is the same as the order of the bytes in bitbuf[] on the
    host platform, that sizeof long is 4, and that the address of bitbuf[0]
    is aligned properly for a long on the host platform.

    > I had another solution of converting each character to a byte and
    > shifting it into the value variable but I want to avoid shifting due
    > to the overhead required for shifting on my target system.


    Bit shifting is typically just about the fastest thing you can do.

    The bit shifting and bitwise-OR in

    val2 = bitbuf[ 3 ] << 24 |
    bitbuf[ 2 ] << 16 |
    bitbuf[ 1 ] << 8 |
    bitbuf[ 0 ];

    can be replaced by multiplication and addition,

    val2 = bitbuf[ 3 ] * 0x1000000 +
    bitbuf[ 2 ] * 0x10000 +
    bitbuf[ 1 ] * 0x100 +
    bitbuf[ 0 ];

    but I'd be very surprised if the second form was faster than the first
    on any platform.

    - Ernie http://home.comcast.net/~erniew
     
    Ernie Wright, Dec 3, 2006
    #5
  6. Re: How to convert a 4 byte character string to its equivalent 4byte integer value?

    In article <>,
    Ernie Wright <> wrote:
    ....
    >> char buffer[3];
    >>
    >> buffer = "aabbccdd";
    >>
    >> where aa is the LSB and dd is the MSB.

    >
    >This is going to hopelessly confuse people here, who will interpret what
    >you've written as literal C code: You create a char array of length 3,
    >not 4, and then try to put 8 chars in it, using an invalid assignment.


    Not to mention that it won't compile - any more than:

    42 = "aabbccdd";

    would.
     
    Kenny McCormack, Dec 3, 2006
    #6
  7. Polaris431

    Ernie Wright Guest

    Re: How to convert a 4 byte character string to its equivalent 4byte integer value?

    I wrote:

    > val2 = *(( unsigned long * ) &bitbuf );


    Oops. "bitbuf" or "&bitbuf[ 0 ]" but not "&bitbuf".

    - Ernie http://home.comcast.net/~erniew
     
    Ernie Wright, Dec 3, 2006
    #7
  8. On 3 Dec 2006 06:08:10 -0800, "Polaris431" <>
    wrote:

    >I have a buffer that holds characters. Four characters in a row
    >represent an unsigned 32 bit value. I want to convert these characters
    >to a 32 bit value. For example:
    >
    >char buffer[3];
    >
    >buffer = "aabbccdd";


    Did you mean char buffer[8] = "aabbccdd";?

    >
    >where aa is the LSB and dd is the MSB.
    >
    >If I define a unsigned 32 variable:
    >
    >uint value;


    int can be as small as 16 bits. Did you mean unsigned long which is
    guaranteed to be at least 32 bits.

    >
    >I want value to be equal to 0xaabbccdd.


    In the hex value you have above, aa is the most significant 8 bits. In
    your description at the beginning, aa is the least significant. Make
    up your mind
    >
    >I thought of making a pointer to point to the address of the value
    >variable and copy each character into the location but I can't figure
    >out how to do that, if it is even possible at all.


    memcpy will let you copy from buffer to the value but it won't give
    you the value you are asking for. If have an ASCII system, copying
    the first a will put 0x61 into a byte in value.

    >
    >I had another solution of converting each character to a byte and


    char values are bytes, by definition.

    >shifting it into the value variable but I want to avoid shifting due to
    >the overhead required for shifting on my target system.


    Shifting is normally one of the things CPUs do well. What makes you
    think it involves a lot of overhead on your system? Avoid the false
    path of premature optimization. How do you expect shifting to convert
    the 0x61 of the 'a' into the bit pattern 1010 for the 0xa half-byte
    you want?

    Look at strtoul and sscanf. Both can convert a sequence of valid
    hexadecimal characters to the equivalent integer value.


    Remove del for email
     
    Barry Schwarz, Dec 4, 2006
    #8
  9. Polaris431

    SM Ryan Guest

    "Polaris431" <> wrote:
    # I have a buffer that holds characters. Four characters in a row
    # represent an unsigned 32 bit value. I want to convert these characters
    # to a 32 bit value. For example:
    #
    # char buffer[3];
    #
    # buffer = "aabbccdd";
    #
    # where aa is the LSB and dd is the MSB.
    #
    # If I define a unsigned 32 variable:

    If you want something like 'DCBA',
    unsigned char *string = (unsigned char*)"ABCD";
    long value = (string[0])
    | (string[1]<<8)
    | (string[2]<<16)
    | (string[3]<<24);

    If you want to evaluate the hex string "aabbccdd" as if 0xddbbccaa,
    char *string = "aabbccdd";
    char rev[strlen(string)+1],*f,*r;
    strcpy(rev,string); f = rev; r = rev+strlen(rev)-1;
    while (f<r) {
    char t = *f; *f++ = *r; *r-- = t;
    }
    long value = strtol(rev,0,16);

    # I had another solution of converting each character to a byte and
    # shifting it into the value variable but I want to avoid shifting due to
    # the overhead required for shifting on my target system.

    Doing
    long value; memcpy((char*)&value,"ABCD",sizeof value);
    might work, but it makes you machine dependent on the string byte order,
    variable type alignment, etc.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    GERBILS
    GERBILS
    GERBILS
     
    SM Ryan, Dec 4, 2006
    #9
    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:
    27
    Views:
    716
    Neil Cerutti
    Sep 29, 2006
  2. =?utf-8?B?Qm9yaXMgRHXFoWVr?=
    Replies:
    3
    Views:
    453
    John Machin
    Sep 15, 2007
  3. Arnold Shore

    How to Convert a Byte to its Numeric Value?

    Arnold Shore, Oct 9, 2003, in forum: ASP General
    Replies:
    1
    Views:
    138
    Arnold Shore
    Oct 9, 2003
  4. thunk
    Replies:
    1
    Views:
    364
    thunk
    Mar 30, 2010
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    818
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page