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
Related
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)
Let's assume that I get number as string or hex or int and I want to convert to integer. I'm looking for a more generic form.
for example,
if I write:
int('0x10',16) then int('0x10') will throw exception.
if I write:
int('10') then int('10',16) will throw exception.
Do I need to check type with instanceof ?
If the string represents a valid hex, it is prefixed by 0x. So, you could just check value.startswith('0x')
You need to specify the base (docs), when using int() on a hex value:
>>> _hex = hex(int("32"))
>>> _hex
'0x20'
>>> int(_hex, 0) # 0 means: Python automatically detects hex or decimal
32
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.
I type following code
z=1+2j
z=list(str(z))
I get output as
['(','1','+','2','j',')']
but when I write the following code
z=input('')
z=list(str(z))
print(z)
I get output as
['1','+','2','j']
and not the curve brackets in the list why?
When using input(), you entered 4 characters: 1,+,2,j. Together, they may have the same semantic value to you as the number 1+2j, but as far as Python is concerned, the former case is just four characters. Python doesn't have any way of knowing you think those characters should be converted to a single numerical value.
You can observe the differences with a few print statements.
And you can explicitly declare that this four-character string should be interpreted as a complex number, with complex():
a = input('') # input: "1+2j"
print(a) # i+2j
print(type(a)) # <class 'str'>
b = complex(a)
print(b) # (1+2j)
print(type(b)) # <class 'complex'>
You probably meant:
z = "1+2j"
When you typed the code
z=1+2j
z=list(str(z))
Type of z is complex. Calling the function str on it will simply give you what __str__ function of class complex returns. In your case, str(z) would return
'(1+2j)'
When you do z=list(str(z)), it gives you a list of all the characters in str(z). Now z points to a list not to a complex object.
When you run this
z=input('')
z=list(str(z))
print(z)
input('') gives you a str type value. Whatever you enter here is converted to the list when you call z=list(str(z)).
So, I've been banging my head against the wall for too long on what seems like it should be an easy data conversion. I am writing in python and passing to another module a hex value that is converted with a wrapper to c type uint_64t. the problem is I am getting this hex value via the python library argparse. When it takes in this value, for example lets use the value 0x3f, it saves it as a string. If I try to cast this as an int it throws the error:
"ValueError: invalid literal for int() with base 10: '0x3f'"
If I create a variable hex = 0x3f however, when I print it out, it gives me the appropriate integer value. (which is great since I'm creating a uint) I am just confused how to make the conversion from string to int if a cast doesn't work. I have seen plenty of examples on turning this string into a hex value by letter (in other words take each individual character of the ascii string '0x3f' and give it a hex value) but I haven't been able to find an example of the situation I am looking for. Apologies if I'm bringing up something that has been answered time and again.
Try specifying that the int is in base 16:
>>> int("0x3f", 16)
63
You could also use ast.literal_eval, which should be able to read any string that could be used as an integer literal. You don't even have to specify a base for this one, as long as the 0x prefix is present.
>>> import ast
>>> #hexadecimal
>>> ast.literal_eval("0x3f")
63
>>> #binary
>>> ast.literal_eval("0b01010")
10
>>> #octal
>>> ast.literal_eval("0712")
458
>>> #decimal
>>> ast.literal_eval("42")
42
int takes an optional second argument, which is the numerical base to use for conversion. The default is 10 (decimal conversion) but you can change it to 16 (for hex) or 0 (for automatic conversion based on prefix):
>>> int('0b1010', 0)
10
>>> int('0x3f', 0)
63
>>> int('0o777', 0)
511
>>> int('1234', 0)
1234