473,327 Members | 2,081 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,327 software developers and data experts.

Conversion between datatypes on basis of blocks of bytes

AmberJain
884 Expert 512MB
I have an array of 32 elements of type 'unsigned char' (i.e. 32 bytes).
I'll like to store first 4 bytes of this array in a 'unsigned long', next 4 bytes in another 'unsigned long' and so on. Any ideas about how do I perform this?

I'm personally thinking that I need to do something like left shifting and then masking repeatedly but I don't get it right.

Another idea in my mind is to use uint32_t (C99) for this purpose. But I'm confused here too.

Any ideas?
May 6 '10 #1
7 1813
hype261
207 100+
Couldn't you just do a reinterpret_cast to go from one type to another?

unsigned char a[32];

unsigned long * b = reinterpret_cast<unsigned long*>(a);

and then access it like
b[0], b[1], b[2], b[3]
May 6 '10 #2
donbock
2,426 Expert 2GB
Are you required to match any particular endianness when you pack the bytes into larger integers?
May 6 '10 #3
AmberJain
884 Expert 512MB
Are you required to match any particular endianness when you pack the bytes into larger integers?
No.
But I'm on little endian (intel) if this info is needed.
May 7 '10 #4
AmberJain
884 Expert 512MB
Couldn't you just do a reinterpret_cast to go from one type to another?
Isn't reinterpret_cast only for C++? because I had never done so in C. Please correct me if I'm wrong.

And yeah, I'm writing code in C for this task.
May 7 '10 #5
hype261
207 100+
Yes reinterpret_cast is a C++ convention, but if you are just using C then a regular cast should do the trick

unsigned long * b = (unsigned long *)a;
May 7 '10 #6
donbock
2,426 Expert 2GB
Some machines have alignment restrictions for wider integer types like short or long. The pointer-cast approach described above will raise an exception on such a machine unless you are careful about alignment.

You can avoid alignment problems by copying bytes from your big byte array into a union. I've seen this done a lot, but the results are certainly implementation dependent. I think you're relying on undefined behavior, and that's always a bad idea.
Expand|Select|Wrap|Line Numbers
  1. union ab {
  2.    unsigned char a[4];
  3.    long b;
  4.    };
The most reliable and portable way to do this is to start with a clear specification how the byte stream relates to the wider integer types, especially endianness and representation of negative values. They write software that explicitly implements the specification without relying on your machine happening to match the specification.

For example, if your specification says negative numbers are represented in the byte stream using two's-complement, then assemble the bytes in an unsigned type, test the sign bit, if it is set then use explicit logic to negate it (convert it to the equivalent positive number by inverting the bits and adding one), copy the positive value to a signed variable, and then use the unary-minus operator to negate it. This should work regardless of whether your machine uses two's-complement representation or not.
May 7 '10 #7
AmberJain
884 Expert 512MB
Thank you.
May 14 '10 #8

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

Similar topics

11
by: Idar | last post by:
Is there an effecient/fast way in python to convert binary data from file (24bit hex(int) big endian) to 32bit int (little endian)? Have seen struct.unpack, but I am unsure how and what Python has...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: Jan Agermose | last post by:
Im writing information into an existing excel document using a connection string like: strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel...
5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
3
by: Mark | last post by:
What are the best .NET datatypes to handle SQL Server's Float and Real datatypes? I'd like to avoid using the SQL Server specific datatypes like SqlInt32 or similar. Thanks in advance. -Mark
9
by: Mark Rae | last post by:
Hi, I posted a couple of days ago about the possibility of "simulating" in ..NET1.1 the nullable datatypes available in .NET2.0 - I'm nearly there, but require a bit more guidance. Basically,...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
2
by: equinox1248 | last post by:
Hi, I thought this would be answered several time in this group, but I couldn't find anything relevant. What would be the most efficient way of calculating sum of absolute values of two...
10
by: Alessio Sangalli | last post by:
Hi, I am facing some performance issues in an algorithm to "translate" some YUV data in another format. I'll make everything very simple: I have 4 blocks of Y data, one of U data and one of V...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.