Note that there are some explanatory texts on larger screens.

plurals
  1. POPacking data of different sizes into a list of unsigned ints
    text
    copied!<p>I have a set of data that represents a hardware structure that I need to manipulate in python. The real structure is 4 KB in size...I'll just whip up a quick example:</p> <pre><code>Byte(s) Value 0-1 0x0102 2-3 0x0304 4-23 "AStrWith20Characters" 24-63 "WoahThisStringHas40CharactersItIsHuge!!!" 64-71 "Only8Chr" 72-74 0x050607 74 0x08 75-127 0x00 (padding) </code></pre> <p>The idea is I pack this structure up into a list of 32 bit Ints, pass the list off to another function, and <em>that</em> function then writes the whole shebang out to memory. The Memory Writing function accepts 64 Bytes at a time, so I'd have to make two calls.</p> <p>So, for the example above, I'd need to make these calls (I'll use big endian for readability):</p> <pre><code>WrToMemory([0x01020304, 0x41537472, 0x57697468, 0x3230436, 0x61726163, 0x74657273, 0x576F6168, 0x5468697, 0x53747269, 0x6E674861, 0x73343043, 0x6861726, 0x63746572, 0x73497449, 0x73487567, 0x65212121]) WrToMemory([0x4F6E6C79, 0x38436872, 0x05060708, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000]) </code></pre> <p>The problem is that I don't know how to pack this data in any kind of efficient way. I have been struggling with using <code>struct</code> and <code>array</code>, but either I am missing something conceptually or I am simply using the wrong tool for the job.</p> <p>Ultimately, I know I can write a big hairy function that checks data types, does a bunch of shifting and masking, converts ASCII to hex, and the constructs data packets I need. But that seems...inelegant. </p> <p>I figure there's got to be a way to tell python "This is a number that is two bytes long. Pack it up. Another two byte number, append it to the last one. Now this is a string, with 20 bytes. Append it. Go on until we have 64 bytes. Then convert the list of 64 bytes we just made into a lsit of 16 unsigned integers.</p> <p>It seems like the <code>pack</code> and <code>unpack</code> functions are what I should be using, but like I said above...I must be missing something. How does <code>pack</code> handle the 3 byte number, for example? If you use a format string like <code>HB</code>, then <code>pack</code> expects two arguments:</p> <pre><code>&gt;&gt;&gt; pack('HB', 0x050607) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; struct.error: pack requires exactly 2 arguments </code></pre> <p>And I can't use <code>unpack</code>, because that expects a string, not a number.</p> <p>Hope I've made my problem clear. Any help is much appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload