Conversion d'un texte en Binaire et inversement sous Delphi XE7
Bonjour à tous. je développe une application qui doit communiquer avec une machine en binaire.
j'aimerai avoir un fonction qui converti un texte par exemple {bonjour le forum} en binaire par exemple {01100010011011110110111001101010011011110111010101110010001000000110110001100101001000000110011001101111011100100111010101101101}. d'après mes recherches, j'ai trouvé deux fonctions StrBinToStr et StrToStrBin que voici:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| function StrBinToStr(const S: string): string;
var pInput : pchar;
pOutput : ^byte;
N, LenInput : integer;
const AtBin : array['0'..'1'] of byte =(0,1);
begin
LenInput:=Length(S);
SetLength(Result, LenInput shr 3);
LenInput:=Length(result);
pInput:=PChar(S);
pOutput:=@result[1];
for N:=1 to LenInput do
begin
pOutput^ :=0;
pOutput^ :=byte((AtBin[pInput[0]] shl 7) or
(AtBin[pInput[1]] shl 6) or
(AtBin[pInput[2]] shl 5) or
(AtBin[pInput[3]] shl 4) or
(AtBin[pInput[4]] shl 3) or
(AtBin[pInput[5]] shl 2) or
(AtBin[pInput[6]] shl 1) or
AtBin[pInput[7]]);
inc(pInput,8);
inc(pOutput);
end;
end; |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| function StrToStrBin(const S: string): string;
var pOutput : pchar;
pInput : ^byte;
N, LenInput : integer;
const AtBin : array[boolean] of char = '01';
begin
LenInput:=Length(S);
SetLength(Result, LenInput shl 3);
pInput:=@S[1];
pOutput:=pChar(result);
for N:=1 to LenInput do
begin
pOutput[0]:=AtBin[(pInput^ and $80)=$80];
pOutput[1]:=AtBin[(pInput^ and $40)=$40];
pOutput[2]:=AtBin[(pInput^ and $20)=$20];
pOutput[3]:=AtBin[(pInput^ and $10)=$10];
pOutput[4]:=AtBin[(pInput^ and $08)=$08];
pOutput[5]:=AtBin[(pInput^ and $04)=$04];
pOutput[6]:=AtBin[(pInput^ and $02)=$02];
pOutput[7]:=AtBin[(pInput^ and $01)=$01];
inc(pOutput,8);
inc(pInput);
end;
end; |
ces deux fonctions marchent parfaitement sous Delphi 7 par exemple texte {bonjour le forum} equivaut en binaire à: {01100010011011110110111001101010011011110111010101110010001000000110110001100101001000000110011001101111011100100111010101101101} et inversement.
Mais sous Delphi XE7 cela ne marche pas du tout. le Binaire ci-dessus donne ceci { 潢橮畯敬映牯浵偤牡楴污祬牔湡灳 } sous Delphi XE7
Aidez-moi SVP. Merci