Trying to understand binary conversion script - python

I have the following python code, which converts a binary string into plain-text:
import bin ascii
n = int('01100001011000100110001101100100', 2)
binascii.unhexlify('%x' % n)
This code functions properly, but I don't understand what's going on here.
What is the purpose of the "2" at the end of the int declaration?
What is the purpose of the "'%x' % n" argument?

What is the purpose of the 2 at the end of the int declaration?
According to the documentation, int can take a second argument: the base of the first argument.
What is the purpose of the '%x' % n argument?
%x in a string indicates that an item will be formatted to hexadecimal with lowercase letters. '%x' % n. It is similar to the builtin hex function, except it doesn't have the leading 0x in the string. An equivalent expression would be format(n, 'x').

Related

what does % mean in python when writing a string

So I was looking at a python tutorial, and in a statement, it used %.
print ("Total Employee %d" % Employee.empCount)
Can someone please take their time and describe to me what that means. I'm pretty sure that it doesn't signify a division.
Python uses C-style string formatting to create new, formatted
strings. The "%" operator is used to format a set of variables
enclosed in a "tuple" (a fixed size list), together with a format
string, which contains normal text together with "argument
specifiers", special symbols like "%s" and "%d".
the % sign in this case is supposed to be used for string formatting. This is an old technique and can be used with and f-string now. % is mostly used in java and not python.
% Employee.empCount is a variable and %d for print integer variable.That's mean value of variable % Employee.empCount print in place of %d.
% sign use for give reference of variable.
Python Program for
Old Style Formatting
of Integers also used %
Integer1 = 12.3456789
print("Formatting in 3.2f format: ")
print('The value of Integer1 is %3.2f' %Integer1)
print("\nFormatting in 3.4f format: ")
print('The value of Integer1 is %3.4f' %Integer1)
Output:
Formatting in 3.2f format:
The value of Integer1 is 12.35
Formatting in 3.4f format:
The value of Integer1 is 12.3457
also use as
print("writing integer in a string: %s" %Integer1)
output
writing integer in a string: 12.3456789

Converting string to binary then xor binary

So I am trying to convert a string to binary then xor the binary by using the following methods
def string_to_binary(s):
return ' '.join(map(bin,bytearray(s,encoding='utf-8')))
def xor_bin(a,b):
return int(a,2) ^ int(b,2)
When I try and run the xor_bin function I get the following error:
Exception has occurred: exceptions.ValueError
invalid literal for int() with base 2: '0b1100010 0b1111001 0b1100101 0b1100101 0b1100101'
I can't see what's wrong here.
bin is bad here; it doesn't pad out to eight digits (so you'll lose data alignment whenever the high bit is a 0 and misinterpret all bits to the left of that loss as being lower magnitude than they should be), and it adds a 0b prefix that you don't want. str.format can fix both issues, by zero padding and omitting the 0b prefix (I also removed the space in the joiner string, since you don't want spaces in the result):
def string_to_binary(s):
return ''.join(map('{:08b}'.format, bytearray(s, encoding='utf-8')))
With that, string_to_binary('byeee') gets you '0110001001111001011001010110010101100101' which is what you want, as opposed to '0b1100010 0b1111001 0b1100101 0b1100101 0b1100101' which is obviously not a (single) valid base-2 integer.
Your question is unclear because you don't show how the two functions you defined where being used when the error occurred — therefore this answer is a guess.
You can convert a binary string representation of an integer into a Python int, (which are stored internally as binary values) by simply using passing it to the int() function — as you're doing in the xor_bin() function. Once you have two int values, you can xor them "in binary" by simply using the ^ operator — which again, you seem to know.
This means means to xor the binary string representations of two integers and convert the result back into a binary string representation could be done like this you one of your functions just as it is. Here's what I mean:
def xor_bin(a, b):
return int(a, 2) ^ int(b, 2)
s1 = '0b11000101111001110010111001011100101'
s2 = '0b00000000000000000000000000001111111'
# ---------------------------------------
# '0b11000101111001110010111001010011010' expected result of xoring them
result = xor_bin(s1, s2)
print bin(result) # -> 0b11000101111001110010111001010011010

Convert Hexadecimal string to long python

