十六进制到二进制,全12位 [英] Hex to binary with all 12 bits
问题描述
i将十六进制值转换为二进制等值,使用以下函数
这里我遇到一个问题,
i正在转换十六进制值ex.:0A8二进制等值是10101000
但是我希望输出为000010101000.
i希望显示所有十二位。
ex.:050
i想要上述值为000001010000
请给我解决方案
我用来将十六进制转换为二进制的函数
函数HexStringToBinary(ByVal hexString As String)As String
Dim num As Integer = Integer.Parse(hexString,Globalization) .NumberStyles.HexNumber)
返回Convert.ToString(num,2)
结束函数
as i我正在计算位数,我需要所有位
提前谢谢
我尝试过:
i am converting a hex value to its binary equivalent,using the below function
here i am facing one problem,
i am converting a hex value ex.:0A8 it binary equivalent is 10101000
but i want the output as 000010101000.
i want all twelve bits to get displayed.
ex.:050
i want the above value as 000001010000
kindly provide me the solution
the function i have used to convert from hex to binary
Function HexStringToBinary(ByVal hexString As String) As String
Dim num As Integer = Integer.Parse(hexString, Globalization.NumberStyles.HexNumber)
Return Convert.ToString(num, 2)
End Function
as i am counting the number of bits,i need all bits
Thanks in advance
What I have tried:
i am converting a hex value to its binary equivalent,using the below function
here i am facing one problem,
i am converting a hex value ex.:0A8 it binary equivalent is 10101000
but i want the output as 000010101000.
i want all twelve bits to get displayed.
ex.:050
i want the above value as 000001010000
kindly provide me the solution
the function i have used to convert from hex to binary
Function HexStringToBinary(ByVal hexString As String) As String
Dim num As Integer = Integer.Parse(hexString, Globalization.NumberStyles.HexNumber)
Return Convert.ToString(num, 2)
End Function
Thanks in advance
推荐答案
请参阅 String.PadLeft方法(Int32,Char)(系统) [ ^ ]。
对于12个二进制数字的情况:
See the String.PadLeft Method (Int32, Char) (System)[^].
In your case for 12 binary digits:
Return Convert.ToString(num, 2).PadLeft(12, "0"c)
尝试:
Try:
Return Convert.ToString(num, 2).PadLeft(12, "0"C)
只是为了改进其他解决方案,支持不同长度的十六进制字符串(只要因为它不大于max int):
Just to improve on other solutions, to support different length of hex string (as long as it's not larger then max int):
Return Convert.ToString(num, 2).PadLeft(hexString.Trim().Length * 4, "0"c)
这篇关于十六进制到二进制,全12位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!