hex to int python but get a letter in response - python

This is pretty well documented but I keep getting output that doesn't make sense. I have a hex value that looks like
\x00\x00\x00\x00\x00\x01\x86\xa0
but I get
>>> b'\x00\x00\x00\x00\x00\x01\x86\xa0'.hex()
'00000000000186a0'
I am expecting a int or at least a readable number. I assume I am using the wrong function.
Advice?

You need to add a base value of 16
For Example
hex_string = "a1"
integer = int(hex_string, 16)
print(integer)
The output of this will be 161
Try this out

Then try this
hex_bytes = b'\x12\x34'
integer= int.from_bytes(hex_bytes,byteorder='big')
print(integer)

Related

String formatting with left zero padding

I would like to format a string with dynamically changing left zero padding depending on the value input to the string. Something like this:
value = 123
n=6
print("{value:0n}".format(value=value, n=n))
'000123'
But I can't quite get it to work.
Try str.zfill:
print(str(value).zfill(n))
Output:
000123
try
f'%0{n}d' % value # python >= 3.6

Keep zero digit save while converting string to integer in python

I am converting a string into integer using int function and it is working fine but i want to keep save zero digit that are at the start of the string.
string_value = '0123'
print(int(string_value))
result is 123
How can i format output 0123 as in integer type value not in string.
You can't, but if you want to put 0's (zero padding) at the beginning of your number, this is the way to do it.
"{:04}".format(123)
# '0123'
"{:05}".format(123)
# '00123'
Like every one said you can try above answers or the following :
string_value = '0123'
int_no = int(string_value)
print("%04d" % int_no)
print(string_value.zfill(4))
Both will give same answer
Impossible, you cannot get an integer value of 0123.
You should change your mind, you do not actually need 0123 in integer, but you need to keep zero when displaying it. So the question should change to how to format output.

How can i convert this byte properly?

I have a byte array b'string\x01' that i need to format to string1. I need to do this for any "string", followed by a byte e.g, b'string\t' to string9. Why is my way not correctly working?
I have tried to get the x = b'string\x01', i am trying to turn into "string1".
So i need to remove the '\x01', s = str(x).split("g",1) and then byte_part = s[1].rstrip('\'') so i get "\x01" on its own, but the next problem is:
I am trying to convert this string to a byte, so i can use int.from_bytes(byte_part,'little') and get the correct integer result. e.g. \x01 = 1.
What is happening is i am converting the string to a bytearray bytearray(string, 'utf-8') which then gives me bytearray(b'\\x01') then using int.from_bytes() gives me the result for b'\\x01' is 825260124 instead of b'\x01' being 1 i am after.
The method you are looking for is ord().
ord('\x01') # the result is 1
Also, following would convert your string and return the last number.
ord(a.decode().split('string')[1])
hope this helps.

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.

Make Fix Size of Hex Number Python

I want make my hex number have 2 bytes size. How I can do it? Example,if in C we can make %02f. How I make it in Python2.7?
Thankyou
I try to use like this : "{0:03x}".format(number) and works
You can format it in a similar way. Just use "%02x" % (the_number,)
In python the use of hexadecimals is not that common. It is therefore typically represented as either a string or int, ie:
print type(0x50),0x50
#returns <type 'int'> 80
print type(hex(80),hex(80)
#returns <type 'str'>, '0x50'
Knowing this you, could exploit this property by doing something along the line of:
myhex_int=0x5 #integer with value 5
myhex_str='%02d'%myhex_int #string '05',use '0x02d' if you prefer this
#to return it to an integer again:
myhex2=int(myhex_str,16) #set base to 16
#integer with value 5

Categories

Resources