我有ABC123EFFF。
我想要001010101111000001001000111110111111111111(即二进制表示,例如42位数字和前导零)。
如何实现?
我有ABC123EFFF。
我想要001010101111000001001000111110111111111111(即二进制表示,例如42位数字和前导零)。
如何实现?
将每个十六进制数字替换为相应的四个二进制数字:
1 - 0001
2 - 0010
...
a - 1010
b - 1011
...
f - 1111
将十六进制转换为十进制,再将十进制转换为二进制。
#decimal to binary
def d2b(n):
bStr = ''
if n < 0: raise ValueError, "must be a positive integer"
if n == 0: return '0'
while n > 0:
bStr = str(n % 2) + bStr
n = n >> 1
return bStr
#hex to binary
def h2b(hex):
return d2b(int(hex,16))
# Python Program - Convert Hexadecimal to Binary
hexdec = input("Enter Hexadecimal string: ")
print(hexdec," in Binary = ", end="") # end is by default "\n" which prints a new line
for _hex in hexdec:
dec = int(_hex, 16) # 16 means base-16 wich is hexadecimal
print(bin(dec)[2:].rjust(4,"0"), end="") # the [2:] skips 0b, and the
另一种方式:
import math
def hextobinary(hex_string):
s = int(hex_string, 16)
num_digits = int(math.ceil(math.log(s) / math.log(2)))
digit_lst = ['0'] * num_digits
idx = num_digits
while s > 0:
idx -= 1
if s % 2 == 1: digit_lst[idx] = '1'
s = s / 2
return ''.join(digit_lst)
print hextobinary('abc123efff')
def hex_to_binary( hex_code ):
bin_code = bin( hex_code )[2:]
padding = (4-len(bin_code)%4)%4
return '0'*padding + bin_code
>>> hex_to_binary( 0xABC123EFFF )
'1010101111000001001000111110111111111111'
>>> hex_to_binary( 0x7123 )
'0111000100100011'
bin_code = bin(int(hex_code, 16))[2:]
。 - Stefan我有一个简短的片段,希望能帮到你 :-)
input = 'ABC123EFFF'
for index, value in enumerate(input):
print(value)
print(bin(int(value,16)+16)[3:])
string = ''.join([bin(int(x,16)+16)[3:] for y,x in enumerate(input)])
print(string)
首先,我使用您提供的输入并枚举每个符号。然后将其转换为二进制并从第三个位置修剪到末尾。获得 0 的诀窍是添加输入的最大值 -> 在这种情况下始终为 16 :-)
短形式是 join 方法。享受吧。
HEX_TO_BINARY_CONVERSION_TABLE = {
'0': '0000',
'1': '0001',
'2': '0010',
'3': '0011',
'4': '0100',
'5': '0101',
'6': '0110',
'7': '0111',
'8': '1000',
'9': '1001',
'a': '1010',
'b': '1011',
'c': '1100',
'd': '1101',
'e': '1110',
'f': '1111'}
def hex_to_binary(hex_string):
binary_string = ""
for character in hex_string:
binary_string += HEX_TO_BINARY_CONVERSION_TABLE[character]
return binary_string
当我调用 hex_to_binary("123ade")
函数时
%timeit hex_to_binary("123ade")
这是结果:
316 ns ± 2.52 ns per loop
或者,你可以使用 "join" 方法:
def hex_to_binary_join(hex_string):
hex_array=[]
for character in hex_string:
hex_array.append(HEX_TO_BINARY_CONVERSION_TABLE[character])
return "".join(hex_array)
我也计时了:
%timeit hex_to_binary_join("123ade")
397 ns ± 4.64 ns per loop
return "".join(HEX_TO_BINARY_CONVERSION_TABLE[c] for c in hex_string)
- OneCricketeera = raw_input('hex number\n')
length = len(a)
ab = bin(int(a, 16))[2:]
while len(ab)<(length * 4):
ab = '0' + ab
print ab
import binascii
hexa_input = input('Enter hex String to convert to Binary: ')
pad_bits=len(hexa_input)*4
Integer_output=int(hexa_input,16)
Binary_output= bin(Integer_output)[2:]. zfill(pad_bits)
print(Binary_output)
"""zfill(x) i.e. x no of 0 s to be padded left - Integers will overwrite 0 s
starting from right side but remaining 0 s will display till quantity x
[y:] where y is no of output chars which need to destroy starting from left"""
ABC123EFFF
--ABC123EFFF
是一个字符串还是数字(例如0xABC123EFFF
)? - hola