关于ASCII.GetString,求教!

benny0526 2008-06-27 11:21:12
简单描述一下
byte[] byteTest = new byte[]{
1,0,222,0,255,255
};
ushort ushortTest1,ushortTest2;
ushort.TryParse(Encoding.ASCII.GetString(byteTest , 2, 2), out ushortTest1);
ushort.TryParse(Encoding.ASCII.GetString(byteTest , 4, 2), out ushortTest2);

运行结果:ushortTest1,ushortTest2都为0,这是为什么啊? 哪个地方写错了吗?
请高手指点,谢谢!!
...全文
93 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
benny0526 2008-06-27
  • 打赏
  • 举报
回复
谢谢楼上的,问题解决了,不过你的例子稍微有点出入

ushort ushortTest1 = BitConverter.ToUInt16(byteTest, 2);
ushort ushortTest2 = BitConverter.ToUInt16(byteTest, 4);

这样就搞定了 ^_^

3Q,结贴了
gomoku 2008-06-27
  • 打赏
  • 举报
回复
byteTest is NOT a string.
If you want to get 'ushort' out of an array, use BitConverter instead:


ushort ushortTest1 = BitConverter.ToUInt16(byteTest, 2, 2);
ushort ushortTest2 = BitConverter.ToUInt16(byteTest, 4, 2);

110,500

社区成员

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

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

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