473,465 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

(C++) Get the byte-values from an unsigned int32

1 New Member
Hi there,

I'm trying to "convert" an unsigned int32 to an array of bytes (with a length of 4, of course). Everything i tried up to now didn't work out, so I would really appreciate any help or tip ;-)

My idea is that an uint32 has a length of 4 bytes, and thus it should be possible to take these bytes on their own and do something with them.

Short example:
uint32 contains the (dec) value 439041101, which is in hex 1A2B3C4D.

All I want is an array of 4 char's or bytes with the values:
ret[0]: 1A
ret[1]: 2B
ret[2]: 3C
ret[3]: 4F
(hex-value [and not a string])


Many thanks in advance,

phiber
Jan 6 '09 #1
5 6389
Banfa
9,065 Recognized Expert Moderator Expert
Here's 2 methods:

1. Take a pointer to your integer and cast it to a pointer to unsigned char, not portable but does work on most systems today.

2. Create a union of 1 int and an array of 4 bytes, assign you int to an instance of the union and then read out the byte array.
Jan 6 '09 #2
JosAH
11,448 Recognized Expert MVP
@Banfa
Both these methods fail on a little endian machine but the htonl macro can be of help then.

kind regards,

Jos
Jan 6 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
@JosAH
I think what you mean is both those methods have to take the endian of the machine into account when copying the data out into your array and are therefore non-portable.

On the other hand htonl is part of a third party library that is not available on all platforms and there for also non-portable.

The portable method is to use the bitwise right shift and and operators to extract the byte values.

:D
Jan 6 '09 #4
JosAH
11,448 Recognized Expert MVP
@Banfa
IMHO that borders to paranoia; if a platform even smells like POSIX it has those functions/macros available.

kind regards,

Jos
Jan 6 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
True but 90% - 100% of the reasonable number of platforms I have worked on have not smelt like POSIX at all.
Jan 6 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
7
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
5
by: Gianmaria I. | last post by:
Hi, having a BitArray, how can i extract bits to create a System.byte as in the example... With BitArray bits and Byte myNewByte
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
0
by: shofu_au | last post by:
Hi Group, I am trying without much luck to pack messages into a byte buffer using get and set accessors. I have attached a simplified example of what I am trying to do using byte variables....
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...
0
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...
0
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 ...

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.