IntToHex(10-16进制的转换)

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 imustworkhard 的 blog


对那个10-2进制转换的程序,我很高兴很多朋友都提供了比我要简洁的好方法,这是看到一位朋友的blog后,写的10-16的转换,一开始我还写错了,呵呵,现在将正确的留在网志,他要求是自己写转换而不是利用类似
format('integer %0:d in hex is %0:.8x', [value]);
当然,有简单方法我们要用简单方法哦接着介绍自己的方法
var hexarr: array[1..15]of string= ('1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');

function int2hex(value: integer): string;
var
  itemp: integer;
  i: integer;
begin
  result := '';
  i := 0;
  while i<4 do
  begin
    case i of
      0: itemp := value shr 24 and $ff;
      1: itemp := value shr 16 and $ff;
      2: itemp := value shr 8 and $ff;
      3: itemp := value and $ff;
    end;
    if not boolean(itemp) then result := result + '00'
    else begin
      result := result + hexarr[itemp div 16];
      result := result + hexarr[itemp mod 16];
    end;
    inc(i);
  end;
end;

测试:showmessage(int2hex(-1)); //显示ffffffff
           showmessage(int2hex(maxint)); //显示7fffffff 

希望大家多提意见,个人觉得挺简洁明了了,呵呵,很明显每步在做什么

本文关键:IntToHex(10-16进制的转换)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top