Question: How can I find out the number of Bytes a certain integer number takes up to store?

E.g. for

* hexadecimal `\x00` - `\xff` (or decimal `0` - `255` = binary `0000 0000` - `1111 1111`) I'm looking to get 1 (Byte),
* hexadecimal `\x100` - `\xffff` (or decimal `256` - `65535` = binary `0000 0001 0000 0000` - `1111 1111 1111 1111`) would give me 2 (Bytes)

and so on.

Any clue for hexadecimal or decimal format as the input?

Answer: 
By using a simple biwise operation to move all the used bits over 1 byte each time you can see how many bytes are needed to store a number.

It's probably worth noting that while this method is very generic, it will not work on negative numbers and only looks at the binary of the variable without taking into account what it is stored in.

```
a = 256
i = 0

while(a > 0):
    a = a >> 8;
    i += 1;

print (i)

```

The program behaves as follows:

a is `0000 0001 0000 0000` in binary
each run of the loop will shift this to the left by 8:

```
loop 1:
0000 0001 >> 0000 0000
0000 0001 > 0 (1 > 0)

loop 2:

0000 0000 >> 0000 0001
0000 0000 > 0 (0 > 0)

END 0 is not > 0

```

so there are 2 bytes needed to store the number.
```
# Python 3

import math

nbr = 0xff                 # 255 defined in hexadecimal
nbr = "{0:b}".format(nbr)    # Transform the number into a string formated as bytes.

bit_length = len(nbr)      # Number of characters
byte_length = math.ceil( bit_length/8 ) # Get minimum number of bytes

```