473,698 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi all,

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

Thanks,
Vincent

Aug 5 '05 #1
19 16800
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(l ong) == 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
--
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.
Aug 5 '05 #2
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_cas t, 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_ca st<unsigned long *>(ul_bin);

--John Ratliff
Aug 5 '05 #3
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.

Aug 5 '05 #4

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(c onst 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.

Aug 5 '05 #5
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.
Aug 5 '05 #6
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_cas t 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
Aug 5 '05 #7
On Fri, 05 Aug 2005 12:25:38 +0200, Tobias Blomkvist <vo**@void.void >
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(l ong) == 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] << 8);
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 6 '05 #8
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 ...
}
Aug 6 '05 #9
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
Aug 6 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
31506
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
19
4136
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
2
32316
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
7
13080
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
7
7109
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done without strtol or s/printf function. Thanks, whatluo.
65
21425
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. #include <string.h> #include <stdio.h> /* Convert an ipv4 address to long integer */ /* "192.168.1.1" --> 3232235777 */ unsigned long iptol(char *ip){
24
14915
by: cedarson | last post by:
I am having trouble writing a simple code that will convert an int ( 487 for example ) to binary form just for the sake of printing the binary. Can someone please help? Thanks!
7
19216
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
29
9976
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
8600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7711
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5859
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3038
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 we have to send another system
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.