Delphi 提供 IntToHex(Value, Digits) 直接將整數轉為十六進制字串 (大寫),支援 Integer 與 Int64 多載。
TConversionunit uConversion;
interface
uses SysUtils, Math;
type
TConversion = class
public
// 十進制 → 二/八/十六進制字串
function IntToBitStr (const IntStr: string): string;
function IntToHexStr (const IntStr: string): string;
function IntToOStr (const IntStr: string): string;
// 二進制 → 十/八/十六進制
function BitToInt (const BitStr: string): LongInt;
function BitToHexStr (const BitStr: string): string;
function BitToOStr (const BitStr: string): string;
// 十六進制 → 十/二/八進制
function HexToIntStr (const HexStr: string): string;
function HexToBitStr (const HexStr: string): string;
function HexToOStr (const HexStr: string): string;
// 八進制 → 十/二/十六進制
function OToIntStr (const OStr: string): string;
function OToBitStr (const OStr: string): string;
function OToHexStr (const OStr: string): string;
end;
var
TC: TConversion;
implementation
// --- 輔助常數 ---
const
BitTable: array[0..15] of string = (
'0000','0001','0010','0011','0100','0101','0110','0111',
'1000','1001','1010','1011','1100','1101','1110','1111'
);
OctTable: array[0..7] of string = (
'000','001','010','011','100','101','110','111'
);
// --- 十進制字串 → 整數 (內部共用) ---
function StrToIntSafe(const s: string): Int64;
begin
Result := StrToInt64Def(s, 0);
end;
// --- 十進制 → 二進制 ---
function TConversion.IntToBitStr(const IntStr: string): string;
var
v: Int64;
begin
v := StrToIntSafe(IntStr);
Result := '';
if v = 0 then Exit('0');
while v <> 0 do begin
Result := Char(Ord('0') + (v and 1)) + Result;
v := v shr 1;
end;
end;
// --- 十進制 → 十六進制 (複用 IntToHex) ---
function TConversion.IntToHexStr(const IntStr: string): string;
begin
Result := IntToHex(StrToIntSafe(IntStr), 1); // 最少 1 位
end;
// --- 十進制 → 八進制 (轉二進制再分組) ---
function TConversion.IntToOStr(const IntStr: string): string;
begin
Result := BitToOStr(IntToBitStr(IntStr));
end;
// --- 二進制 → 十進制 ---
function TConversion.BitToInt(const BitStr: string): LongInt;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(BitStr) do
if BitStr[i] = '1' then
Result := (Result shl 1) or 1
else
Result := Result shl 1;
end;
// --- 二進制 → 十六進制 (每 4 位一組) ---
function TConversion.BitToHexStr(const BitStr: string): string;
var
i, len, group: Integer;
val: Byte;
begin
len := Length(BitStr);
group := (len + 3) div 4;
SetLength(Result, group);
val := 0;
i := len;
while i > 0 do begin
if BitStr[i] = '1' then val := val or (1 shl ((len - i) mod 4));
if ((len - i) mod 4) = 3 then begin
Result[group] := Char(ifThen(val < 10, Ord('0')+val, Ord('A')+val-10));
Dec(group); val := 0;
end;
Dec(i);
end;
// 處理最高位不足 4 位
if val > 0 then
Result[group] := Char(ifThen(val < 10, Ord('0')+val, Ord('A')+val-10));
// 移除前導零
i := 1;
while (i < Length(Result)) and (Result[i] = '0') do Inc(i);
if i > 1 then Delete(Result, 1, i-1);
end;
// --- 二進制 → 八進制 (每 3 位一組) ---
function TConversion.BitToOStr(const BitStr: string): string;
var
i, len, group: Integer;
val: Byte;
begin
len := Length(BitStr);
group := (len + 2) div 3;
SetLength(Result, group);
val := 0;
i := len;
while i > 0 do begin
if BitStr[i] = '1' then val := val or (1 shl ((len - i) mod 3));
if ((len - i) mod 3) = 2 then begin
Result[group] := Char(Ord('0') + val);
Dec(group); val := 0;
end;
Dec(i);
end;
if val > 0 then Result[group] := Char(Ord('0') + val);
i := 1;
while (i < Length(Result)) and (Result[i] = '0') do Inc(i);
if i > 1 then Delete(Result, 1, i-1);
end;
// --- 十六進制 → 二進制 (查表法) ---
function TConversion.HexToBitStr(const HexStr: string): string;
var
i, v: Integer;
begin
Result := '';
for i := 1 to Length(HexStr) do begin
v := StrToIntDef('$' + HexStr[i], 0);
Result := Result + BitTable[v];
end;
// 移除前導零
i := 1;
while (i < Length(Result)) and (Result[i] = '0') do Inc(i);
if i > 1 then Delete(Result, 1, i-1);
if Result = '' then Result := '0';
end;
// --- 十六進制 → 十進制 ---
function TConversion.HexToIntStr(const HexStr: string): string;
begin
Result := IntToStr(StrToInt64Def('$' + HexStr, 0));
end;
// --- 十六進制 → 八進制 (轉二進制再轉) ---
function TConversion.HexToOStr(const HexStr: string): string;
begin
Result := BitToOStr(HexToBitStr(HexStr));
end;
// --- 八進制 → 二進制 (查表法) ---
function TConversion.OToBitStr(const OStr: string): string;
var
i, v: Integer;
begin
Result := '';
for i := 1 to Length(OStr) do begin
v := StrToIntDef(OStr[i], 0);
Result := Result + OctTable[v];
end;
i := 1;
while (i < Length(Result)) and (Result[i] = '0') do Inc(i);
if i > 1 then Delete(Result, 1, i-1);
if Result = '' then Result := '0';
end;
// --- 八進制 → 十進制 ---
function TConversion.OToIntStr(const OStr: string): string;
begin
Result := IntToStr(StrToInt64Def('0' + OStr, 0)); // 前綴 0 表示八進制
end;
// --- 八進制 → 十六進制 ---
function TConversion.OToHexStr(const OStr: string): string;
begin
Result := BitToHexStr(OToBitStr(OStr));
end;
end.
var
conv: TConversion;
begin
conv := TConversion.Create;
try
ShowMessage('10→2: ' + conv.IntToBitStr('255')); // 11111111
ShowMessage('10→16: ' + conv.IntToHexStr('255')); // FF
ShowMessage('10→8: ' + conv.IntToOStr('255')); // 377
ShowMessage('2→10: ' + IntToStr(conv.BitToInt('11111111'))); // 255
ShowMessage('16→2: ' + conv.HexToBitStr('FF')); // 11111111
ShowMessage('8→16: ' + conv.OToHexStr('377')); // FF
finally
conv.Free;
end;
end;
Int64 範圍)。StrToIntDef 非法輸入回傳 0,不拋出例外。TConversion 類別提供 Delphi 專案開箱即用的進制轉換工具,涵蓋十/二/八/十六進制全排列互轉。核心演算法採用位運算與查表法,效能優異,介面簡潔,可直接整合至計算機、偵錯器、通訊協定解析等工具中。