如何将字符串转16进制的ASCII码数组形式 比如 string buf="124"+" "+"245"+" "+"789";//中间的“ ”表示空格// 需要转换为16进制的ASCII码形式,存到一个byte的数组里,好进行串口通讯时发送出去,byte[] buf16=将字符串转为16进制数组形式; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 byte[] buf16 = Encoding.ASCII.GetBytes(buf); buf.Split(' ').Select(s=>Convert.ToByte(s)).ToArray() 入行时间不短了啊。给你不全一下吧byte[] buf16 = buf.Split(' ').Select(s=>Convert.ToByte(s)).ToArray(); 要加一个16,才是十六进制。code=csharp]byte[] buf16 = buf.Split(' ').Select(s=>Convert.ToByte(s,16)).ToArray();[[/code]不过十六进制的124大于255,byte放不下了,你可能要2位2位的搞,写个函数吧。 private byte[] StringToBytes16(string source) { byte[] destination = new byte[source.Length / 2]; for (int i = 0, j = 0; i < source.Length && j < source.Length / 2; i += 2, j++) { string item = source.Substring(i, 2); destination[j] = Convert.ToByte(item, 16); } return destination; } 摄像头截图问题 C#获取网页中匹配代码的正则问题 bin\debug中的数据库和根目录上的数据库有什么区别? 水晶報表5行換頁套表打印的問題 sql server 自增列问题 各位老师,请来看看这个疑难问题! 麻烦前辈们,我想通过进程获取金城路径,但是有的进程获取就会报错 string str; str="abc" 然后转换成BigEndianUnicode bigstr; 怎么把bigstr逆向转回str 显示图片异常 怎样在c#的组件中得到当前组件所运行的目录路径? C# 如果把这一串字符串转为16进制的ASCII码
byte[] buf16 = buf.Split(' ').Select(s=>Convert.ToByte(s)).ToArray();
code=csharp]
byte[] buf16 = buf.Split(' ').Select(s=>Convert.ToByte(s,16)).ToArray();[
[/code]不过十六进制的124大于255,byte放不下了,你可能要2位2位的搞,写个函数吧。 private byte[] StringToBytes16(string source)
{
byte[] destination = new byte[source.Length / 2];
for (int i = 0, j = 0; i < source.Length && j < source.Length / 2; i += 2, j++)
{
string item = source.Substring(i, 2);
destination[j] = Convert.ToByte(item, 16);
}
return destination;
}