Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to convert a 4 byte character string to its equivalent 4 byte integer value?

Reply
Thread Tools

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

 
 
Polaris431
Guest
Posts: n/a
 
      12-03-2006
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

 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      12-03-2006
Polaris431 <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-03-2006
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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-03-2006
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
 
Reply With Quote
 
Ernie Wright
Guest
Posts: n/a
 
      12-03-2006
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

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      12-03-2006
In article <(E-Mail Removed)>,
Ernie Wright <(E-Mail Removed)> 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.

 
Reply With Quote
 
Ernie Wright
Guest
Posts: n/a
 
      12-03-2006
I wrote:

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


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

- Ernie http://home.comcast.net/~erniew

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-04-2006
On 3 Dec 2006 06:08:10 -0800, "Polaris431" <(E-Mail Removed)>
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
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      12-04-2006
"Polaris431" <(E-Mail Removed)> 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]<<
| (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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
convert n-byte string into integer Maligree Python 2 04-28-2010 09:14 PM
int('\x23') != 0x23 (a.k.a convert char to integer of its byte representation) =?utf-8?B?Qm9yaXMgRHXFoWVr?= Python 3 09-15-2007 12:41 PM
Can string formatting be used to convert an integer to its binaryform ? fdu.xiaojf@gmail.com Python 27 09-29-2006 02:47 PM



Advertisments