Delphi中的進制轉換
阿新 • • 發佈:2019-05-11
fun div value con cto span phi function reverse
二進制轉換
function binToDec(Value: string): integer; var str: string; i: integer; begin Str := UpperCase(Value); result := 0; for i := 1 to Length(str) do result := result * 2 + ORD(str[i]) - 48; end; function mod_num(n1, n2: integer): integer; //取余數 begin result := n1 - n1 divn2 * n2 end; function reverse(s: string): string; //取反串 var i, num: Integer; st: string; begin num := Length(s); st := ‘‘; for i := num downto 1 do begin st := st + s[i]; end; Result := st; end; function DecTobin(Value: Integer): string; //十進制轉化二進制 var ST: string; N: Integer; begin ST := ‘‘; n := value; while n >= 2 do begin st := st + IntToStr(mod_num(n, 2)); n := n div 2; end; st := st + IntToStr(n); Result := reverse(st); end;
16進制轉字符
function HexToChar(Str: string): string; var i: integer; buf1:array[0..100] of byte; begin for i := 0 to (length(Str) div 2 - 1) do begin buf1[i] := strtoint(‘$‘ + copy(Str, i * 2 + 1, 2)); Result := Result + char(buf1[i]); end; end; function CharToHex(Str: string): string; var i: integer; ch: char; begin for i := 1 to (length(str)) do begin ch := Str[i]; Result := Result + inttohex(byte(ch), 2); end; end;
16進制轉十進制
function hextoint(s: string): Integer; begin //$代表16進制 Result:=StrToInt(‘$‘+s); end;
數據轉換成二進制
function conertde(s:string):string; var //數據都是以二進制的形式保存 i:integer; begin for i:=1 to length(s) do result:=result+inttohex(ord(s[i]),2); end;
Delphi中的進制轉換