成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

用C#實現的十進制和十六進制的轉換函數

2010-08-28 10:50:44來源:西部e網作者:

1.十進制轉十六進制:

 public static string Ten2Hex(string ten)
 {
  ulong tenValue = Convert.ToUInt64(ten);
  ulong divValue,resValue;
  string hex = "";
  do
  {
   divValue = (ulong)Math.Floor(tenValue/16);
   resValue = tenValue%16;
   hex = tenValue2Char(resValue) + hex;
   tenValue = divValue;
  }
  while (tenValue >= 16);
  if (tenValue != 0)
   hex = tenValue2Char(tenValue) + hex;
  return hex;
 }

 public static string tenValue2Char(ulong ten)
 {
  switch(ten)
  {
   case 0:
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   case 7:
   case 8:
   case 9:
    return ten.ToString();
   case 10:
    return "A";
   case 11:
    return "B";
   case 12:
    return "C";
   case 13:
    return "D";
   case 14:
    return "E";
   case 15:
    return "F";
   default:
    return "";
  }
 }

2.十六進制轉十進制:

 public static string Hex2Ten(string hex)
 {
  int ten = 0;
  for (int i=0,j=hex.Length-1 ; i<hex.Length ; i++)
  {
   ten += HexChar2Value(hex.Substring(i,1)) * ((int)Math.Pow(16,j));
   j--;
  }
  return ten.ToString();
 }

 public static int HexChar2Value(string hexChar)
 {
  switch (hexChar)
  {
   case "0":
   case "1":
   case "2":
   case "3":
   case "4":
   case "5":
   case "6":
   case "7":
   case "8":
   case "9":
    return Convert.ToInt32(hexChar);
   case "a":
   case "A":
    return 10;
   case "b":
   case "B":
    return 11;
   case "c":
   case "C":
    return 12;
   case "d":
   case "D":
    return 13;
   case "e":
   case "E":
    return 14;
   case "f":
   case "F":
    return 15;
   default:
    return 0;
  }
 }

關鍵詞:C#

贊助商鏈接:

主站蜘蛛池模板: 清涧县| 沽源县| 改则县| 富顺县| 阿尔山市| 新和县| 中宁县| 达州市| 舟曲县| 赤壁市| 察隅县| 长寿区| 锦屏县| 鄂温| 文水县| 阳曲县| 尚志市| 壶关县| 黑山县| 拉萨市| 瑞金市| 安远县| 哈巴河县| 富阳市| 岳池县| 华亭县| 锦屏县| 万山特区| 望都县| 清苑县| 北票市| 普兰店市| 大名县| 固镇县| 桃江县| 平塘县| 方城县| 迁西县| 石家庄市| 萝北县| 河津市|