I want to get the value of 99997 in big endian which is (2642804992) and then return the answer as a long value
here is my code in python:
v = 99997
ttm = pack('>i', v) # change the integer to big endian form
print ("%x"), {ttm}
r = long(ttm, 16) # convert to long (ERROR)
return r
Output: %x set(['\x00\x01\x86\x9d'])
Error: invalid literal for long() with base 16: '\x00\x01\x86\x9d'
As the string is already in hex form why isn't it converting to a long? How would I remove this error and what is the solution to this problem.
pack will return a string representation of the data you provide.
The string representation is different than a base 16 of a long number. Notice the \x before each number.
Edit:
try this
ttm = pack('>I',v)
final, = unpack('<I',ttm)
print ttm
Notice the use of I, this so the number is treated as an unsigned value
You have to use struct.unpack as a reverse operation to struct.pack.
r, = unpack('<i', ttm)
this will r set to -1652162304.
You just converted the integer value to big endian binary bytes.
This is useful mostly to embed in messages addressed to big-endian machines (PowerPC, M68K,...)
Converting to long like this means parsing the ttm string which should be 0x1869D as ASCII.
(and the print statement does not work either BTW)
If I just follow your question title: "Convert hexadecimal string to long":
just use long("0x1869D",16). No need to serialize it.
(BTW long only works in python 2. In python 3, you would have to use int since all numbers are represented in the long form)
Well, I'm answering to explain why it's bound to fail, but I'll edit my answer when I really know what you want to do.
This is a nice question.
Here is what you are looking for.
s = str(ttm)
for ch in r"\bx'":
s = s.replace(ch, '')
print(int(s, 16))
The problem is that ttm is similar to a string in some aspects. This is what is looks like: b'\x00\x01\x86\x9d'. Supplying it to int (or long) keeps all the non-hex characters. I removed them and then it worked.
After removing the non-hex-digit chars, you are left with 0001869d which is indeed 99997
Comment I tried it on Python 3. But on Python 2 it will be almost the same, you won't have the b attached to the string, but otherwise it's the same thing.

How to convert octal to string without change the value?

How to convert 00024 to '00024' with python?
If I run 00024 in python shell, it print 20 as result. I want to convert 00024 to string, with the result is '00024'. Any suggestions?
If you want to represent integers with leading zeros, format() the number to a string explicitly:
format(integer, '05d')
Whenever you output values, print implicitly converts it to a string with str(), with format() you get to control how that conversion takes place explicitly.
When you echo a number in the interactive interpreter, repr() is used instead, but for integers the output is exactly the same as when you use str().
Demo:
>>> format(24, '05d')
'00024'
When you enter 00024 as an integer literal, Python 2 parses that as an octal number, in Python 3 that's a syntax error.
If you want to interpret such a number in the Python 2 shell as if it was a string, you cannot do what you want, not with an arbitrary number of leading zeros. You can, at best, re-format the resulting integer as octal, again with leading zeros. That'll produce a string again, but you have to hardcode the number of leading zeros:
>>> number = 00024
>>> format(number, '05o')
'00024'
Note that this'll also fail as soon as you have a number that doesn't have a leading zero; you could auto-detect such values if they are greater than 4095 (07777 octal):
strnumber = format(number, '5d' if number > 0o07777 else '05o')
and any number with leading zero and the digits 8 or 9 simply fails with a syntax error:
>>> 09876
File "<stdin>", line 1
09876
^
SyntaxError: invalid token
Your better bet is to just enter such numbers as strings instead.

perl hex() analog in python

perl hex() analog in python how to?
I have next perl code:
my $Lon = substr($Hexline,16,8);
say $output_f "Lon: " . hex($Lon) . "";
where $Hexline has "6a48f82d8e828ce82b82..." format
I try it on python
Lon = int(Hexline[16:24], 16)
f.write('lon = %s' % str(Lon)+'\n')
is it right?
EDIT: in perl's case hex() gives me a decimal value.
Yes, to convert an hexadecimal string to an integer you use int(hex_str, 16).
Note that in your write method call:
You don't need to concatenate two strings to add the new line character, you can add it to the formatting string directly.
To print integer you should use %d instead of %s.
You don't really need to call str to transform the integer into a string.
Hence, the write call could be written as:
f.write('lon = %d\n' % Lon)
Alternatively, you could also use format this way:
f.write('lon = {0}\n'.format(Lon))

Categories

Resources