0

I have a question considering python3. I am already trying for a few days to create a byte object, which looks like these:

b"'\x10\x00\x0020180425"

The first part '\x10\x00\x00 are two int16 objects which were combined. This is how I tried it to combine them:

array = [0,0,0,0,1,9,7,0,0,1,0,1]
blebytes = bytearray(array)

z = np.int16(20)
blebytes[0] = (z & 0xFF00) >> 8
blebytes[1] = (z & 0x00FF)

z = np.int16(123)
blebytes[2] = (z & 0xFF00) >> 8
blebytes[3] = (z & 0x00FF)

The second part(20180425) is just the date of today. I am trying to do this like this:

datestr = time.strftime("%Y%m%d")
for i in range(0,8):
    blebytes[i+4] = np.int16(datestr[i])
print(blebytes)

But if I print my blebytes array, it looks like this:

bytearray(b'\x04\xe8\t\xf7\x02\x00\x01\x08\x00\x04\x02\x05')

What am I doing wrong? Can someone help me?

Simon K
  • 28
  • 8
  • Use `ord(datestr[i])` in the loop. This will give you the ascii ordinals of the string characters instead of the byte values set to those numbers, which is what you want. In short: `ord("2") == 50 != 2`. – Jeronimo Apr 25 '18 at 11:14
  • Thanks, but it is still not the same.. Here is how it looks right now: `bytearray(b'\x00\x14\x00{20180425')` – Simon K Apr 25 '18 at 11:34
  • Are you sure the original encoded values were `20` and `123`? They don't seem to be related to `'\x10\x00\x00`... – Jeronimo Apr 25 '18 at 13:11
  • Oh sorry, that was my fault, didn't mentioned it. No the 20 and the 123 are just two random numbers for testing. It should work with every two numbers of the type int16. – Simon K Apr 25 '18 at 13:23
  • Ok so for your 20 and 123 I get `b'\x00\x14\x00{'` as the first part. What would you like to see here instead? – Jeronimo Apr 25 '18 at 14:25
  • For the z values i choosed now: `z1 = -14113 and z2 = 18402` Therefore, the constructed byte type looks like this: `b'\xc8\xdfG\xe220180425'` But it still looks slightly different to the original: `b"'\xc8\xdfG\xe220180425"` Actually only this `"` is missing at the beginning.. – Simon K Apr 25 '18 at 14:26
  • So it's just about which numbers they were? You can use `int.from_bytes(b"'\x10", "big", signed=True)` to find those. Beware, I only guessed byteorder and signed. – Jeronimo Apr 25 '18 at 14:34
  • Okay, the two int 16 types have to be big Endian. If I understood it right, the `\x10` is for the first number, the `\x00` is for the second number and the rest `\x0020180425` is for the date. How can I combine it all together to this kind: `b"'\x10\x00\x0020180425"`? So now my Problem is to convert the entries blebytes[0] + blebytes[1] into one big endian byte and the blebytes[2] + blebytes[3] into the other one. – Simon K Apr 26 '18 at 08:50
  • The first number is `b"'\x10"` - the apostrophe is also a byte, just replaced by its ascii representation, and the second number is `b"\x00\x00"`. So 16 bit each. Assuming big-endian, those two numbers were 10000 and 0. Using these, you should get the exact bytearray as above with the code you already have. – Jeronimo Apr 26 '18 at 09:43

0 Answers0