Python 2.4.3 Converting String to Bits and Vice-versa - python

How can i convert Hex Digits to Binary and then perform "OR" operation in Python 2.4.3 ?
Example-
a= '1000'
b= '2000'
//Now convert both numbers a and b to binary form
//a_inbinary= '0001000000000000'
//b_inbinary= '0010000000000000'
c= a_inbinary | b_inbinary // or of a and b
//c would be result example - 0011000000000000
result=3000 //c in hex
Can someone please tell me that how i can convert two numbers to binary form and then result into hexadecimal form ?

You dont have to convert them to binary form to do the bitwise operations, but decimal integers should be fine. You can convert the numbers to base 10 by specifying the actual base, in which the numbers are represented, as the second parameter to int function. Finally, the result which is in decimal form can be converted back to hexa decimal using, hex function.
a= '1000'
b= '2000'
print hex(int(a, 16) | int(b, 16))
Output
0x3000

Assuming the first two strings are 0x1000 and 0x2000 there is no need to convert to a binary string, just use the bitwise or operator |
a='1000'
b='2000'
a=int(a,16)
b=int(b,16)
hex(a|b)

Related

python how convert a binary string to a binary number [duplicate]

I can't quite find a solution for this.
Basically what I've done so far is created a string which represents the binary version of x amount of characters padded to show all 8 bits.
E.g. if x = 2 then I have 0101100110010001 so 8 digits in total. Now I have 2 strings of the same length which I want to XOR together, but python keeps thinking it's a string instead of binary. If I use bin() then it throws a wobbly thinking it's a string which it is. So if I cast to an int it then removes the leading 0's.
So I've already got the binary representation of what I'm after, I just need to let python know it's binary, any suggestions?
The current function I'm using to create my binary string is here
for i in origAsci:
origBin = origBin + '{0:08b}'.format(i)
Thanks in advance!
Use Python's int() function to convert the string to an integer. Use 2 for the base parameter since binary uses base 2:
binary_str = '10010110' # Binary string
num = int(binary_str, 2)
# Output: 150
Next, use the bin() function to convert the integer to binary:
binary_num = bin(num)
# Output: 0b10010110

Convert 32 bit binary to a decimal in python

how do I convert Convert 32 bit binary to a decimal in python
this
00011110001101110110110000001000
to
506948616
this
use in-built function int():
a = '00011110001101110110110000001000'
a_dec = int(a, 2)
use int for conversion of a string (just give the correct base as parameter):
int('00011110001101110110110000001000', 2)
Another way is to add 0b as a prefix for the number (same as 0x for hex values or 0o for octal values):
x=0b00011110001101110110110000001000
x will be an integer with decimal value of 506948616

Binary conversion and XOR in python

I am doing an assignment to decipher a one-time-pad code (7 sentences, repeated keys for each character position among all 7 sentences). I'm solving it by guess work and I need to XOR the binary value of my guess letter with the binary value of the cypher character in order to get a key.
However, I cannot XOR the binary values returned by Python as they are in string format. I cannot convert them to integers since I need the '0b' part, but I also cannot XOR it because it's a string.
Any suggestions as to how to work around this?
Integers in Python support binary bitwise operations; the binary bitwise operators take integer operands and produce new integers with the bits altered, just like they would in C code.
Convert your string (presumably you have something like 0b1001101) to integer, use the ^ XOR operator on that. If you need string output at the end, you can always use bin() again on the integer:
>>> bin(102)
'0b1100110'
>>> 102 ^ 255
153
>>> bin(102 ^ 255)
'0b10011001'
If you have ASCII bytes (characters in Python 2 strings are bytes), use ord() to get an integer representation, chr() to go back to a byte (character) again.

python adding binary number

There is an unexpected output when I am dealing with binary number in Python 3.
We can easily convert any integer to binary by built-in bin() function. For example:
>>>bin(4243125)
Here's the issue when I try to add 2 binary function:
>>>bin(x)+bin(y)
The output is concatenation of two binary number, not addition of binary number. The output of binary function has become a string.
Addition in a binary function works fine:
>>>bin(x+y)
And try to add two binary number without bin() is viable too:
>>>0b100+0b10111
What is the reasons/purposes of setting a bin() output to a string?
bin, like hex, converts a decimal to a string literal representing the number in that base.
If you want to add 2 numbers together simply do so:
x = 10
y = 2
x + y
If you want to take binary strings as input and add them together convert them back from string literals with int base 2, like this:
x = bin(10)
y = bin(2)
int(x, 2) + int(y, 2)
If you're looking to do bitwise operations look at the Python bitwise operators:
https://wiki.python.org/moin/BitwiseOperators

How to convert floating point number in python?

How to convert floating point number to base-16 numbers, 8 hexadecimal digits per 32-bit FLP number in python?
eg : input = 1.2717441261e+20 output wanted : 3403244E
If you want the byte values of the IEEE-754 representation, the struct module can do this:
>>> import struct
>>> f = 1.2717441261e+20
>>> struct.pack('f', f)
'\xc9\x9c\xdc`'
This is a string version of the bytes, which can then be converted into a string representation of the hex values:
>>> struct.pack('f', f).encode('hex')
'c99cdc60'
And, if you want it as a hex integer, parse it as such:
>>> s = struct.pack('f', f).encode('hex')
>>> int(s, 16)
3382500448
To display the integer as hex:
>>> hex(int(s, 16))
'0xc99cdc60'
Note that this does not match the hex value in your question -- if your value is the correct one you want, please update the question to say how it is derived.
There are several possible ways to do so, but none of them leads to the result you wanted.
You can code this float value into its IEEE binary representation. This leads indeed to a 32 bit number (if you do it with single precision). But it leads to different results, no matter which endianness I suppose:
import struct
struct.pack("<f", 1.2717441261e+20).encode("hex")
# -> 'c99cdc60'
struct.pack(">f", 1.2717441261e+20).encode("hex")
# -> '60dc9cc9'
struct.unpack("<f", "3403244E".decode("hex"))
# -> (687918336.0,)
struct.unpack(">f", "3403244E".decode("hex"))
# -> (1.2213533295835077e-07,)
As the other one didn't fit result-wise, I'll take the other answers and include them here:
float.hex(1.2717441261e+20)
# -> '0x1.b939919e12808p+66'
Has nothing to do with 3403244E as well, so maybe you want to clarify what exactly you mean.
There are surely other ways to do this conversation, but unless you specify which method you want, no one is likely to be able to help you.
There is something wrong with your expected output :
import struct
input = 1.2717441261e+20
buf = struct.pack(">f", input)
print ''.join("%x" % ord(c) for c in struct.unpack(">4c", buf) )
Output :
60dc9cc9
Try float.hex(input) if input is already a float.
Try float.hex(input). This should convert a number into a string representing the number in base 16, and works with floats, unlike hex(). The string will begin with 0x however, and will contain 13 digits after the decimal point, so I can't help you with the 8 digits part.
Source: http://docs.python.org/2/library/stdtypes.html#float.hex

Categories