Conversion binaire en caractère

Résolu
codepvc Messages postés 24 Date d'inscription mercredi 29 octobre 2008 Statut Membre Dernière intervention 13 juillet 2011 - 9 nov. 2008 à 16:33
codepvc Messages postés 24 Date d'inscription mercredi 29 octobre 2008 Statut Membre Dernière intervention 13 juillet 2011 - 17 nov. 2008 à 14:13
Bonjour,
voulez vous m'aider pour convertir un texte en binaire à un texte en caractère, c'est-à-dire prendre chaque 8bits et le convertir utilisant le code Ascii, j'ai essayé la fonction "chr " mais elle ne marche pas, par exemple pour chr(01000001) elle me donne le "A" et c'est vrai mais pour chr(01000010) elle me donne "J " et c'est normalement le "B",
Merci d'avance.
A voir également:

10 réponses

codepvc Messages postés 24 Date d'inscription mercredi 29 octobre 2008 Statut Membre Dernière intervention 13 juillet 2011
17 nov. 2008 à 14:13
Merci à tous de me répondre
j'ais trouvé sur ce site un petit algorithme trés simple qui atteint mon objectif, le voila:

Conversion binaire à un caractère

procedure TForm1.Button1Click(Sender: TObject);
var Nbr,result,B,N: integer  ;
begin
  result := 0;
  B := 1; { le premier est a 1 }
  For N := Length(Edit1.Text) downto 1
  do begin                         { si le caractere a la position N est egal a 1 }
      if Edit1.Text[N] = '1' then
      result := result + B;        { alors on additionne Result avec la valeur courrante de B }
      B := B*2;                 { a chaque passage on additionne B avec lui meme }
  end;



  Nbr := result;
  Label1.Caption := chr(Nbr) ;
end;

Merci encore une deuxième fois.
3
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
9 nov. 2008 à 19:11
Bonsoir,
pour B
Showmessage(chr(01000002));
cantador
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
9 nov. 2008 à 19:25
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;

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;






-> S = "le petit chien fait caca"
SB := StrToStrBin(S);
-> SB = "01101100 01100101 00100000 01110000 01100101 01110100 01101001 01110100 00100000 01100011 01101000 01101001 01100101 01101110 00100000 01100110 01100001 01101001 01110100 00100000 01100011 01100001 01100011 01100001"
S := StrBinToStr(SB);
-> S = "le petit chien fait caca"





0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
9 nov. 2008 à 19:32
Je le savais que f0xi viendrait dans cet exercice dans lequel il excelle..

cantador
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
codepvc Messages postés 24 Date d'inscription mercredi 29 octobre 2008 Statut Membre Dernière intervention 13 juillet 2011
9 nov. 2008 à 22:36
pour B se n'est pas Showmessage(chr(01000002));
car j'ai dit binaire c'est à dire uniquement le 0 et le 1;
f0xi, est ce que tu peut m'expliquer chaque instruction car j'ai pas compris le programme, je suis qu'un débutant en delphi,
Merci cantador et f0xi.
0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
9 nov. 2008 à 22:52
essaie avec "le petit chien l'a dans le baba"

cantador
0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
10 nov. 2008 à 13:27
une explication un peu juste..

B: code ascii: 66
showmessage(chr(66)) donne B
et 66 en binaire donne 1000010
et en servant du code à f0xi :
showmessage(strbintoStr('01000010')) donne B

en attendant le tuto..
cantador
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
10 nov. 2008 à 14:36
function StrToStrBin(const S: string): string;

StrToStrBin renvois la representation binaire (texte) d'un texte

var pOutput : pChar;  // pointeur sur sortie de fonction
    pInput : ^byte;   // pointeur sur entrée de fonction

    N, LenInput : integer;

const
  AtBin : array[boolean] of char = '01'; // convertion directe booléen vers 0 ou 1 (false=0 et true=1)

begin

  // recuperation de la taille du texte en entrée
  LenInput := Length(S);

  // definition de la taille de la representation binaire du texte en sortie (LenInput * 8 puisque 1 byte = 8 bits)
  SetLength(Result, LenInput shl 3);

  // pointage sur premier elements du texte en entrée (premier octet)
  pInput := @S[1];

  // pointage sur sortie (pChar, voir l'aide delphi)
  pOutput := pChar(result);

  // pour 1 a taille du texte en entrée
  for N := 1 to LenInput do
  begin
    // pOuput[x] = '1' si bit[y] de pInput = 1, sinon '0'
    { $80 = 10000000 (bit 7)
      $40 = 01000000 (bit 6)
      $20 = 00100000 (bit 5)
      $10 = 00010000 (bit 4)
      $08 = 00001000 (bit 3)
      $04 = 00000100 (bit 2)
      $02 = 00000010 (bit 1)
      $01 = 00000001 (bit 0)
     
      exemple :
      'd' vaut #100 : $64 : 01100100
     

      'd' and $80 01100100 and 10000000 00000000 = '0'

      'd' and $40 01100100 and 01000000 01000000 = '1'

      'd' and $20 01100100 and 00100000 00100000 = '1'

      'd' and $10 01100100 and 00010000 00000000 = '0'

      'd' and $08 01100100 and 00001000 00000000 = '0'

      'd' and $04 01100100 and 00000100 00000100 = '1'

      'd' and $02 01100100 and 00000010 00000000 = '0'

      'd' and $01 01100100 and 00000001 00000000 = '0'
      chaine '01100100' 01100100 = 'd'


    }

    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];

    // increment le pointeur de sortie de 8 bytes
    inc(pOutput,8);

    // incremente le pointeur d'entrée de 1 byte
    inc(pInput);
  end;
end;

// la même chose mais en sens inverse
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;




0
cs_cantador Messages postés 4720 Date d'inscription dimanche 26 février 2006 Statut Modérateur Dernière intervention 31 juillet 2021 13
10 nov. 2008 à 17:32
merci f0xi

cantador
0
jlen100 Messages postés 1606 Date d'inscription samedi 10 juillet 2004 Statut Membre Dernière intervention 25 juillet 2014 13
11 nov. 2008 à 20:09
salut
dans l'exemple que tu donnes(chr(01000001)) 01000001 n'est pas une valeur binaire mais un valeur décimale il est donc normal que chr(01000010) affiche 'J' puisque 'J' est le 10éme caractère

@+
jlen
0
Rejoignez-nous