473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with unpack hex to decimal

Hello,

I was looking at this:
http://docs.python.org/lib/module-struct.html
and tried the following
import struct
struct.calcsize('h') 2 struct.calcsize('b') 1 struct.calcsize('bh') 4

I would have expected
struct.calcsize('bh')

3

what am I missing ?

Thanks in advance,

Jake.

Jul 19 '05 #1
6 2608
"se*******@gmail.com" wrote:
I was looking at this:
http://docs.python.org/lib/module-struct.html
and tried the following
import struct
struct.calcsize('h') 2 struct.calcsize('b') 1 struct.calcsize('bh') 4

I would have expected
struct.calcsize('bh')

3

what am I missing ?


the sentence

"By default, C numbers are represented in the machine's native format
and byte order, and properly aligned by skipping pad bytes if necessary
(according to the rules used by the C compiler)."

and the text and table following that sentence.

</F>

Jul 19 '05 #2

<se*******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello,

I was looking at this:
http://docs.python.org/lib/module-struct.html
and tried the following
import struct
struct.calcsize('h') 2 struct.calcsize('b') 1 struct.calcsize('bh') 4

I would have expected
struct.calcsize('bh') 3

what am I missing ?


Not sure, however I also find the following confusing:
struct.calcsize('hb') 3 struct.calcsize('hb') == struct.calcsize('bh')

False

I could understand aligning to multiples of 4, but why is 'hb' different
from 'bh'?
Jul 19 '05 #3
Jonathan Brady wrote:
... I also find the following confusing:
struct.calcsize('hb') 3
struct.calcsize('hb') == struct.calcsize('bh')

False

I could understand aligning to multiples of 4, but why is 'hb' different
from 'bh'?


Pad bytes have never been, as far as I know, added
at the end of structs in C, only between fields, when
required. So the leading "b" gets a following pad
byte, but the trailing one doesn't need padding.

-Peter
Jul 19 '05 #4
On Sun, 17 Apr 2005 20:47:20 +0100, "Jonathan Brady"
<no****@denbridgedigital.com> wrote:

<se*******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googleg roups.com...
Hello,

I was looking at this:
http://docs.python.org/lib/module-struct.html
and tried the following
> import struct
> struct.calcsize('h') 2
> struct.calcsize('b')

1
> struct.calcsize('bh')

4

I would have expected
> struct.calcsize('bh')

3

what am I missing ?

A note for the original poster: "unpack hex to decimal" (the subject
line from your posting) is an interesting concept. Hex[adecimal] and
decimal are ways of representing the *same* number.

Let's take an example of a two-byte piece of data. Suppose the first
byte has all bits set (== 1) and the second byte has all bits clear
(== 0). The first byte's value is hexadecimal FF or decimal 255,
whether or not you unpack it, if you are interpreting it as an
unsigned number ('B' format). Signed ('b' format) gives you
hexadecimal -1 and decimal -1. The second byte's value is 0
hexadecimal and 0 decimal however you interpret it.

Suppose you want to interpret the two bytes as together representing a
16-bit signed number (the 'h' format). If the perp is little-endian,
the result is hex FF and decimal 255; otherwise it's hex -100 and
decimal -256.

Not sure, however I also find the following confusing: struct.calcsize('hb')3 struct.calcsize('hb') == struct.calcsize('bh')
False

I could understand aligning to multiples of 4,


Given we know nothing about the OP's platform or your platform, "4" is
no more understandable than any other number.
but why is 'hb' different
from 'bh'?


Likely explanation: the C compiler aligns n-byte items on an n-byte
boundary. Thus in 'hb', the h is at offset 0, and the b can start OK
at offset 2, for a total size of 3. With 'bh', the b is at offset 0,
but the h can't (under the compiler's rules) start at 1, it must start
at 2, for a total size of 4.

Typically, you would use "native" byte ordering and alignment (the
default) only where you are accessing data in a C struct that is in
code that is compiled on your platform [1]. When you are picking apart
a file that has been written elsewhere, you will typically need to
read the documentation for the file format and/or use trial & error to
determine which prefix (@, <, >) you should use. If I had to guess for
you, I'd go for "<".

[1] Care may need to be taken if the struct is defined in source
compiled by a compiler *other* than the one used to compile your
Python executable -- there's a slight chance you might need to fiddle
with the "foreign" compiler's alignment options to make it suit.

HTH,

John

Jul 19 '05 #5
Jonathan Brady wrote:
<se*******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello,

I was looking at this:
http://docs.python.org/lib/module-struct.html
and tried the following

>import struct
>struct.calcsize('h')


2
>struct.calcsize('b')


1
>struct.calcsize('bh')


4

I would have expected

>struct.calcsize('bh')


3

what am I missing ?

Not sure, however I also find the following confusing:
struct.calcsize('hb')
3
struct.calcsize('hb') == struct.calcsize('bh')


False

I could understand aligning to multiples of 4, but why is 'hb' different
from 'bh'?

Evidently, shorts need to be aligned at an even address on your
platform. Consider the following layout, where `b' represents the signed
char, `h' represents the bytes occupied by the short and `X' represents
unused bytes (due to alignment.

'bh', a signed char followed by a short would look like:

bXhh -- or four bytes, but 'hb', a short followed by a signed char would be:

hhb (as `char' and its siblings have no alignment requirements)

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 19 '05 #6
Thank you all very much. It looked like I was not the only one
confused.

Jake.

Jul 19 '05 #7

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

Similar topics

2
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
6
by: WipeOut | last post by:
I am having a problem in a perl script that I can't seem to find an answer for.. The $cost and $retail vars come from another part of the script and would be something like 000134.345 and...
3
by: Chris Garland | last post by:
What's wrong here? I can unpack an unsigned char (144,) I can unpack a short (6,) But an unsigned char & a short give me this
4
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
1
by: santrooper | last post by:
Hello all, I am trying to unpack big wav file (more than 10 mb), because i want to generate Visualization of an wav file, for smaller files (less than 2 mb) it works fine, but for bigger files it...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.