[英]Converting hex to binary in array
I'm working on Visual Studio about python Project and The user input like that 010203
and I use this code for saparating the input: 我正在开发关于python项目的Visual Studio和像
010203
那样的用户输入,我使用这段代码来调整输入:
dynamic_array = [ ]
hexdec = input("Enter the hex number to binary ");
strArray = [hexdec[idx:idx+2] for idx in range(len(hexdec)) if idx%2 == 0]
dynamic_array = strArray
print(dynamic_array[0] + " IAM" )
print(dynamic_array[1] + " NOA" )
print(dynamic_array[2] + " FCI" )
So, the output is: 所以,输出是:
01 IAM
02 NOA
03 FCI
However my expected output converting this hex numbers to binary numbers look like this: 但是我将这个十六进制数转换为二进制数的预期输出如下所示:
00000001 IAM
00000010 NOA
00000011 FCI
Is there any way to do this? 有没有办法做到这一点?
It's a lot easier if you thnk of hex as a integer (number). 如果你把十六进制作为整数(数字),这会容易得多。
There's a lot of tips on how to convert integers to different outcomes, but one useful string representation tool is .format()
which can format a integer (and others) to various outputs. 关于如何将整数转换为不同的结果有很多提示,但是一个有用的字符串表示工具是
.format()
,它可以将整数(和其他)格式化为各种输出。
This is a combination of: 这是以下组合:
The solutions would be: 解决方案是:
binary = '{:08b}'.format(int(hex_val, 16))
And the end result code would look something like this: 最终结果代码看起来像这样:
def toBin(hex_val):
return '{:08b}'.format(int(hex_val, 16)).zfill(8)
hexdec = input("Enter the hex number to binary ");
dynamic_array = [toBin(hexdec[idx:idx+2]) for idx in range(len(hexdec)) if idx%2 == 0]
print(dynamic_array[0] + " IAM" )
print(dynamic_array[1] + " NOA" )
print(dynamic_array[2] + " FCI" )
Rohit also proposed a pretty good solution, but I'd suggest you swap the contents of toBin()
with bin(int())
rather than doing it per print statement. Rohit还提出了一个非常好的解决方案,但我建议你用
bin(int())
交换toBin()
的内容,而不是每个print语句。
I also restructured the code a bit, because I saw no point in initializing dynamic_array
with a empty list. 我还重新编写了一些代码,因为我没有看到使用空列表初始化
dynamic_array
。 Python doesn't need you to set up variables before assigning values to them. Python在为它们赋值之前不需要设置变量。 There was also no point in creating
strArray
just to replace the empty dynamic_array
with it, so concatenated three lines into one. 创建
strArray
只是为了用它替换空的dynamic_array
也没有意义,所以将三行连接成一行。
machnic also points out a good programming tip, the use of format()
on your entire string. machnic还指出了一个很好的编程技巧,在整个字符串上使用
format()
。 Makes for a very readable code :) 使代码非常易读:)
Hope this helps and that my tips makes sense. 希望这有所帮助,我的提示是有道理的。
dynamic_array = [ ]
hexdec = input("Enter the hex number to binary ");
strArray = [hexdec[idx:idx+2] for idx in range(len(hexdec)) if idx%2 == 0]
dynamic_array = strArray
print((bin(int(dynamic_array[0],16))[2:]).zfill(8)+ " IAM" )
print((bin(int(dynamic_array[1],16))[2:]).zfill(8) + " NOA" )
print((bin(int(dynamic_array[2],16))[2:]).zfill(8) + " FCI")
You can format
the string to achieve it. 您可以
format
字符串以实现它。 Your code may look like: 您的代码可能如下所示:
dynamic_array = [ ]
hexdec = input("Enter the hex number to binary ")
strArray = [hexdec[idx:idx+2] for idx in range(len(hexdec)) if idx%2 == 0]
dynamic_array = strArray
print("{:08b} IAM".format(int(dynamic_array[0])))
print("{:08b} NOA".format(int(dynamic_array[1])))
print("{:08b} FCI".format(int(dynamic_array[2])))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.