Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading "integer" size bytes from a char* array.

Tags:

c++

c

endianness

I want to read sizeof(int) bytes from a char* array.

a) In what scenario's do we need to worry if endianness needs to be checked?

b) How would you read the first 4 bytes either taking endianness into consideration or not.

EDIT : The sizeof(int) bytes that I have read needs to be compared with an integer value.

What is the best approach to go about this problem

like image 909
kal Avatar asked Feb 13 '09 06:02

kal


People also ask

How do I find the size of a char array?

first, the char variable is defined in charType and the char array in arr. Then, the size of the char variable is calculated using sizeof() operator. Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.

Can we store int in char array?

You can only store int type in array "arr" there. Arrays in c/c++/java are homogenious (same) type data structure.

How many bytes is a char array in Java?

The first 128 characters (corresponding to the characters available in ASCII) can be stored in one byte each. Characters with order numbers 128 or higher need two or more bytes.


3 Answers

Do you mean something like that?:

char* a;
int i;
memcpy(&i, a, sizeof(i));

You only have to worry about endianess if the source of the data is from a different platform, like a device.

like image 197
Dani van der Meer Avatar answered Oct 14 '22 15:10

Dani van der Meer


a) You only need to worry about "endianness" (i.e., byte-swapping) if the data was created on a big-endian machine and is being processed on a little-endian machine, or vice versa. There are many ways this can occur, but here are a couple of examples.

  1. You receive data on a Windows machine via a socket. Windows employs a little-endian architecture while network data is "supposed" to be in big-endian format.
  2. You process a data file that was created on a system with a different "endianness."

In either of these cases, you'll need to byte-swap all numbers that are bigger than 1 byte, e.g., shorts, ints, longs, doubles, etc. However, if you are always dealing with data from the same platform, endian issues are of no concern.

b) Based on your question, it sounds like you have a char pointer and want to extract the first 4 bytes as an int and then deal with any endian issues. To do the extraction, use this:

int n = *(reinterpret_cast<int *>(myArray)); // where myArray is your data

Obviously, this assumes myArray is not a null pointer; otherwise, this will crash since it dereferences the pointer, so employ a good defensive programming scheme.

To swap the bytes on Windows, you can use the ntohs()/ntohl() and/or htons()/htonl() functions defined in winsock2.h. Or you can write some simple routines to do this in C++, for example:

inline unsigned short swap_16bit(unsigned short us)
{
    return (unsigned short)(((us & 0xFF00) >> 8) |
                            ((us & 0x00FF) << 8));
}

inline unsigned long swap_32bit(unsigned long ul)
{
    return (unsigned long)(((ul & 0xFF000000) >> 24) |
                           ((ul & 0x00FF0000) >>  8) |
                           ((ul & 0x0000FF00) <<  8) |
                           ((ul & 0x000000FF) << 24));
}
like image 27
Matt Davis Avatar answered Oct 14 '22 15:10

Matt Davis


Depends on how you want to read them, I get the feeling you want to cast 4 bytes into an integer, doing so over network streamed data will usually end up in something like this:

int foo = *(int*)(stream+offset_in_stream);
like image 32
Daniel Sloof Avatar answered Oct 14 '22 16:10

Daniel Sloof