简体   繁体   English

以反字节顺序从基数16转换为基数10

[英]Converting from base 16 to base 10 in reverse byte order

I am reading data from a network stream from hardware that starts with a fixed header followed by the length of expected data as a System.UInt16 whose first byte is 0x23 and the second byte is 0x00. 我正在从硬件固定的头开始的网络流中读取数据,其后是期望数据的长度,作为System.UInt16,其第一个字节为0x23,第二个字节为0x00。 The order of the bytes is supposed to be reversed according to the hardware documentation and the value I should expect to convert this ushort into is 35 which is 23 converted from base 16 to base 10. 字节的顺序应该根据硬件文档进行反转,我应该期望将此ushort转换为35的值,即从23到10的23。

How could I make this conversion programatically considering the reverse ordered bytes. 如何以编程方式考虑反向顺序的字节进行此转换。 I am using BinaryReader.ReadUInt16() at the moment. 我目前正在使用BinaryReader.ReadUInt16()。

UPDATE: Please note that I am not looking to convert to string using System.Convert.ToString(value, base) for example. 更新:请注意,我不希望使用System.Convert.ToString(value, base)转换为字符串。

Does this work for your? 这对您有用吗?

byte a1 = 0x23;
byte a2 = 0x00;

ushort a12 = (ushort)(a1 << 8 | a2); //This what you receive
ushort a21 = (ushort)((a12 & 0xFF00) >> 8 | (a12 & 0xFF) << 8);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM