473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8395
Ico
Polaris431 <po********@gma il.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********@gma il.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********@gma il.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,stri ng); 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",si zeof 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
41333
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 nmea checksum for gps data. e.g. everything between $ and * needs to be xor: $GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287,00,05,25,103,44* to...
11
17391
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 internal string StringToAscii(string S) { byte strArray = Encoding.UTF7.GetBytes(S); string NewString = Encoding.UTF7.GetString(strArray);
7
10806
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
7231
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 Byte Dim b As Byte
14
1463
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 integer that was entered. So, if 353 was entered, the output should be 11.
20
3407
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 ----> 0001 2 ----> 0002
7
19200
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
3
2874
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 writing functions abstracting endianness, e.g. converting a string of length 4 to the appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big...
10
4927
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
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.