C#如何将string型的“01010101”转成byte[]的0x55?谢谢!
1个回答
展开全部
public static string B2H(string s)
{
StringBuilder b = new StringBuilder(s.Length / 8 + 1);
int mod4Len = s.Length % 8;
if (mod4Len != 0)
s = s.PadLeft(((s.Length / 8) + 1) * 8, '0');
for (int i = 0; i < s.Length; i += 8)
{
string b8 = s.Substring(i, 8);
b.AppendFormat("{0:X2}", Convert.ToByte(b8, 2));
}
return b.ToString();
}
static void Main(string[] args)
{
string s = B2H("01010101");
//如果只有一字节,下面的更简单
string s55 = string.Format("{0:X2}", Convert.ToByte("01010101", 2));
Console.WriteLine(s);
Console.ReadLine();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询