如何将一个string转换为byte数组

zhizlm 2009-03-01 02:40:45
比如 string是 61-00-61-00 转换为byte赋给一个变量,改如何做呢?
...全文
178 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
whzlong328 2009-03-01
  • 打赏
  • 举报
回复

string a = "61-00-61-00";
string[] result = a.Split(new char[]{'-'});

byte[] arrByte = new byte[result.Length];

for(int i=0;i<result.Length;i++)
{
arrByte[i] = Convert.ToByte(result[i]);
}
vrhero 2009-03-01
  • 打赏
  • 举报
回复
例...自己加上输入检查和异常处理...
string input = "61-00-61-00";
byte[] result = Array.ConvertAll<string, byte>(input.Split('-'), delegate(string s) { return byte.Parse(s); });
Mr_Long 2009-03-01
  • 打赏
  • 举报
回复
很简单。字符串切割,以“—”做切割的标准。没逢“—”就切割一次。
然后存储到数组中去。
zhizlm 2009-03-01
  • 打赏
  • 举报
回复
我的意思是让
byte数组为
61
00
61
00
wangping_li 2009-03-01
  • 打赏
  • 举报
回复
byte[] byte= System.Text.Encoding.Default.GetBytes("61-00-61-00");
x_ch 2009-03-01
  • 打赏
  • 举报
回复
System.Text.Encoding.....一直点下去帮助多多
vrhero 2009-03-01
  • 打赏
  • 举报
回复
System.Text.Encoding.ASCII.GetBytes("61-00-61-00")

注意编码...如果字符串中包含Unicode文字如汉字,要用本机编码或Unicode编码器...如...

System.Text.Encoding.UTF8.GetBytes("61-00-61-00")//UTF-8编码

System.Text.Encoding.Default.GetBytes("61-00-61-00")//本机编码,简体中文系统就是GB2312编码

System.Text.Encoding.GetEncoding("GB2312").GetBytes("61-00-61-00")//GB2312编码
-过客- 2009-03-01
  • 打赏
  • 举报
回复
try...

string test = "61-00-61-00";
byte[] result = System.Text.Encoding.Default.GetBytes(test);

111,126

社区成员

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

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

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