473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unpacking ints

Hi

I'm attempting to write a client for an existing p2p network.
The protocol defines that ints are packed into 4 bytes for transfer.

// Creating the byte vector using the following is fine:
for(int i=3; i>=0; i--) {
vecBuff.push_ba ck(value&0xff);
value/=256;
}

// but reading out an int value from the array using
value = (int)data[0] + (int)data[1]*256 + (int)data[2]*65536 +
(int)data[3]*16777216;

Gives varying results. Often correct but sometimes the results is a
negative number, which i don't believe to be correct.

Is my method above for unpacking the int correct?

Thanks loads in advance.
Chris
Jul 22 '05 #1
5 1977
Chris wrote:

Hi

I'm attempting to write a client for an existing p2p network.
The protocol defines that ints are packed into 4 bytes for transfer.

// Creating the byte vector using the following is fine:
for(int i=3; i>=0; i--) {
vecBuff.push_ba ck(value&0xff);
value/=256;
}

// but reading out an int value from the array using
value = (int)data[0] + (int)data[1]*256 + (int)data[2]*65536 +
(int)data[3]*16777216;

Gives varying results. Often correct but sometimes the results is a
negative number, which i don't believe to be correct.

Is my method above for unpacking the int correct?
The casts are wrong. You need to cast the individual bytes to unsigned int
first and cast the total result to int only after the number is constructed.

Since packing and unpacking are platform dependent in any way,
why don't you try if the following works on your machine. Strictly
speaking it is illegal to use a union in this way, but it works on
most compilers.
union Bridge
{
unsigned char Bytes[4];
int TheInt;
};

.....

Bridge Converter;

Converter.TheIn t = 25; // eg. to convert 25
for( i = 0; i < 4; ++i )
do_somthing_wit h( Converter.Bytes[i] );

.....

Bridge Converter;

for( i = 0; i < 4; ++i )
Converter[i].Bytes = get_byte_from_s omewhere;

do_something_wi th_int( Converter.TheIn t );

Thanks loads in advance.
Chris

--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad .at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 22 '05 #2
Chris wrote:
// but reading out an int value from the array using
value = (int)data[0] + (int)data[1]*256 + (int)data[2]*65536 +
(int)data[3]*16777216;

Gives varying results. Often correct but sometimes the results is a
negative number, which i don't believe to be correct.

Is my method above for unpacking the int correct?


You should use unsigned ints for all intermediate calculations, even if
the final result is an int. Also, you have to be careful about type char
being signed or unsigned (from your message, it appears that your
compiler uses signed chars). The whole, pedantic, but safe method is:

value = (int)(
(unsigned int)(unsigned char)data[0] +
(unsigned int)(unsigned char)data[1] * 256u +
(unsigned int)(unsigned char)data[2] * 65536u +
(unsigned int)(unsigned char)data[3] * 16777216u);

The four "(unsigned int)" are probably redundant, but some pedantic
compiler may emit a warning if you don't put them. On the other hand,
the four "(unsigned char)" are essential.

If you want to be more human-friendly, you can also write:

value = (int)(
(unsigned int)(unsigned char)data[0] +
(unsigned int)(unsigned char)data[1] << 8 +
(unsigned int)(unsigned char)data[2] << 16 +
(unsigned int)(unsigned char)data[3] << 24);

The compiler would probably generate the same code, as it knows how to
optimize multiplications with shifts, but it's way more readable, IMHO.

Regards,

Alberto Barbati

Jul 22 '05 #3
Karl Heinz Buchegger wrote:
Is my method above for unpacking the int correct?
The casts are wrong. You need to cast the individual bytes to unsigned int
first and cast the total result to int only after the number is constructed.


Wrong. You need to cast the individual bytes to unsigned char not
unsigned int. Otherwise a signed char will be promoted to (signed) int
before being converted to unsigned int. In fact:

