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

P

Polaris431

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
 
I

Ico

Polaris431 said:
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 };

?
 
P

pete

Polaris431 said:
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 */
 
P

pete

pete said:
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);
 
E

Ernie Wright

Polaris431 said:
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
 
K

Kenny McCormack

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.[/QUOTE]

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

42 = "aabbccdd";

would.
 
B

Barry Schwarz

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
 
S

SM Ryan

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top