1

I need to use an embedded system running Python 1.5.2+ (!!!) with very few modules. And there is no "struct" module usable... Here is the list of usable modules :

marshal
imp
_main_
_builtin_
sys
md5
binascii

Yes that's it, no struct module...

So, I need to create a 4 bytes representation of an unsigned short integer to send to serial...

With struct :

date = day + month * 32 + (year - 2000) * 512
time = 100 * hour + minute
data = struct.pack(b'HH', date, time)

date on 2 bytes time on 2 bytes and everybody's happy...

But without using 'struct' module, how can I do that?

10
  • I.. don't see how you could do that without at least ctypes available. Note that strcut itself is implemented in c for exactly that reason. If you can't write a c module yourself you're out of luck I'd think. Commented Jun 27, 2012 at 17:50
  • 1
    @Voo: That's perfectly doable with plain Python Commented Jun 27, 2012 at 17:50
  • @Niklas TJDs answer creates a PyObject* though, how do you get the pointer to the start of the actual data? Commented Jun 27, 2012 at 17:51
  • @Voo: It creates a byte string, exactly like struct would Commented Jun 27, 2012 at 17:52
  • @Voo, no my answer creates a string with binary data in it, just like struct.pack Commented Jun 27, 2012 at 17:52

3 Answers 3

5

You can do something like this:

x = 0xabcd

packed_string = chr((x & 0xff00) >> 8) + chr(x & 0x00ff)
Sign up to request clarification or add additional context in comments.

4 Comments

I think example code that would convert date/time to the specified output string would be more beneficial to OP.
@SeanJohnson, I don't intend to write code for OP. The crux of the question is 'how do I replace struct.pack'? You can easily use my mechanism to implement your own struct.pack and use that for whatever purposes you may have.
@NiklasB. Only because its highly portable(to any language) and relies on no modules other than __builtin__.chr that has been around a very long time(forever?) ... bytes(x) is good but wont work for OP... on a side note your solution was good too... just less general
@Joran: I don't know what you're even talking about. lists, map and join has been around for at least as long as chr (in fact they have both been part of Python 1.0). Care to explain what you mean by "less general"? In fact I like to think of higher-order operations on being more general. Also, did you just imply that "correct" == "general" == "portable"?
1

Here is a complete translation for you

Before

>>> import struct
>>> day = 1; month = 2; year = 2003
>>> hour = 4; minute = 5
>>> date = day + month * 32 + (year - 2000) * 512
>>> time = 100 * hour + minute
>>> data = struct.pack(b'HH', date, time)
>>> data
'A\x06\x95\x01'
>>> data.encode("hex")
'41069501'

And after

>>> data2 = chr(date & 0xFF) + chr((date >> 8) & 0xFF) + chr(time & 0xFF) + chr((time >> 8) & 0xFF)
>>> data2
'A\x06\x95\x01'
>>> data2.encode("hex")
'41069501'
>>>

1 Comment

Thanks a lot fot this solution too !
0

I was able to do it by passing a list of the bytes to bytes():

data=bytes([date%256,date//256,time%256,time//256])

2 Comments

bytes is a recent addition to builtins, not applicable to this question
@TJD: The idea of creating a list of bytes is more elegant than manual bit-fiddling, though. The joining is more or less cosmetics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.