V
Vincent
Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
Vincent said:Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
The program will have to work on MS Windows 2000. The char[4] is a set
of characters, read from a file.
I hope this will help you answering my question.
Vincent said:The program will have to work on MS Windows 2000. The char[4] is a set
of characters, read from a file.
I hope this will help you answering my question.
Vincent sade:Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
assert(sizeof(long) == 4);
char b[4] = {0x01,0x02,0x03,0x04};
unsigned long a = 0;
a |= (b[0] << 24);
a |= (b[1] << 16);
a |= (b[2] << 8);
a |= b[3];
But if you have MSB in b[3] then you should reverse the order.
Beware of big endian and little endian.
Tobias
John said:I don't think this is possible without knowing the endian-ness of the
machine. Maybe someone will correct me.
Vincent said:Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
This doesn't actually solve the problem. And what happens if
sizeof(long) is 8, which it is on some 64 bit platforms?
char b[4] = {0x01,0x02,0x03,0x04};
unsigned long a = 0;
a |= (b[0] << 24);
The problem here is that b[0] is promoted to either int or unsigned
int before it is shifted. There are still a large number of platforms
where long has 32 bits but int has only 16. Shifting by 24 on such a
platform is undefined behavior, and will almost certainly give the
wrong results.
The last two do not need the cast. Except maybe platforms where
unsigned char and int both have 16 bits, and the value in the unsigned
char is greater than 255. And yes, there are platforms like this that
actually have C++ compilers.
int x = 1;
endianness = * (char *) & x ? LITTLE_ENDIAN : BIG_ENDIAN;
"Gianni Mariani"
Can someone explain how this expression works? std::reverse is useful for
changing endiann type.
Fraser.
> I see it now. A static_cast would be more understandable.
Vincent skrev:
Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
Use memcpy:
unsigned long ChararrToLong(const char * const src)
{
unsigned long dest;
memcpy(&dest, src, sizeof(dest));
return dest;
}
This may be what you want or not. If you depend on the chars being put
in a specific order into the unsigned long, you might want to do some
byte-swapping while copying.
Tobias said:Jack Klein sade:This doesn't actually solve the problem. And what happens if
sizeof(long) is 8, which it is on some 64 bit platforms?
It fails.
char b[4] = {0x01,0x02,0x03,0x04};
unsigned long a = 0;
a |= (b[0] << 24);
The problem here is that b[0] is promoted to either int or unsigned
int before it is shifted. There are still a large number of platforms
where long has 32 bits but int has only 16. Shifting by 24 on such a
platform is undefined behavior, and will almost certainly give the
wrong results.
True. An
assert(sizeof(int) == 4);
would secure the code.
Vincent said:Thanks for this suggestion. It works! Somewhere else in my script, I
have to convert an unsigned long to a char[4]. I tried to use memcpy to
create a LongtoChararr function, but i failed. I'm not very familiar
with memcpy. Can you help me again?
&x points to a number of bytes which contain (on a big endian machine,
LSB is at highest byte address) 0, 0, ... , 1, and (on a little endian
machine, LSB is at lowest byte address) 1, 0, ... 0
Interpreting the pointer as a char * and getting the byte pointed to
will return the contents of the lowest addressed byte of the word,
which will be 0 for big endian machines and 1 for little endian
machines.
Optimising out the code is presumably a result of gcc recognising that
particular pattern - it would be rather dangerous if you were cross
compiling!
Vincent said:Hi all,
I want to convert a char[4] (binary) to an unsigned long. How can I do
this?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.