int string BCD 轉換

  • 7802
  • 0
  • c#
  • 2011-05-11

摘要:進制轉換

把代表16進制的字串轉為UInt32, 下例轉換後的結果a1 = 0x2b:

 String a = "2b";
 UInt32 a1 = UInt32.Parse(a, NumberStyles.HexNumber);

把UInt32轉為16進制的字串, 下例轉換後的結果 s = "12ab" :

 UInt32 Source = 0x12ab;
 String s=Source.ToString("x");

把int轉為一個byte的BCD碼,下例轉換後 bcd = 0x23:

 int value= 23;
 if (value < 0 || value > 99)
  return;
 byte bcd = (byte)(value / 10 * 16 + value % 10);

把int轉為一個int的BCD碼,下例轉換後 bcd = 0x12345678:

            int value = 12345678;
            int bcd = IntToBcd(value);

        int IntToBcd(int value)
        {
            int st = 0;
            if (value != 0)
                st = IntToBcd(value / 10) * 16 + value % 10;
            return st;
        }

把bcd碼轉為UInt32,下例轉換後 ss2=23:

  byte bcd2 = 0x23;
 UInt32 ss2 = UInt32.Parse(bcd2.ToString("x"));