1

I have two questions and they both are inter-related.

My first question is: As for example, I have to declare few U8 variables, U16 and U48 and then have to pass the values to them. After passing the values, I have to covert them into a binary (one binary-all of them combined). I don't know how to perform this task How to define U8, U16 and U32 in python(in binary). I know struct as a module. But when I write

ex=binascii.hexlify(struct.pack("I",1))

it returns b'01000000' instead of returning 01000000. Is there any other way to do it(I need it in U8 format)

My second question is:

How can disintegrate U8 into 2MSbits and 6LSbits

  • 2
    This is the correct output. `b` stands for bytes. It's not a part of the string. – simonzack Nov 26 '15 at 16:18
  • What do mean by defining U8 in python? Python natively doesn't have a `byte` type like c/java, there is `array` `ctypes` and `numpy` but it helps to know what your goal is. – simonzack Nov 26 '15 at 16:21
  • 1
    [Need help-typecasting in Python](http://stackoverflow.com/questions/385572/need-help-typecasting-in-python) is this the same thing too –  Nov 26 '15 at 16:26
  • @simonzack Can I disintegrate U8 into MSB and LSB. is it possible? –  Nov 26 '15 at 16:27
  • Could you please elaborate more about your goal? I don't think anyone else except you knows what you're asking about. – Michał Fita Nov 26 '15 at 16:40
  • @MichałF I have edited my question –  Nov 26 '15 at 18:08

2 Answers2

3

I have to declare few U8 variables, U16 and U48 and then have to pass the values to them.

Python is a dynamic-typed language to a variable doesn't have a fixed type, the type of your value will depend on the value it holds. That said, in python there is a single integer type (int), and it can hold any signed integer number, if your number is to big it will be automatically converted to a bigint.

E.g.

var1 = 256  # A plain integer
var2 = 4294967296L # Bigger than 2**31, it has to be prefixed with an L

Since there is a single integer type, if you want to limit your variable to hold a given binary width, you should mask the LSBits.

E.g.

bignum = 2**50 - 1
only_8_lsb = bignum & 0xFF
only_16_lsb = bignum & 0xFFFF
only_48_lsb = bignum & (2**49 - 1)

After passing the values, I have to covert them into a binary (one binary-all of them combined).

Technically, the numbers are already stored as binary. If you want to show the binary representation of a number, you'll have to hold it in a string, and you can use formatting

E.g. Converting a number to a zero-paded 8-digits binary representation

> "{0:08b}".format(0b11000011)
'11000011'

E.g. Converting a number to a zero-paded 16-digits binary representation

> "{0:016b}".format(0xCAFE)
'1100101011111110'

Hopefully, you noticed that a you can specify an integer in different ways.

How can disintegrate U8 into 2MSbits and 6LSbits

Unless you need other representation, use bit operators as noted in the other answer.

struct has other purpose, it allows you to convert some python values to a string of bytes, as would other languages will see them in memory. And you are getting a b prefixed string, because in Python 3 str is meant to store unicode code points while bytes are meant to store raw bytes (sequences of "numbers" in the range 0 <= chr > 256).

memoselyk
  • 3,993
  • 1
  • 17
  • 28
  • So that means I don't have to use `struct` –  Nov 26 '15 at 22:41
  • Using `struct` to convert a number to binary will be a two process step. `struct` will only give you a raw string of bytes (e.g. `struct.pack("I", 0xA1B2C3D4)` => `'\xd4\xc3\xb2\xa1'`) and you'll need to post-process it to be a binary representation. The only benefit of `struct` I can think of is to get the bytes in a specifc [endianess](https://en.wikipedia.org/wiki/Endianness). – memoselyk Nov 27 '15 at 00:10
1

It's unclear what you want, but you can extract the MSB and LSB using bit operations.

For a byte:

  • 2 MSBs: x >> 6
  • 6 LSBs: x & ((1 << 6) - 1)
simonzack
  • 19,729
  • 13
  • 73
  • 118