zoukankan      html  css  js  c++  java
  • C# 负数十进制数转十六进制,String转16位ushort

    在做上位机的项目的时候,一般会把数字转成十六进制再发送给下位机。一般会用1个byte,2个byte,4个byte来表示对应的十进制数。相关的转换可以用下面的方法:

    负数转成1byte,2byte,4 byte

    ?Convert.ToSByte("-50").ToString("X")
    "CE"
    ?Convert.ToInt16("-50").ToString("X");
    "FFCE"
    ?Convert.ToInt32("-50").ToString("X");
    "FFFFFFCE"

    ?Convert.ToInt16("-30000").ToString("X");
    "8AD0"
    ?Convert.ToInt32("-30000").ToString("X8");
    "FFFF8AD0"

    ?Convert.ToInt32("-30000").ToString("X8");
    "FFFF8AD0"

    var a = Convert.ToInt16("-100").ToString("X");
    var b = Convert.ToInt64("-9999000000").ToString("X32");
    ushort us = Convert.ToUInt16(("0x" + a), 16);

     ushort  Location = -10000;

    16进制数据转负数。

    string strLocation = Convert.ToInt32(Location).ToString("X8");
    double dLocation = Convert.ToDouble( Convert.ToInt16(strLocation.Substring(4, 4), 16)) / Convert.ToDouble(1000);

    Convert.ToDouble((dLocation).ToString("0.00"));

     1     /// <summary>
     2         /// 
     3         /// </summary>
     4         /// <param name="value">数据</param>
     5         /// <param name="byteOrder">类型1234/3412</param>
     6         /// <returns></returns>
     7         private static float ByteArrayToFloat(ushort[] value, int byteOrder)
     8         {
     9             byte[] bytes = null;
    10             switch (byteOrder)
    11             {
    12                 case 0: //1234 opto22
    13                     bytes = new byte[] {
    14                         (byte)((value[0] >> 8) & 0xff),
    15                         (byte)((value[0] >> 0) & 0xff),
    16                         (byte)((value[1] >> 8) & 0xff),
    17                         (byte)((value[1] >> 0) & 0xff),
    18                     };
    19                     if (BitConverter.IsLittleEndian)
    20                         Array.Reverse(bytes);
    21                     return BitConverter.ToSingle(bytes, 0);
    22                 case 1: //3412 selec
    23                     bytes = new byte[] {
    24                         (byte)((value[1] >> 8) & 0xff),
    25                         (byte)((value[1] >> 0) & 0xff),
    26                         (byte)((value[0] >> 8) & 0xff),
    27                         (byte)((value[0] >> 0) & 0xff),
    28                     };
    29                     if (BitConverter.IsLittleEndian)
    30                         Array.Reverse(bytes);
    31                     return BitConverter.ToSingle(bytes, 0);
    32             }
    33             throw new ArgumentException();
    34         }
      #region 进制转BOOL
            /// <summary>
            /// 
            /// </summary>
            /// <param name="packet">数值</param>
            /// <param name="offset">固定值3</param>
            /// <param name="count">数据长度</param>
            /// <returns></returns>
            public static bool[] DecodeBools(byte[] packet, int offset, ushort count)
            {
                var bools = new bool[count];
                var bytes = BytesForBools(count);
                for (var i = 0; i < bytes; i++)
                {
                    var bits = count >= 8 ? 8 : count % 8;
                    var b = packet[offset + i];
                    ByteToBools(b, bools, bools.Length - count, bits);
                    count -= (ushort)bits;
                }
                return bools;
            }
            private static void ByteToBools(byte b, bool[] bools, int offset, int count)
            {
                for (int i = 0; i < count; i++)
                    bools[offset + i] = ((b >> i) & 0x01) == 1;
            }
            public static byte BytesForBools(int count)
            {
                return (byte)(count == 0 ? 0 : (count - 1) / 8 + 1);
            }
            #endregion
      #region 高低位数据转换
            public static byte High(int value)
            {
                return (byte)((value >> 8) & 0xff);
            }
    
            public static byte Low(int value)
            {
                return (byte)((value >> 0) & 0xff);
            }
            #endregion
  • 相关阅读:
    传输问题
    修改对象目录
    传输与系统单点登录问题
    两个小错误
    BW数据库优化过程记录20100529
    SAP ABAP 到外部数据库Oracle问题
    固定资产传输问题
    软件外包的商业模式和软件人员的职业规划
    做有意义的事,继续研究FarMap
    FarMap诞生了!
  • 原文地址:https://www.cnblogs.com/weifeng123/p/15787595.html
Copyright © 2011-2022 走看看