16进制字符串转BCD码

15位IMEI字符串转8位BCD码

        public static string SwapStr(string str) 
        {
            return (str.Substring(1, 1) + str.Substring(0, 1));
        }

        public static byte[] HexStrToBCD(string hexString)
        {
            hexString = hexString.Replace(" ", "").Trim();
            if (hexString.Length % 2 != 0){
                hexString += "F";
            }
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++) {
                returnBytes[i] = Convert.ToByte(SwapStr(hexString.Substring(i * 2, 2).Trim()), 16);
            }                
            return returnBytes;
        }

"869300038715250"-->{0x68,0x39,0x00,0x30,0x78,0x51,0x52,0xF0}

posted @ 2020-07-09 11:02  bitwoods  阅读(1382)  评论(0编辑  收藏  举报