Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > convert a char[4] (binary) to an unsigned long

Reply
Thread Tools

convert a char[4] (binary) to an unsigned long

 
 
Vincent
Guest
Posts: n/a
 
      08-05-2005
Hi all,

I want to convert a char[4] (binary) to an unsigned long. How can I do
this?

Thanks,
Vincent

 
Reply With Quote
 
 
 
 
Tobias Blomkvist
Guest
Posts: n/a
 
      08-05-2005
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] << ;
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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      08-05-2005
Vincent wrote:
> Hi all,
>
> I want to convert a char[4] (binary) to an unsigned long. How can I do
> this?
>
> Thanks,
> Vincent
>


I don't think this is possible without knowing the endian-ness of the
machine. Maybe someone will correct me.

If the char[4] came from the machine, then you could probably do a
reinterpret_cast, but I'm almost positive it won't be portable.

// assume this has your binary unsigned long
extern char ul_bin[4];
unsigned long ul = *reinterpret_cast<unsigned long *>(ul_bin);

--John Ratliff
 
Reply With Quote
 
Vincent
Guest
Posts: n/a
 
      08-05-2005
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.

 
Reply With Quote
 
Hans
Guest
Posts: n/a
 
      08-05-2005

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.

 
Reply With Quote
 
Tobias Blomkvist
Guest
Posts: n/a
 
      08-05-2005
Vincent sade:
> 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.
>


Find out what format the long's are stored in, what endianness.

long l = 0x04030201

can be stored as

Big endian: 0x04030201
Little endian: 0x01020304

Or any random order you desire in your file, but if you don't
know the byte order, how will you be able to read them back correctly?

http://en.wikipedia.org/wiki/Endianess

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
 
Reply With Quote
 
John Ratliff
Guest
Posts: n/a
 
      08-05-2005
Vincent wrote:
> 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.
>


It will only work if the char[4] read from Windows was created on a
machine with endian-ness the same as Windows 2000 (little endian for
x86) written in endian correct order.

In other words, if you wrote an unsigned long created on a machine to a
file, and then wanted to read that unsigned long from a char[4] byte
array on the same machine, the reinterpret_cast would work. If this file
is created on some other machine, you can only know if it would work if
you know the endian-ness of the machine which created the file.

A question though, why are you reading an unsigned long into a char[4]
array anyways? Why not read it directly into an unsigned long? Or, how
does the unsigned long get written in the first place? Maybe you should
consider writing it as a string instead and parsing the string back
using strtoul() instead.

--John Ratliff
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-06-2005
On Fri, 05 Aug 2005 12:25:38 +0200, Tobias Blomkvist <(E-Mail Removed)>
wrote in comp.lang.c++:

> 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);


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.

Perhaps you think that the extra step of initializing 'a' to 0 and
using |= forces b[0] to be promoted to unsigned long. It most
certainly does not. b[0] is promoted to either int or unsigned int,
the shift is performed and assuming there is no undefined behavior or
the program continues regardless, the resulting unsigned int value is
only then promoted to unsigned long.

Should be:

unsigned long a = ((unsigned long)b1 << 24);

> a |= (b[1] << 16);


Same cast here.

> a |= (b[2] << ;
> a |= b[3];


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.

> But if you have MSB in b[3] then you should reverse the order.
> Beware of big endian and little endian.
>
> Tobias


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-06-2005
John Ratliff wrote:

> I don't think this is possible without knowing the endian-ness of the
> machine. Maybe someone will correct me.


int x = 1;

endianness = * (char *) & x ? LITTLE_ENDIAN : BIG_ENDIAN;

Some compilers (GCC for sure) can optimize away code using this expression.

e.g.

int x = 1;
if ( * (char *) & x )
{
... little endian code ...
... optimized away when compiled for a little endian machine ...
} else
{
... big endian code ...
... optimized away when compiled for a big endian machine ...
}
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-06-2005
Vincent wrote:
> Hi all,
>
> I want to convert a char[4] (binary) to an unsigned long. How can I do
> this?



See:
http://groups-beta.google.com/group/...6?dmode=source

http://groups.google.com/group/comp....F-8&edition=us
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
unsigned long to unsigned char ashtonn@gmail.com Python 1 06-01-2005 07:00 PM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments