【進位制轉換】十進位制與十六進位制相互轉換方法

secooler發表於2009-11-24
Oracle提供的to_char和to_number函式在進位制轉換上也有貢獻。簡單演示一下。

1.使用TO_CHAR函式將十進位制數轉換為十六進位制數的方法和效果如下
sec@ora10g> select TO_CHAR (1, 'XX') as "1",
  2         TO_CHAR (2, 'XX') as "2",
  3         TO_CHAR (3, 'XX') as "3",
  4         TO_CHAR (4, 'XX') as "4",
  5         TO_CHAR (5, 'XX') as "5",
  6         TO_CHAR (6, 'XX') as "6",
  7         TO_CHAR (7, 'XX') as "7",
  8         TO_CHAR (8, 'XX') as "8",
  9         TO_CHAR (9, 'XX') as "9",
 10         TO_CHAR (10, 'XX') as "10",
 11         TO_CHAR (11, 'XX') as "11",
 12         TO_CHAR (12, 'XX') as "12",
 13         TO_CHAR (13, 'XX') as "13",
 14         TO_CHAR (14, 'XX') as "14",
 15         TO_CHAR (15, 'XX') as "15",
 16         TO_CHAR (16, 'XX') as "16",
 17         TO_CHAR (17, 'XX') as "17"
 18    from DUAL;

  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
  1   2   3   4   5   6   7   8   9   A   B   C   D   E   F  10  11
 
2.反過來,使用TO_NUMBER函式將十六進位制數轉換為十進位制數的方法和效果如下
sec@ora10g> select TO_NUMBER ('1', 'XX') as "1",
  2         TO_NUMBER ('2', 'XX') as "2",
  3         TO_NUMBER ('3', 'XX') as "3",
  4         TO_NUMBER ('4', 'XX') as "4",
  5         TO_NUMBER ('5', 'XX') as "5",
  6         TO_NUMBER ('6', 'XX') as "6",
  7         TO_NUMBER ('7', 'XX') as "7",
  8         TO_NUMBER ('8', 'XX') as "8",
  9         TO_NUMBER ('9', 'XX') as "9",
 10         TO_NUMBER ('A', 'XX') as "A",
 11         TO_NUMBER ('B', 'XX') as "B",
 12         TO_NUMBER ('C', 'XX') as "C",
 13         TO_NUMBER ('D', 'XX') as "D",
 14         TO_NUMBER ('E', 'XX') as "E",
 15         TO_NUMBER ('F', 'XX') as "F",
 16         TO_NUMBER ('10', 'XX') as "10",
 17         TO_NUMBER ('11', 'XX') as "11"
 18    from DUAL;

  1   2   3   4   5   6   7   8   9   A   B   C   D   E   F  10  11
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17

演示完畢。

-- The End --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-620447/,如需轉載,請註明出處,否則將追究法律責任。

相關文章