急!!!!!请问怎样将string型转换成byte[]型

xuyo 2003-12-25 04:29:22
例如有一个字符串s="0891683110501905F0240D91683155272781F400003021411072410004D4F29C0E"
我想吧他转换成byte[]型
b={0x08,0x91,0x68,0x31,.....0x9C,0x0E}怎样转换?
也就是怎样把string型的08转换成byte型的0x08!
...全文
53 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯嘉 2003-12-25
  • 打赏
  • 举报
回复
// This function converts a string containing number( HEX) characters
// a byte array.
private void stringToBytes()
{
string str
= "0891683110501905F0240D91683155272781F400003021411072410004D4F29C0E";
string temp = null;
int sLength = str.Length / 2;
byte[] bHex = new byte[sLength];
for (int i = 0; i < sLength; i++)
{
temp = str.Substring(i * 2, 2);
bHex[i] = byte.Parse(temp,
System.Globalization.NumberStyles.AllowHexSpecifier);
}
}
xuyo 2003-12-25
  • 打赏
  • 举报
回复
public static byte[] StringToByte(string InString) {
byte[] ByteOut;
ByteOut = new byte[InString.Length/2];
for(int i=0;i<InString.Length;i=i+2)
{
//MessageBox.Show(InString.Substring(i,2));
ByteOut[i]=Convert.ToByte(InString.Substring(i,2));
}
return ByteOut;
}
我这样写的函数,如果遇到0F这样的字符串就无法转换成byte型了
acewang 2003-12-25
  • 打赏
  • 举报
回复
不好意思,没细看,偶在想想
acewang 2003-12-25
  • 打赏
  • 举报
回复
byte[] arr=System.Text.Encoding.Default.GetBytes[s]
xuyo 2003-12-25
  • 打赏
  • 举报
回复
acewang(大灰很)的方法只能将某一个数转换,我要的是两个两个转换的
xuyo 2003-12-25
  • 打赏
  • 举报
回复
转换呢?Convert.ToByte(??)括号里面应该是什么样的格式?
acewang 2003-12-25
  • 打赏
  • 举报
回复
byte[] arr=System.Text.Default.GetBytes[s]
tomsoncat 2003-12-25
  • 打赏
  • 举报
回复
for(int i=0;i<s.length/2;i++)
{
b[i]=s.Substring(i*2,i*2+2)
}
larrybest 2003-12-25
  • 打赏
  • 举报
回复
只有用个循环自己写代码了!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