- Delphi的几种类型转换
- unit Support;
- interface
- type dword=longword;
- function WordToStr(Value: word): string;
- function DwordToStr(Value: dword): string;
- function StrToWord(Value: string): word;
- function StrToDword(Value: string): dword;
- procedure SetBit(var Str: string; BitNr: dword; Value: boolean);
- function GetBit(Str: string; BitNr: dword): boolean;
- function Pack(I: string):string;
- function UnPack(I: string): string;
- procedure FindBest(Main, Sub: string;var FoundLen, FoundPos: integer);
- implementation
- // DwordToStr() : Converts a DWORD to a 4 byte string
- function DwordToStr(Value: dword): string;
- var
- ResultPtr: PChar;
- begin
- SetLength(Result, 4);
- ResultPtr:=@Result[1];
- asm
- MOV EAX, [ResultPtr]
- MOV EBX, Value
- MOV [EAX], EBX end;
- end;
- // StrToDWord() : Converts a 4 byte string to a DWORD
- function StrToDword(Value: string): dword;
- var
- ValuePtr: PChar;
- begin
- ValuePtr:=@Value[1];
- asm
- MOV EAX, [ValuePtr]
- MOV EAX, [EAX]
- MOV Result, EAX end;
- end;
- // WordToStr() : Converts a WORD to a 2 byte string
- function WordToStr(Value: word): string;
- var
- ResultPtr: PChar;
- begin
- SetLength(Result, 2);
- ResultPtr:=@Result[1];
- asm
- MOV EAX, [ResultPtr]
- MOV BX, Value
- MOV [EAX], BX end;
- end;
- // StrToWord() : Converts a 2 byte string to a WORD
- function StrToWord(Value: string): word;
- var
- ValuePtr: PChar;
- begin
- ValuePtr:=@Value[1];
- asm
- MOV EAX, [ValuePtr]
- MOV AX, [EAX]
- MOV Result, AX end;
- end;
- function HexStrToStr(const S:string):string;
- //16进制字符串转换成字符串
- var
- t:Integer;
- ts:string;
- M,Code:Integer;
- begin
- t:=1;
- Result:='';
- while t<=Length(S) do
- begin //xlh 2006.10.21
- while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
- inc(t);
- if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
- ts:='$'+S[t]
- else
- ts:='$'+S[t]+S[t+1];
- Val(ts,M,Code);
- if Code=0 then
- Result:=Result+Chr(M);
- inc(t,2);
- end;
- end;
- function StrToHexStr(const S:string):string;
- //字符串转换成16进制字符串
- var
- I:Integer;
- begin
- for I:=1 to Length(S) do
- begin
- if I=1 then
- Result:=IntToHex(Ord(S[1]),2)
- else Result:=Result+' '+IntToHex(Ord(S[I]),2);
- end;
- end;
- // SetBit() : Sets a single BIT in a string to true or false
- procedure SetBit(var Str: string; BitNr: dword; Value: boolean);
- var
- CharNr: dword;
- CharBit: byte;
- Original, Mask: byte;
- begin
- CharNr:=(BitNr DIV 8)+1;
- CharBit:=(BitNr MOD 8);
- Original:=byte(Str[CharNr]);
- Mask:=1 shl CharBit;
- if Value=true then
- Original:=(Original or Mask)
- else
- Original:=(Original and not Mask);
- Str[CharNr]:=char(Original);
- end;
- // GetBit() : Returns the state of a single bit in a string
- function GetBit(Str: string; BitNr: dword): boolean;
- var
- CharNr: dword;
- CharBit: byte;
- Original, Mask: byte;
- begin
- CharNr:=(BitNr DIV 8)+1;
- CharBit:=(BitNr MOD 8);
- Original:=byte(Str[CharNr]);
- Mask:=1 shl CharBit;
- if (Original and Mask)=Mask then
- Result:=true
- else
- Result:=false;
- end;
- // Pack() : Compresses a string to a hopefully smaller string
- function Pack(I: string):string;
- var
- Header: string;
- Tag,T1,T2: string;
- Buffer: string;
- History: string;
- FindStr: string;
- P: integer;
- FP,FL: integer;
- begin
- SetLength(Tag,(Length(I) DIV 8)+1); // Create TAG string
- Header:=DwordToStr(Length(I)); // Create Header string (length of original)
- // Pack the string
- P:=1; while P<=Length(I) do begin
- FindStr:=Copy(I,P,10);
- FindBest(History,FindStr,FL,FP);
- if FL>2 then begin // if match found in history and length>2
- Buffer:=Buffer+WordToStr((FP SHL 3)+(FL-3));
- History:=History+Copy(History,FP,FL);
- T1:=Copy(I,P,FL);
- T2:=Copy(History,FP,FL);
- SetBit(Tag,P-1,true);
- P:=P+(FL-1);
- end else begin // if no match found in history
- Buffer:=Buffer+I[P];
- History:=History+I[P];
- SetBit(Tag,P-1,false);
- end;
- if Length(History)>8100 then History:=Copy(History,1024,8100); INC(P);
- end;
- Result:=Header+Tag+Buffer;
- end;
- // UnPack() : DeCompresses a string compressed with Pack()
- function UnPack(I: string): string;
- var
- Tag,T: string;
- Buffer: string;
- TmpWrd: string;
- History: string;
- P, OL: integer;
- FP, FL: integer;
- begin
- // Split I in Tag and Buffer
- OL:=StrToDword(I);
- SetLength(Buffer, OL);
- SetLength(Tag,(OL DIV 8)+1);
- P:=5;
- Tag:=Copy(I,P,Length(Tag));
- P:=P+Length(Tag);
- Buffer:=Copy(I,P,Length(Buffer));
- Result:='';
- // begin unpacking
- P:=1; while Length(Result)<OL do begin
- if GetBit(Tag, Length(Result))=true then begin // if is packed
- TmpWrd:=Buffer[P]+Buffer[P+1];
- FL:=(StrToWord(TmpWrd) and 7)+3;
- FP:=(StrToWord(TmpWrd) shr 3) and 8191;
- Result:=Result+Copy(History,FP,FL);
- History:=History+Copy(History,FP,FL);
- T:=Copy(History,FP,FL);
- P:=P+1;
- end else begin // if is not packed
- Result:=Result+Buffer[P];
- History:=History+Buffer[P];
- end;
- if Length(History)>8100 then History:=Copy(History,1024,8100); INC(P);
- end;
- end;
- // FindBest() : Finds a substring in another string an returns position and
- // the number of characters upto where they are equal
- procedure FindBest(Main, Sub: string;var FoundLen, FoundPos: integer);
- var
- P,T,FL,MaxLen: integer;
- begin
- if Length(Sub)>Length(Main) then
- MaxLen:=Length(Main)
- else
- MaxLen:=Length(Sub);
- FoundLen:=0; FoundPos:=0;
- for P:=1 to Length(Main)-MaxLen do begin
- FL:=0;
- for T:=1 to MaxLen do begin
- if Main[P+T-1]=Sub[T] then FL:=T else Break;
- end;
- if FL>FoundLen then begin
- FoundLen:=FL;
- FoundPos:=P;
- end;
- end;
- end;
- end.
- 在做一些和硬件通讯的程序中,交换和读写的数据格式有时是 16 进制,比如说从 IC 卡 或 ID 卡获取到的数据,把他们转换成 10 进制可能更便于使用。
- Delphi 中 16 进制数以 $ 为前缀,所以将 16 进制形式的字符串转换为整数即可:
- function HexToDec(const AHexString: String): Integer;
- begin
- // 16 进制转换为 10 进制;
- Result := StrToInt(’$’ + AHexString);
- end;
- //十六进制(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;
- 16进制字符转换为10进制 delphi
- Function HexToDec(const Value :Integer ) : string;
- var
- s : string;
- begin
- s := '$' + IntToStr(Value);
- Result := InToStr(StrToInt(s));
- end;
- (2)方法2
- Funtion HexToDec(const Value :Integer) : string;
- CONST HEX : ARRAY['A'..'F'] OF INTEGER =
- (10,11,12,13,14,15);
- VAR
- str : String;
- In : Integer;
- i : integer;
- BEGIN
- Str := UpperCase(IntToStr(Value));
- Int := 0;
- FOR i := 1 TO Length(str) DO
- IF str[i] < 'A' THEN
- Int := Int * 16 + ORD(str[i]) - 48
- ELSE
- Int := Int * 16 + HEX[str[i]];
- Result := IntToStr(Int);
- end;
Delphi的几种类型转换
最新推荐文章于 2023-11-28 18:30:14 发布