I already have a list that contain some string (as output of other process) as,(example)
BitStream = ['011000111001', '100100111001', '100101100110', ...]
Now i need to get the actual value as binary, not as string
But when i try to change it to binary with
for bit in BitStream:
BitInteger = bin(bit)
But it give me error message as str object cannot be interpreted as an index
But when i try to make change it as int first, and i change it to binary,
for bit in BitStream:
BitInteger = int(bit)
BitIntegerBin = bin(BitInteger)
The binary value is not the actual value in the string. But the value of actual binary that treated as integer that changed to binary.
How do i get the actual value?
Its not clear but what i understand:
>>> a
'011000111001'
>>> bin(int(a,2))
'0b11000111001' # python valid binary format
>>> int(a,2) # integer
1593
for your code integer and binary:
>>> BitStream = ['011000111001', '100100111001', '100101100110']
>>> [ [int(x,2),bin(int(x,2))] for x in BitStream ]
[[1593, '0b11000111001'], [2361, '0b100100111001'], [2406, '0b100101100110']]
Related
When I load xlsx file. I have a number IBAN with scientific notation.
For example:
7.810500161524e+25
This number should be:
78105001615240000000000000
I want to convert this number to string, but I get result:
'7.810500161524e+25'
I thought if I converted to int and then to string it would be the correct result, but again I get wrong result:
'78105001615239996483043328'
Anyone have any idea?
You can convert your string '7.810500161524e+25' to a decimal.Decimal number without altering its precision at all the way you will if you convert it to a floating point value. Then (if necessary) you can convert the decimal value to an integer or a string.
import decimal
scientific_notation = '7.810500161524e+25'
decimal_number = decimal.Decimal(scientific_notation) # Decimal('7.810500161524E+25')
int_number = int(decimal_number) # 78105001615240000000000000
fixed_point_string = format(decimal_number, 'f') # '78105001615240000000000000'
If I typed this code the output will be : b'd'
a = bytes([100])
print(type(a))
print(a)
I know that d in hex is number 100 and it seems like python print show me that the data are of type binary due to the letter be in the output so why is the result of when i store decimal number 100 as binary in variable which will be 1100100
so
a = 1100100 #in binary
and type(a) are also return binary type
so what is the reason for print function to print as binary ( b ) letter at start and at the same time represent number by its ASCII hex representation which is d
or is it because stored number is hex or what exactly is going on cause its so confusing for me right
now so if someone can answer these questions
how is variable a represented in the meomery now
why print show this weird behavior at least for me
and is variable a hex or a binary number
The b letter is short for bytes. So its neither a hex or a binary number, it's a list of bytes. The default __str__ function of bytes, which is used when you call print(a) tries to decode the bytes to string, that's why you're seeing the letter d.
I want to convert a hex string I read from a file
"0xbffffe43" to a value written in little endian "\x43\xfe\xff\xbf".
I've tried using struct.pack but it requires a valid integer. Everytime I try to cast hex functions it will convert the 43. I need this for an assignment around memory exploits.
I have access to python 2.7
a = "0xbffffe43"
...
out = "\x43\xfe\xff\xbf"
Is what I want to achieve
You have a string in input. You can convert it to an integer using int and a base.
>>> a = "0xbffffe43"
>>> import struct
>>> out = struct.pack("<I",int(a,16))
>>> out
b'C\xfe\xff\xbf'
The b prefix is there because solution was tested with python 3. But it works as python 2 as well.
C is printed like this because python interprets printable characters. But
>>> b'C\xfe\xff\xbf' == b'\x43\xfe\xff\xbf'
True
see:
Convert hex string to int in Python
Convert a Python int into a big-endian string of bytes
You can try doing:
my_hex = 0xbffffe43
my_little_endian = my_hex.to_bytes(4, 'little')
print(my_little_endian)
In Python 3.3 I need to convert an integer into the middle of three bytes to send it over a serial connection.
That is, I need to have a value of: b'\x4c\x00\x46', except that the \x00 byte will need to take the single-byte value of an integer variable that may vary from 0 to 255. I thought chr(value) would work, but that gives a string rather than a byte.
For example, if value is 255, I want to get b'\x4c\xff\x46'.
Using bytearray:
>>> b'\x4c\x00\x46'
b'L\x00F'
>>> a = bytearray(b'\x4c\x00\x46')
>>> a[1] = 255
>>> a
bytearray(b'L\xffF')
>>> bytes(a)
b'L\xffF'
You can also use list in place of bytearray. But using list does not work in Python 2.x.
I have a script that calls a function that takes a hexadecimal number for an argument. The argument needs to the 0x prefix. The data source is a database table and is stored as a string, so it is returned '0x77'. I am looking for a way to take the string from the database and use it as an argument in hex form with the 0x prefix.
This works:
addr = 0x77
value = class.function(addr)
The database entry has to be a string, as most of the other records do not have hexadecimal values in this column, but the values could be changed to make it easier, so instead of '0x77', it could be '119'.
Your class.function expects an integer which can be represented either by a decimal or a hexadecimal literal, so that these two calls are completely equivalent:
class.function(0x77)
class.function(119) # 0x77 == 119
Even print(0x77) will show 119 (because decimal is the default representation).
So, we should rather be talking about converting a string representation to integer. The string can be a hexadecimal representation, like '0x77', then parse it with the base parameter:
>>> int('0x77', 16)
119
or a decimal one, then parse it as int('119').
Still, storing integer whenever you deal with integers is better.
EDIT: as #gnibbler suggested, you can parse as int(x, 0), which handles both formats.
>>> hex(119)
'0x77'
#or:
>>> hex(int("119"))
'0x77'
This should work for you.
You can also get the hex representation of characters:
>>> hex(ord("a"))
'0x61'
I think you're saying that you read a string from the database and you want to convert it to an integer, if the string has the 0x prefix you can convert it like so:
>>> print int("0x77", 16)
119
If it doesnt:
>>> print int("119")
119