473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Dec 3 '06 #1
8 8364
Ico
Polaris431 <po********@gmail.comwrote:
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
Dec 3 '06 #2
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
Dec 3 '06 #3
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
Dec 3 '06 #4
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

Dec 3 '06 #5
In article <gc******************************@comcast.com>,
Ernie Wright <er****@comcast.netwrote:
....
>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.

Dec 3 '06 #6
I wrote:
val2 = *(( unsigned long * ) &bitbuf );
Oops. "bitbuf" or "&bitbuf[ 0 ]" but not "&bitbuf".

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

Dec 3 '06 #7
On 3 Dec 2006 06:08:10 -0800, "Polaris431" <po********@gmail.com>
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
Dec 4 '06 #8
"Polaris431" <po********@gmail.comwrote:
# 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
Dec 4 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
7
by: Yama | last post by:
Hi, I have the following binary data: StringData = 800006000000; which is equivalent to the following: byte bytes = new byte; bytes = 0x80;
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
3
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hi, I am looking for the best way to convert a string of length 1 (= 1 character as string) to integer that has the same value as numeric representation of that character. Background: I am...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.