(unsigned)'\xff ' == 4294967295u
(unsigned)(unsi gned char)'\xff' == 255u
Since packing and unpacking are platform dependent in any way,
why don't you try if the following works on your machine. Strictly
speaking it is illegal to use a union in this way, but it works on
most compilers.
union Bridge
{
unsigned char Bytes[4];
int TheInt;
};


Be careful about endianness...

Alberto Barbati
Jul 22 '05 #4
> I'm attempting to write a client for an existing p2p network.
The protocol defines that ints are packed into 4 bytes for transfer.

// Creating the byte vector using the following is fine:
for(int i=3; i>=0; i--) {
vecBuff.push_ba ck(value&0xff);
value/=256;
}

// but reading out an int value from the array using
value = (int)data[0] + (int)data[1]*256 + (int)data[2]*65536 +
(int)data[3]*16777216;

Gives varying results. Often correct but sometimes the results is a
negative number, which i don't believe to be correct.


Use | (bitwise or) instead of + (add). This avoids signs
making a difference. Some people would also advise you to use
left-shifts , eg:

int char_4_to_int(c onst char *cp)
{
return cp[0] | (cp[1] << CHAR_BIT)
| (cp[2] << (CHAR_BIT*2)) | (cp[3] << (CHAR_BIT*3);
}

Also, consider making your int unsigned (depends what you want to do
with it later).
Jul 22 '05 #5
Alberto Barbati wrote:

Karl Heinz Buchegger wrote:
Is my method above for unpacking the int correct?


The casts are wrong. You need to cast the individual bytes to unsigned int
first and cast the total result to int only after the number is constructed.


Wrong. You need to cast the individual bytes to unsigned char not
unsigned int. Otherwise a signed char will be promoted to (signed) int
before being converted to unsigned int.


Right.
I assumed that anybody working at the bytelevel uses unsigned char
to store bytes and not plain char.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #6

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

Similar topics

32
2807
by: Dave Benjamin | last post by:
Hey all, I just realized you can very easily implement a sequence grouping function using Python 2.3's fancy slicing support: def group(values, size): return map(None, * for i in range(size)]) >>> group(range(20), 4)
0
1351
by: Victor S. Miller | last post by:
I'm sure that this has been asked and answered before, but about 1/2 hour of searching hasn't turned up anything. I have external data that requires the packing and unpacking of long vectors of bits. It's not hard to do this "by hand" in Python, but surely there must be some more efficient package that does this. The package struct doesn't...
2
1754
by: george young | last post by:
I came across an cool python feature that I've not seen discussed. This may be *implied* by the language reference manual http://docs.python.org/ref/assignment.html], but it was never obvious to me: An assignment statement can unpack a sequence, binding it's values to explicit names, e.g.: r,g,b = color_tuple and most of us use this...
8
3617
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something with a,b, and c Now I would like to add a special fourth config value to "T": ("T",1,5,4,0.005),
4
3357
by: Claudio Grondi | last post by:
I need to unpack on a Windows 2000 machine some Wikipedia media .tar archives which are compressed with TAR 1.14 (support for long file names and maybe some other features) . It seems, that Pythons tarfile module is able to list far more files inside the archives than WinRAR or 7zip or TotalCommander, but will it unpack all available files...
9
1408
by: tkpmep | last post by:
I have a list y >>>y from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried >>>a,b = y Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
16
1323
by: John Salerno | last post by:
I'm a little confused, but I'm sure this is something trivial. I'm confused about why this works: ('more', 'less'), ('something', 'nothing'), ('good', 'bad')) (('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'), ('good', 'bad')) print x
5
1509
by: ram | last post by:
Stupid question #983098403: I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong? def f(*args, **kw): for a in args: print 'arg:', a for (k,v) in kw.iteritems(): print k, '=', v
21
7232
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP SIGNATURE-----
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
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...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7449
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...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
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...
0
5069
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...
1
1911
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
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.