# [Tutor] revisiting struct module

Luke Paireepinart rabidpoobear at gmail.com
Fri Sep 29 04:49:08 CEST 2006

```shawn bright wrote:
> Luke !
>
!!!!
> That worked !
> Man, if you knew how i have pulled my hair out over this for a while.
Well, I don't know how your experience in particular has been, but I
know I've had moments like that quite often.
That's what the tutor list is for :)
> I did not wind up using the struct at all. I really thought that I was
> supposed to, but once i made the message with ord. Like
> ord(0)+ord(0)+ord(0)+ord(200)... it worked.
do you mean chr?
ord and chr do opposite things...
so ord('a') will be 96 or something,
whereas chr(96) will be 'a'
Anyway, yeah, this is an easier way to do it if you have short strings.
I tried to make an ID3v1.1 tag editor (tags on mp3 files that tell you
artist info, etc)
without using struct and it was fairly long code.
If I'd just used struct it would've been a few lines.
> So i guess this means that it doesn't matter if its big endian or not ?
hmm...
well, endianness is only taken into account if you have things that are
more than one byte long.
For example,
the integer 2882400018 base10 is ABCDEF12 base16.
If you were going to store this in a little-endian system (smallest first)
it would be stored as
12-EF-CD-AB
whereas on a big-endian (biggest first :D )
it would be stored as
AB-CD-EF-12
Which is the way that numbers are usually written (in the United States,
at least)

Keep in mind that 2 hex digits is 1 byte, because 16^2(2 digits in base
16) == 255(1 digit in base 256 ;))== 2^8(8 digits in binary)
and you know 8 bits is a byte (usually) and 1 bit is a single binary digit.

So don't think that it'd be stored as
21FEDCBA on a little-endian, because every group-of-two hex digits is a
single byte.
you can't just do 'int(str(number).reverse())' or something :)

However, since you're using
chr(0)
which is hex 00

and
chr(200)
which is hex C8

well, they're both one-digit long.
so
00-00-00-C8
would be chr(0)+chr(0)+chr(0)+chr(200) on both little- and big-endian
storage systems.

So yes, the endianness does matter if you're not storing single-byte items.
you should probably learn how to use struct if you need this functionality.

To make this clearer, if it's still confusing, suppose you needed to
store a long unsigned integer
at the end of this 4-byte string we have.
say it's the number AB.

On big-endian, this would be stored as (including your previous data)
00-00-00-C8-00-00-00-AB

On little-endian, it would be
00-00-00-C8-AB-00-00-00

See, it's not the overall string that has a particular endianness, but
actually the specific items you're storing in it.
That's why you'd want to use struct.pack().  If you decided you needed
to change endianness for something,
you wouldn't have to change your code very much at all.  Just change the
> to a <
in the format string you pass to struct.pack() and it'll handle the rest.
>
> anyway, thanks for lots of help, i would have replied sooner, but i
> was tinkering.
Sure!
I wouldn't want you to stop tinkering just for politeness.
we programmers need as few distractions as possible, right?
;)

Well, glad that helped, hope that this extra info clears up any
confusion you may have.
P.S. --- What are you making? :)
-Luke
```

More information about the Tutor mailing list