This question already has answers here:
Writing binary files in Python 3, why I'm not getting the hexadecimal representation of 9,10 and 13?
(1 answer)
Convert 10 to bytes, strange result
(1 answer)
Closed 2 years ago.
I am using bytes in python to convert following integers into bytes:
>>> b = bytes([192, 168, 10, 254])
I get the following output:
b'\xc0\xa8\n\xfe'
My question, why does the int 10 is represented as \n and not hex value of '0xa'.
Looking at the output, 192, 168 and 254 all gets represented as their equilvalent hex values.
Why not 10 is represented by its hex value.
Thanks
Related
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 5 months ago.
I have an input a=int(1010). I want to find integer equivalence of binary 1010. If I use bin(a) then output will be 1111110010, but I want to get 10.
You need to tell python, that your integer input is in binary. You can either parse a string with a defined base, or ad a 0b-prefix to your code constants.
a = int("1010", base=2)
a = 0b1010
print(a) # result: 10
print(bin(a)) # result: 1010
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Python int to binary string?
(36 answers)
Closed 1 year ago.
eight character (zero and one) string -> turn it into 8 bit binary number -> perform binary operation -> turn back into 8 0/1 string
>>> the_entry = "00000000"
>>> the_binary number = 8CharacterStringIntoThe8CharacterBinaryNumber(the_entry)
>>> the_binary_number << 1 # any binary operation
>>> the_result_string = EightCharacterBinaryNumberIntoEightCharacterString(the_binary_number)
this is soo simple, I refuse to write my own method, but obviously, in th 180000 possible results, I cannot find the one related to my question.
Do you hold the magic??
These are simple basic operations:
entry = "00000000"
number = int(entry, 2)
result = f"{number:08b}"
This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 4 years ago.
So i have a function that converts an alphabet character to it's binary.
def toBinary(char):
return "".join([format(ord(char), '#010b')[2:]])
For example, toBinary('a') gives me
01100001
How do i convert 01100001 back to the ascii 'a'?
One way could be
c = chr(int(s, 2))
where s is the binary string.
Try This:
chr(int('01100001',2))
This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 6 years ago.
x = "01100001"
How do i convert this string into ASCII
Expected Result:
print(x.somefunction())
Output: a
Use following one liner
x = chr(int('010101010',2))
will work.
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 9 years ago.
I want to convert a string consists of binary digits and convert that string into a binary number.
suppose, my input is "01001", this is in string format, i want to convert this to binary number format to perform various bit wise operations on that.
You can convert the number to an int and do bitwise operations, like this
my_int = int("01001", 2)
print my_int & 1 # 1
print my_int & 8 # 8
print my_int & 16 # 0