2

I have to translale some code into python. Both are to be 32 bits. How do I verify that?

  const char kEncryptionKey[] = {
      0xb0, 0x8c, 0x70, 0xcf, 0xbc, 0xb0, 0xeb, 0x6c, 0xab, 0x7e, 0x82, 0xc6,
      0xb7, 0x5d, 0xa5, 0x20, 0x72, 0xae, 0x62, 0xb2, 0xbf, 0x4b, 0x99, 0x0b,
      0xb8, 0x0a, 0x48, 0xd8, 0x14, 0x1e, 0xec, 0x07
  };
  const char kIntegrityKey[] = {
      0xbf, 0x77, 0xec, 0x55, 0xc3, 0x01, 0x30, 0xc1, 0xd8, 0xcd, 0x18, 0x62,
      0xed, 0x2a, 0x4c, 0xd2, 0xc7, 0x6a, 0xc3, 0x3b, 0xc0, 0xc4, 0xce, 0x8a,
      0x3d, 0x3b, 0xbd, 0x3a, 0xd5, 0x68, 0x77, 0x92
  };

So..in python:

kIntegrityKey = ????
kEncryptionKey = ????
3
  • 1
    Your question is related to how should you represent the hexadecimal array in Python, or are you concerned about how many bytes are going to be used to store your information? Commented Aug 27, 2012 at 0:43
  • Well...both..... i believe. Sorry, not an expert in this area. All I know is that I am given keys and the sample keys in c++ look like the above. Commented Aug 27, 2012 at 1:08
  • Humm, OK. I have updated my answer with a solution concerned with the size of each item. Commented Aug 27, 2012 at 1:10

3 Answers 3

6

You could store your hexadecimal numbers in a list:

kIntegrityKey = [0xbf, 0x77, 0xec, 0x55, 0xc3, 0x01, 0x30, 0xc1, 0xd8, 0xcd, 0x18, 0x62,
  0xed, 0x2a, 0x4c, 0xd2, 0xc7, 0x6a, 0xc3, 0x3b, 0xc0, 0xc4, 0xce, 0x8a,
  0x3d, 0x3b, 0xbd, 0x3a, 0xd5, 0x68, 0x77, 0x92]

and the same thing to kEncryptionKey.

Update: If you have concerns related to the size of your data, you could pack your data into an array. For example:

>>> import array
>>> kIntegrityKey = array.array('B', [0xbf, 0x77, 0xec, 0x55, 0xc3, 0x01, 0x30, 0xc1, 
... 0xd8, 0xcd, 0x18, 0x62, 0xed, 0x2a, 0x4c, 0xd2, 0xc7, 0x6a, 0xc3, 0x3b, 0xc0, 0xc4,   
... 0xce, 0x8a, 0x3d, 0x3b, 0xbd, 0x3a, 0xd5, 0x68, 0x77, 0x92])
>>> len(kIntegrityKey)
32
>>> kIntegrityKey.itemsize
1

The first argument to array ('B') indicates that we want to store the second parameter (your unsigned char list) using a list of unisgned char.

The itemsize attribute is described in the docstring as:

itemsize -- the length in bytes of one array item

Sign up to request clarification or add additional context in comments.

Comments

2

Python strings can be given arbitrary hex characters using the \x special character.

kIntegrityKey = "\xb0\x8c\x70\xcf ..."

1 Comment

This is only true in Python 2. In Python 3 a string is always Unicode and so you may get an error when using arbitrary binary data. Instead, you'd use the bytes type, which is explicitly an array of 8 bit values. You can use bytes literals too, by giving a "b" prefix, e.g. b"\xb0\x8c\x70\xcf ..."
2

Py3k has a "bytes" type which is likely ideal for this application.

NB: python 2 has a bytes type now too, but it's just an alias of str.

Comments

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.