Conversions courantes sous Delphi
Auteur:lyboy99
Courriel : [email protected]
URL : http://hnh.126.com
Vous fournir plusieurs méthodes de conversion couramment utilisées et leurs fonctions de conversion
J'espère que ça t'aide
1.HEX -> Entier
2.2.Déc vers HEX
3.ASCII vers HEX/mathématiques
4.Binaire à décimal
=================================================== ============
1.HEX -> Entier
=================================================== ============
Méthode.1
-------------------------------------------------- ----------------------------------
var
je : entier
s : chaîne ;
commencer
s := '$' + ThatHexString;
je := StrVersInt(a);
fin;
-------------------------------------------------- ----------------------------------
Méthode.2
-------------------------------------------------- ----------------------------------
CONST HEX : ARRAY['A'..'F'] OF INTEGER = (10,11,12,13,14,15);
VAR str : Chaîne ;
Int,
je : entier ;
COMMENCER
READLN(chaîne);
Int := 0;
POUR i := 1 À Longueur(str) DO
SI str[i] < 'A' ALORS Int := Int * 16 + ORD(str[i]) - 48
ELSE Int := Int * 16 + HEX[str[i]];
ÉCRITURE(Int);
LIRE ;
FIN.
=================================================== =
2.Déc vers HEX
-----------------------------------------------
HexString := Format('%0x',DecValue);
-----------------------------------------------
=================================================== ==
3.ASCII vers HEX/mathématiques
--------------------------
unité Hexstr;
interface
utilise String16, SysUtils ;
Taper
Poctet = ^ OCTET ;
PRocedure BytesToHexStr(var hHexStr: String; pbyteArray: PByte; InputLength: Word);
procédure HexStrToBytes(hHexStr: String; pbyteArray: Pointer);
procédure HexBytesToChar(var Réponse : String ; hexbytes : PChar ; InputLength : WORD) ;
mise en œuvre
procédure BytesToHexStr(var hHexStr: String; pbyteArray: PByte; InputLength: WORD);
Const
HexChars : Tableau[0..15] de Char = '0123456789ABCDEF';
var
je, j : MOT ;
commencer
SetLength(hHexStr, (InputLength * 2));
FillChar(hHexStr, sizeof(hHexStr), #0);
j := 1;
pour i := 1 à InputLength, commencez
hHexStr[j] := Char(HexChars[pbyteArray^ shr 4]);
hHexStr[j] := Char(HexChars[pbyteArray^ et 15]);
inc(pbyteArray);
fin;
fin;
procédure HexBytesToChar(var Réponse : String ; hexbytes : PChar ; InputLength : WORD) ;
var
je : MOT ;
c : octet ;
commencer
SetLength(Réponse, InputLength);
FillChar(Réponse, SizeOf(Réponse), #0);
pour i := 0 à (InputLength - 1) commence
c := BYTE(hexbytes[i]) Et BYTE($f);
si c > 9 alors
Inc(c, 37 $)
autre
Inc(c, 30 $);
Réponse[i + 1] := char(c);
fin;{pour}
fin;
procédure HexStrToBytes(hHexStr: String; pbyteArray: Pointer);
{pbyteArray doit pointer vers suffisamment de mémoire pour contenir la sortie}
var
je, j : MOT ;
tempPPTr : PChar ;
deuxchiffres : chaîne[2] ;
commencer
tempPtr := pbyteArray;
j := 1;
pour i := 1 à (Length(hHexStr) DIV 2) commence
deuxChiffres := Copie(hHexStr, j, 2); Inc(j, 2);
PByte(tempPtr)^ := StrToInt('$' + deuxDigits Inc(tempPtr);
fin;{pour}
fin;
fin.
-------------------------------------------------- ----------------------------------
-------------------------------------------------- ----------------------------------
UNITÉ Chaîne16.
interface
{$IFNDEF Win32}
procédure SetLength(var S : chaîne ; Len : Integer);
procédure SetString(var Dst : chaîne ; Src : PChar ; Len : Integer) ;
{$ENDIF}
mise en œuvre
{$IFNDEF Win32}
procédure SetLength(var S : chaîne ; Len : Integer);
commencer
si Len > 255 alors
S[0] := Chr(255)
autre
S[0] := Chr(Len)
fin;
procédure SetString(var Dst : chaîne ; Src : PChar ; Len : Integer) ;
commencer
si Len > 255 alors
Déplacer(Src^, Dst[1], 255)
autre
Déplacer(Src^, Dst[1], Len);
SetLength(Dst, Len);
fin;
{$ENDIF}
fin.
=================================================== ===============
4.Binaire à décimal
-------------------------------------------------- ---------------
/////////////////////////////////////////////// /
// convertit 32 bits base2 en 32 bits base10 //
// nombre maximum = 99 999 999, renvoie -1 si plus //
/////////////////////////////////////////////// /
fonction Base10(Base2:Integer) : Entier ;
asme
cmp eax,100000000 // vérifier la limite supérieure
jb @1 // ok
mov eax,-1 // indicateur d'erreur
jmp @exit // quitte avec -1
@1 :
push ebx // sauvegarder les registres
pousser esi
xor esi,esi // résultat = 0
mov ebx,10 // base de dérivation 10
mov ecx,8 // 8 quartets (10^8-1)
@2 :
mov edx,0 // effacer le reste
div ebx // eax DIV 10, edx mod 10
ajouter esi,edx // résultat = résultat + reste[I]
ror esi,4 // décalage quartet
loop@2 // boucle pour les 8 quartets
mov eax,esi // résultat de la fonction
pop esi // restaurer les registres
pop ebx
@sortie:
fin;
-------------------------------------------------- ----------------------------------
Méthode.2
[Oliver Townshend, [email protected]]
-------------------------------------------------- ----------------------------------
function IntToBin(Valeur : LongInt;Taille : Integer) : Chaîne ;
var
je : entier ;
commencer
Résultat :=';
pour i:=Taille jusqu'à 0, commencez
si Valeur et (1 shl i)<>0 alors commencez
Résultat :=Résultat+'1' ;
fin sinon commence
Résultat :=Résultat+'0' ;
fin;
fin;
fin;
fonction BinToInt (Valeur : Chaîne) : LongInt ;
var
i, Taille : Entier ;
commencer
Résultat :=0 ;
Taille :=Longueur(Valeur);
pour i:=Taille jusqu'à 0, commencez
si Copy(Value,i,1)='1' alors commencez
Résultat :=Résultat+(1 shl i);
fin;
fin;
fin;
=================================================== =========================