//十六进制(S)-->>十进制(I) [重写:Jey] function hextoint(s: string): Integer; begin //$代表16进制 Result:=StrToInt('$'+s); end;
//十进制转换为二进制字符串 [重写:Jey] function inttoBin(i: integer): string; begin while i <>0 do begin //i mod 2取模,再使用format格式化 result:=Format('%d'+result,[i mod 2]); i:=i div 2 end end;
//二进制(S)-->>十进制(D) [重写:Jey] uses Math; function hextoint(s: string): Double; begin while Length(s) <>0 do begin //2^(长度-1)次方 if s[1]='1' then Result:=Result+power(2,Length(s)-1); s:=Copy(s,2,Length(s)); end end;
//十进制(I)-->>十六进制(S) //D自带函数,Digits长度,一般设4. function IntToHex(Value: Integer; Digits: Integer): string;
//数据(S)-->>二进制(S) //任何数据都是以二进制形式存储的! (转) function conertde(s:string):string; var i:integer; begin for i:=1 to length(s) do result:=result+inttohex(ord(s[i]),2); end;
最后一个,对大字符串,还是得用这个: function ToHexStr(const S: string): string; const csHexChar: array[0..15] of Char = '0123456789ABCDEF'; var i: Integer; begin SetLength(Result, Length(S) shl 1); for i := Length(S) downto 1 do begin Result[i * 2] := csHexChar[Byte(Result[i]) and $F]; Result[i * 2 -1] := csHexChar[(Byte(Result[i]) and $F0) shr 4]; end; end;
呀,刚才写错了: function ToHexStr(const S: string): string; const csHexChar: array[0..15] of Char = '0123456789ABCDEF'; var i: Integer; begin SetLength(Result, Length(S) shl 1); for i := Length(S) downto 1 do begin Result[i * 2] := csHexChar[Byte(S[i]) and $F]; Result[i * 2 -1] := csHexChar[(Byte(S[i]) and $F0) shr 4]; end; end;
完全用API完成:..uses Windows; function IntToStr(I: integer): string; begin Str(I, Result); end;
function StrToInt(S: string): integer; begin Val(S, Result, Result); end;
function HexToInt(Const HexValue: String) : Integer; begin Val('$'+HexValue, Result, Result); end;
function IntToHex(Const Value: Integer): string; const HexChars: array[0..15] of Char = '0123456789ABCDEF'; 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; Result := Result + HexChars[iTemp div 16]; Result := Result + HexChars[iTemp mod 16]; Inc(i); end; end;
function LowerCase(const S: string): String; begin Result:=CharLower(Pchar(S)); end;
function UpperCase(const S: string): String; begin Result:=CharUpper(Pchar(S)); end;