1

What is the difference between "L", "LLL" ? I think in L, result converted to long and the result is 5 and in LLL, result converted to long and the result is 555?

import struct data = 5

  1. Result = struct.pack("L", data)

  2. Result = struct.pack("LLL", data, data, data)

I tried to see my self what these outputs are like by following code, but it just results in unreadable random charactors

import struct
data = 5

Result1 = struct.pack("L", data)

Result2 = struct.pack("LLL", data, data, data)

print (Result1)
print (Result2)
rrauenza
  • 6,285
  • 4
  • 32
  • 57
Pretty_Girl5
  • 49
  • 1
  • 1
  • 7

1 Answers1

0

What might be more revealing than the contents is the len of your results:

import struct
data = 5

Result1 = struct.pack("L", data)

Result2 = struct.pack("LLL", data, data, data)

print len(Result1)
print len(Result2)

Output:

$ python p.py 
8
24

Packing data is packing it into a binary format as a str. Each long is encoded as 8 bytes. When you encode 3 longs, you're getting 3*8=24 bytes.

The encodings are documented here. If you're using a 32 bit Python vs a 64 bit Python you may get different answers -- Windows vs. Linux may also give different results.

If you want to view the data as hex strings:

>>> map(hex, map(ord, struct.pack("L", 255)))
['0xff', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0']

or

>>> struct.pack("L", 255).encode('hex_codec')
'ff00000000000000'

or binary

>>> map(bin, map(ord, struct.pack("L", 255)))                                                             
['0b11111111', '0b0', '0b0', '0b0', '0b0', '0b0', '0b0', '0b0']
rrauenza
  • 6,285
  • 4
  • 32
  • 57
  • `@rrauenza` :ohh, great thanks, but the `len` result is different in my output it 4 and 12 in my output, and how to print the bits of those values? – Pretty_Girl5 Jun 20 '16 at 05:43
  • You're probably just using a 32bit python. – rrauenza Jun 20 '16 at 05:46
  • but, my operating system is 64bit win 10, so, I have installed the wrong python environment right .. – Pretty_Girl5 Jun 20 '16 at 05:48
  • Both are fine, and you might have the 64bit one installed. You can [check which is installed](http://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os) – rrauenza Jun 20 '16 at 05:56
  • @Pretty_Girl5 Windows64 has [32bit longs](http://stackoverflow.com/questions/384502/what-is-the-bit-size-of-long-on-64-bit-windows). – rrauenza Jun 20 '16 at 06:00
  • `@rrauenza` : `print struct.calcsize("P") * 8` I used this to check the outcome is 32, so what I'm using is a 32 bit version, I looked at python download site for a 64 bit version, couldn't find any 2.7.11 version here `https://www.python.org/downloads/release/python-2711/` – Pretty_Girl5 Jun 20 '16 at 06:05
  • @Pretty_Girl5 https://www.python.org/ftp/python/2.7.11/python-2.7.11.amd64.msi - "Windows x86-64 MSI installer" – rrauenza Jun 20 '16 at 06:08
  • `@rrauenza` it's for the AMD processors right, I use an Intel processor will that be ok? – Pretty_Girl5 Jun 20 '16 at 06:10
  • @Pretty_Girl5 [AMD invented 64bit extensions for x86 and then Intel played catchup.](https://en.wikipedia.org/wiki/X86-64) So it's often called AMD64. – rrauenza Jun 20 '16 at 06:11
  • """The encodings are documented here. If you're using a 32 bit Python vs a 64 bit Python you may get different answers -- Windows vs. Linux may also give different results.""" What's annoying is the Python documentation itself says L is 4 bytes, and makes no mention that it might be 8 bytes on 64-bit Linux. Does anyone know why Python doesn't fix its docs? – Danny S Aug 01 '16 at 02:58