C# 一个关于16进制计算的问题

zlero 2018-03-27 11:45:12
我有一个16进制的数据如下
0x7B 0x82
我想让后面的左移八位 与 前面的或,使用C#怎么得到一个short 的值
公式如下
( ( short ) 0x82 << 8) | (short)0x7B

大概需求就是这样。希望写一个方法直接返回一个short数据回来,传入的可以是 16进制字符串
我自己写了一个但算出的结果和标准结果有差距

public Int16 HexToInt16(string p_strRaw)
{
int len = p_strRaw.Length;
byte[] TempArry = new byte[len / 2];
for (int i = 0; i < p_strRaw.Length / 2; i++)
{
TempArry[i] = Convert.ToByte(p_strRaw.Substring(i * 2, 2), 16);
}
return BitConverter.ToInt16(TempArry, 0);
}
...全文
546 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlero 2018-03-27
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
允许出现负数?那你原来的代码就是对的
            Console.WriteLine(HexToInt16_("7B82"));
            Console.WriteLine(HexToInt16("7B82"));
        public static Int32 HexToInt16(string p_strRaw)
        {
            int len = p_strRaw.Length;
            byte[] TempArry = new byte[len / 2];
            for (int i = 0; i < p_strRaw.Length / 2; i++)
            {
                TempArry[i] = Convert.ToByte(p_strRaw.Substring(i * 2, 2), 16);
            }
            return BitConverter.ToInt16(TempArry, 0);
        } 
        public static Int16 HexToInt16_(string p_strRaw)
        {
            var a = short.Parse(p_strRaw, NumberStyles.HexNumber);
            var b = BitConverter.GetBytes(a);
            return (Int16)(b[1] | b[0] << 8);            
        } 
多得到 -32133
感谢了,原来可以在C#中直接使用运算符计算,我以为要转换后再计算,膜拜膜拜
xuzuning 2018-03-27
  • 打赏
  • 举报
回复
允许出现负数?那你原来的代码就是对的
            Console.WriteLine(HexToInt16_("7B82"));
            Console.WriteLine(HexToInt16("7B82"));
        public static Int32 HexToInt16(string p_strRaw)
        {
            int len = p_strRaw.Length;
            byte[] TempArry = new byte[len / 2];
            for (int i = 0; i < p_strRaw.Length / 2; i++)
            {
                TempArry[i] = Convert.ToByte(p_strRaw.Substring(i * 2, 2), 16);
            }
            return BitConverter.ToInt16(TempArry, 0);
        } 
        public static Int16 HexToInt16_(string p_strRaw)
        {
            var a = short.Parse(p_strRaw, NumberStyles.HexNumber);
            var b = BitConverter.GetBytes(a);
            return (Int16)(b[1] | b[0] << 8);            
        } 
多得到 -32133
zlero 2018-03-27
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
( ( short ) 0x82 << 8) | (short)0x7B 得 33403 已经超出 short 可表示的范围了 虽然你还是看到了 33403 这个数,那是因为已被转换成 Int32 类型了 而你的 HexToInt16 方法仍需要返回 Int16 类型,那就只能是负数了(-32133) 你应将 Int16 改成 UInt16
shot类型是确定的,允许出现负数。你算出的这个负数-32133 是对的,能告诉我是怎么算的么?最好是有C# 方法代码 感谢
xuzuning 2018-03-27
  • 打赏
  • 举报
回复
( ( short ) 0x82 << 8) | (short)0x7B 得 33403 已经超出 short 可表示的范围了 虽然你还是看到了 33403 这个数,那是因为已被转换成 Int32 类型了 而你的 HexToInt16 方法仍需要返回 Int16 类型,那就只能是负数了(-32133) 你应将 Int16 改成 UInt16
HandyOrg 2018-03-27
  • 打赏
  • 举报
回复
你要16位?那就是Convert.ToInt16("7B", 16)
HandyOrg 2018-03-27
  • 打赏
  • 举报
回复
Convert.ToInt32("7B", 16)
cheng2005 2018-03-27
  • 打赏
  • 举报
回复
你写的代码根本就没用你给的公式啊!

111,098

社区成员

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

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

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