如何将二进制数据转换为十六进制数据? [英] How can I convert binary data to hexadecimal data?
本文介绍了如何将二进制数据转换为十六进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上,我需要上面的逻辑。
提前谢谢.....
Actually, I need the logic for the above.
Thanks in advance.....
推荐答案
看看将长字符串转换为十六进制c# [< a href =http://stackoverflow.com/questions/5612306/converting-long-string-of-binary-to-hex-c-sharptarget =_ blanktitle =New Window> ^ ]
Take a look at Converting long string of binary to hex c#[^]
public static string BinaryStringToHexString(string binary)
{
StringBuilder result = new StringBuilder(binary.Length / 8 + 1);
// TODO: check all 1's or 0's... Will throw otherwise
int mod4Len = binary.Length % 8;
if (mod4Len != 0)
{
// pad to length multiple of 8
binary = binary.PadLeft(((binary.Length / 8) + 1) * 8, '0');
}
for (int i = 0; i < binary.Length; i += 8)
{
string eightBits = binary.Substring(i, 8);
result.AppendFormat("{0:X2}", Convert.ToByte(eightBits, 2));
}
return result.ToString();
}
这篇关于如何将二进制数据转换为十六进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文