begin process at 2008 08 20 15:06:09
1 228 884 membres
250 nouveaux aujourd'hui
14 258 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

ULTRACONVERTOR - CONVERSION DE CHAÎNE CHR-DEC-HEX-BIN + OPTIONS


Information sur la source

Catégorie :Divers Niveau : Débutant Date de création : 12/06/2003 Date de mise à jour : 12/06/2003 08:35:21 Vu / téléchargé: 2 339 / 239

Note :
10 / 10 - par 1 personne
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (8)
Ajouter un commentaire et/ou une note

Description

Voilà, j'pense qu'il y en a déjà eu des convertisseurs, mais celui là est assez complet :
- bon déjà il va dans les 4 sens
- les chaînes sont limités à la puissance du pc (100 caractères ne l'effrayent pas)
- il rajoute des espaces si vous le voulez
- il rajoute des '00' en hex (utile pour vous savez quoi...)

Voilà l'code :

Source

  • unit Unit1;
  • interface
  • uses
  • Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  • Dialogs, StdCtrls;
  • type
  • TForm1 = class(TForm)
  • Label1: TLabel;
  • Edit1: TEdit;
  • Label2: TLabel;
  • Edit2: TEdit;
  • Label3: TLabel;
  • Edit3: TEdit;
  • Label4: TLabel;
  • Edit4: TEdit;
  • Label5: TLabel;
  • CheckBox1: TCheckBox;
  • CheckBox2: TCheckBox;
  • CheckBox3: TCheckBox;
  • CheckBox4: TCheckBox;
  • procedure Edit1Change(Sender: TObject);
  • procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  • procedure Edit2KeyPress(Sender: TObject; var Key: Char);
  • procedure Edit3KeyPress(Sender: TObject; var Key: Char);
  • procedure Edit4KeyPress(Sender: TObject; var Key: Char);
  • procedure Edit2Change(Sender: TObject);
  • procedure Edit3Change(Sender: TObject);
  • procedure Edit4Change(Sender: TObject);
  • private
  • { Déclarations privées }
  • public
  • { Déclarations publiques }
  • end;
  • var
  • Form1 : TForm1;
  • id : integer;
  • implementation
  • {$R *.dfm}
  • function ChrToDec(chaine : string; space : boolean) : string;
  • var
  • i : integer;
  • value : string;
  • begin
  • result := '';
  • for i := 1 to length(chaine) do
  • begin
  • value := IntToStr(ord(chaine[i]));
  • while length(value) <> 3 do
  • value := '0'+value;
  • if (space = true) and (i <> 1) then
  • value := ' '+value;
  • result := result+value;
  • end;
  • end;
  • function DecToHex(chaine : string; space, zero : boolean) : string;
  • var
  • i : integer;
  • value : string;
  • begin
  • result := '';
  • for i := 1 to length(chaine) do
  • begin
  • value := IntToHex(ord(chaine[i]),2);
  • if (space = true) and (i <> 1) then
  • value := ' '+value;
  • if (zero = true) and (i <> 1) then
  • value := '00'+value;
  • if (space = true) and (zero = true) and (i <> 1) then
  • value := ' '+value;
  • result := result+value;
  • end;
  • end;
  • function DecToBin(chaine : string; space : boolean) : string;
  • var
  • i, i2, byte : integer;
  • value : string;
  • begin
  • result := '';
  • for i := 1 to length(chaine) do
  • begin
  • value := '';
  • byte := ord(chaine[i]);
  • for i2 := 7 downto 0 do
  • if byte and (1 shl i2)<>0 then
  • value := value+'1'
  • else
  • value := value+'0';
  • if (space = true) and (i <> 1) then
  • value := ' '+value;
  • result := result+value;
  • end;
  • end;
  • function DecToChr(chaine : string) : string;
  • var
  • i : integer;
  • value : string;
  • begin
  • value := '';
  • for i := 1 to length(chaine) do
  • if (ord(chaine[i]) <> 32) then
  • value := value+chaine[i];
  • chaine := value;
  • if length(chaine) < 3 then exit;
  • SetLength(chaine,(length(chaine) div 3)*3);
  • result := '';
  • i := 1;
  • repeat
  • value := chaine[i]+chaine[i+1]+chaine[i+2];
  • if StrToInt(value) > 255 then
  • begin
  • ShowMessage(value+' is an incorrect value');
  • exit;
  • end;
  • if (value <> '000') then
  • begin
  • value := chr(StrToInt(value));
  • result := result+value;
  • end;
  • Inc(i,3);
  • until i = length(chaine)+1;
  • end;
  • function HexToDec(chaine : string; space : boolean) : string;
  • var
  • i : integer;
  • value : string;
  • begin
  • value := '';
  • for i := 1 to length(chaine) do
  • if (ord(chaine[i]) <> 32) then
  • value := value+chaine[i];
  • chaine := value;
  • if length(chaine) < 2 then exit;
  • SetLength(chaine,(length(chaine) div 2)*2);
  • result := '';
  • i := 1;
  • repeat
  • value := IntToStr(StrToInt('$'+chaine[i]+chaine[i+1]));
  • while length(value) <> 3 do
  • value := '0'+value;
  • if (value <> '000') then
  • begin
  • if (space = true) and (i <> 1) then
  • value := ' '+value;
  • result := result+value;
  • end;
  • Inc(i,2);
  • until i = length(chaine)+1;
  • end;
  • function BinToDec(chaine : string; space : boolean) : string;
  • var
  • i, i2, value2 : integer;
  • value : string;
  • begin
  • value := '';
  • for i := 1 to length(chaine) do
  • if (ord(chaine[i]) <> 32) then
  • value := value+chaine[i];
  • chaine := value;
  • if length(chaine) < 8 then exit;
  • SetLength(chaine,(length(chaine) div 8)*8);
  • result := '';
  • i := 1;
  • repeat
  • value := chaine[i ]+chaine[i+1]+chaine[i+2]+chaine[i+3]+
  • chaine[i+4]+chaine[i+5]+chaine[i+6]+chaine[i+7];
  • value2 := 0;
  • for i2 := 1 to 8 do
  • begin
  • if (value[i2] <> '0') and (value[i2] <> '1') then
  • begin
  • ShowMessage('An error occured'+#10+'You may have entered bad data');
  • result := '';
  • exit;
  • end;
  • if (value[i2] = '0') then
  • value2 := value2*2
  • else
  • value2 := value2*2+1;
  • end;
  • value := IntToStr(value2);
  • while (length(value) <> 3) do
  • value := '0'+value;
  • if (space = true) and (i <> 1) then
  • value := ' '+value;
  • result := result+value;
  • Inc(i,8);
  • until i = length(chaine)+1;
  • end;
  • procedure TForm1.Edit1Change(Sender: TObject);
  • begin
  • if (id = 1) then
  • begin
  • try
  • Edit2.Text := ChrToDec(Edit1.Text, CheckBox1.Checked);
  • Edit3.Text := DecToHex(Edit1.Text, CheckBox2.Checked, CheckBox4.Checked);
  • Edit4.Text := DecToBin(Edit1.Text, CheckBox3.Checked);
  • except
  • ShowMessage('An error occured'+#10+'You may have entered bad data');
  • end;
  • end;
  • end;
  • procedure TForm1.Edit2Change(Sender: TObject);
  • begin
  • if (id = 2) then
  • begin
  • try
  • Edit1.Text := DecToChr(Edit2.Text);
  • Edit3.Text := DecToHex(Edit1.Text, CheckBox2.Checked, CheckBox4.Checked);
  • Edit4.Text := DecToBin(Edit1.Text, CheckBox3.Checked);
  • except
  • ShowMessage('An error occured'+#10+'You may have entered bad data');
  • end;
  • end;
  • end;
  • procedure TForm1.Edit3Change(Sender: TObject);
  • begin
  • if (id = 3) then
  • begin
  • try
  • Edit2.Text := HexToDec(Edit3.Text, CheckBox1.Checked);
  • Edit1.Text := DecToChr(Edit2.Text);
  • Edit4.Text := DecToBin(Edit1.Text, CheckBox3.Checked);
  • except
  • ShowMessage('An error occured'+#10+'You may have entered bad data');
  • end;
  • end;
  • end;
  • procedure TForm1.Edit4Change(Sender: TObject);
  • begin
  • if (id = 4) then
  • begin
  • try
  • Edit2.Text := BinToDec(Edit4.Text, CheckBox1.Checked);
  • Edit1.Text := DecToChr(Edit2.Text);
  • Edit3.Text := DecToHex(Edit1.Text, CheckBox2.Checked, CheckBox4.Checked);
  • except
  • ShowMessage('An error occured'+#10+'You may have entered bad data');
  • end;
  • end;
  • end;
  • procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  • begin
  • id := 1;
  • end;
  • procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
  • const
  • ValidChars = ['0'..'9', #8, #26, #24, #3, #22, #1, #32];
  • begin
  • id := 2;
  • if not (Key in ValidChars) then Key := #0;
  • end;
  • procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
  • var
  • str : string;
  • const
  • ValidChars = ['0'..'9', 'a'..'f', 'A'..'F', #8, #26, #24, #3, #22, #1, #32];
  • begin
  • id := 3;
  • str := Key;
  • Key := UpperCase(str)[1];
  • if not (Key in ValidChars) then Key := #0;
  • end;
  • procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);
  • const
  • ValidChars = ['0', '1', #8, #26, #24, #3, #22, #1, #32];
  • begin
  • id := 4;
  • if not (Key in ValidChars) then Key := #0;
  • end;
  • end.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Edit4: TEdit;
    Label5: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    procedure Edit1Change(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure Edit4KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1 : TForm1;
  id : integer;

implementation

{$R *.dfm}

function ChrToDec(chaine : string; space : boolean) : string;
var
  i : integer;
  value : string;
begin
  result := '';
  for i := 1 to length(chaine) do
    begin
      value := IntToStr(ord(chaine[i]));
      while length(value) <> 3 do
        value := '0'+value;
      if (space = true) and (i <> 1) then
        value := ' '+value;
      result := result+value;
    end;
end;

function DecToHex(chaine : string; space, zero : boolean) : string;
var
  i : integer;
  value : string;
begin
  result := '';
  for i := 1 to length(chaine) do
    begin
      value := IntToHex(ord(chaine[i]),2);
      if (space = true) and (i <> 1) then
        value := ' '+value;
      if (zero = true) and (i <> 1) then
        value := '00'+value;
      if (space = true) and (zero = true) and (i <> 1) then
        value := ' '+value;
      result := result+value;
    end;
end;

function DecToBin(chaine : string; space : boolean) : string;
var
  i, i2, byte : integer;
  value : string;
begin
  result := '';
  for i := 1 to length(chaine) do
    begin
      value := '';
      byte := ord(chaine[i]);
      for i2 := 7 downto 0 do
        if byte and (1 shl i2)<>0 then
          value := value+'1'
        else
          value := value+'0';
      if (space = true) and (i <> 1) then
        value := ' '+value;
      result := result+value;
    end;
end;

function DecToChr(chaine : string) : string;
var
  i : integer;
  value : string;
begin
  value := '';
  for i := 1 to length(chaine) do
    if (ord(chaine[i]) <> 32) then
      value := value+chaine[i];
  chaine := value;
  if length(chaine) < 3 then exit;
  SetLength(chaine,(length(chaine) div 3)*3);
  result := '';
  i := 1;
  repeat
    value := chaine[i]+chaine[i+1]+chaine[i+2];
    if StrToInt(value) > 255 then
      begin
        ShowMessage(value+' is an incorrect value');
        exit;
      end;
    if (value <> '000') then
      begin
        value := chr(StrToInt(value));
        result := result+value;
      end;
    Inc(i,3);
  until i = length(chaine)+1;
end;

function HexToDec(chaine : string; space : boolean) : string;
var
  i : integer;
  value : string;
begin
  value := '';
  for i := 1 to length(chaine) do
    if (ord(chaine[i]) <> 32) then
      value := value+chaine[i];
  chaine := value;
  if length(chaine) < 2 then exit;
  SetLength(chaine,(length(chaine) div 2)*2);
  result := '';
  i := 1;
  repeat
    value := IntToStr(StrToInt('$'+chaine[i]+chaine[i+1]));
    while length(value) <> 3 do
      value := '0'+value;
    if (value <> '000') then
      begin
        if (space = true) and (i <> 1) then
          value := ' '+value;
        result := result+value;
      end;
    Inc(i,2);
  until i = length(chaine)+1;
end;

function BinToDec(chaine : string; space : boolean) : string;
var
  i, i2, value2 : integer;
  value : string;
begin
  value := '';
  for i := 1 to length(chaine) do
    if (ord(chaine[i]) <> 32) then
      value := value+chaine[i];
  chaine := value;
  if length(chaine) < 8 then exit;
  SetLength(chaine,(length(chaine) div 8)*8);
  result := '';
  i := 1;
  repeat
    value := chaine[i  ]+chaine[i+1]+chaine[i+2]+chaine[i+3]+
             chaine[i+4]+chaine[i+5]+chaine[i+6]+chaine[i+7];
    value2 := 0;
    for i2 := 1 to 8 do
      begin
        if (value[i2] <> '0') and (value[i2] <> '1') then
          begin
            ShowMessage('An error occured'+#10+'You may have entered bad data');
            result := '';
            exit;
          end;
        if (value[i2] = '0') then
          value2 := value2*2
        else
          value2 := value2*2+1;
      end;
    value := IntToStr(value2);
    while (length(value) <> 3) do
      value := '0'+value;
    if (space = true) and (i <> 1) then
      value := ' '+value;
    result := result+value;
    Inc(i,8);
  until i = length(chaine)+1;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if (id = 1) then
    begin
      try
      Edit2.Text := ChrToDec(Edit1.Text, CheckBox1.Checked);
      Edit3.Text := DecToHex(Edit1.Text, CheckBox2.Checked, CheckBox4.Checked);
      Edit4.Text := DecToBin(Edit1.Text, CheckBox3.Checked);
      except
        ShowMessage('An error occured'+#10+'You may have entered bad data');
      end;
    end;
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  if (id = 2) then
    begin
      try
      Edit1.Text := DecToChr(Edit2.Text);
      Edit3.Text := DecToHex(Edit1.Text, CheckBox2.Checked, CheckBox4.Checked);
      Edit4.Text := DecToBin(Edit1.Text, CheckBox3.Checked);
      except
        ShowMessage('An error occured'+#10+'You may have entered bad data');
      end;
    end;
end;

procedure TForm1.Edit3Change(Sender: TObject);
begin
  if (id = 3) then
    begin
      try
      Edit2.Text := HexToDec(Edit3.Text, CheckBox1.Checked);
      Edit1.Text := DecToChr(Edit2.Text);
      Edit4.Text := DecToBin(Edit1.Text, CheckBox3.Checked);
      except
        ShowMessage('An error occured'+#10+'You may have entered bad data');
      end;
    end;
end;

procedure TForm1.Edit4Change(Sender: TObject);
begin
  if (id = 4) then
    begin
      try
      Edit2.Text := BinToDec(Edit4.Text, CheckBox1.Checked);
      Edit1.Text := DecToChr(Edit2.Text);
      Edit3.Text := DecToHex(Edit1.Text, CheckBox2.Checked, CheckBox4.Checked);
      except
        ShowMessage('An error occured'+#10+'You may have entered bad data');
      end;
    end;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  id := 1;
end;

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
const
  ValidChars = ['0'..'9', #8, #26, #24, #3, #22, #1, #32];
begin
  id := 2;
  if not (Key in ValidChars) then Key := #0;
end;

procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
var
  str : string;
const
  ValidChars = ['0'..'9', 'a'..'f', 'A'..'F', #8, #26, #24, #3, #22, #1, #32];
begin
  id := 3;
  str := Key;
  Key := UpperCase(str)[1];
  if not (Key in ValidChars) then Key := #0;
end;

procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);
const
  ValidChars = ['0', '1', #8, #26, #24, #3, #22, #1, #32];
begin
  id := 4;
  if not (Key in ValidChars) then Key := #0;
end;

end. 

Conclusion

Ouais, j'pense que c'est plus sympa de d'abord matter le .exe...

V'la j'ai viré les goto... :(
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

  • signaler à un administrateur
    Commentaire de Bestiol le 12/06/2003 08:19:48

    Quoi ?!!! Des goto ?!! Aucun goto ne peut être bien placé !! ;o)
    En plus, vu ou sont placés tes labels, je pense qu'un appel récursif à ta fonction ferais le même effet, sauf que ça ne ferait pas sauter tout le monde au plafond !

  • signaler à un administrateur
    Commentaire de TheWhiteShadow le 12/06/2003 08:38:20

    Voià, y'a plus de goto. J'ai fait avec des if ça marche nickel...

    Mais au fait, pkoi tout le monde il est si méchant avec les goto, j'l'ai aime bien moi ?

    Sinon, y'a d'autres choses qui clochent ??!!

  • signaler à un administrateur
    Commentaire de Bestiol le 12/06/2003 10:54:41

    Aaaaaaaaaaaaah !!!
    Tu vois qu'on peut toujours se débrouiller sans goto !

    En fait, il me semble que cette "fonction interdite" n'est restée qu'après le Turbo Pascal, qui n'était pas un langage de type événementiel... Et en plus le goto, ça fait très "Basic", et ça fait alors penser à un code VB traduit en Delphi... Or tout le monde sait que, bien que ce soit un rien ridicule, les mondes de Delphi et de VB s'adorent !

    Voilà... De toute façon, je pense que s'il y a des goto dans un code, c'est que celui a mal été pensé, car on ne devrait pas avoir à retourner en arrière dans le code, mais plutôt tout faire pour prendre le "bon chemin" dès le début !

  • signaler à un administrateur
    Commentaire de Bestiol le 12/06/2003 10:55:52

    Au fait... Pourquoi l'ajout des '00' est-il si utile ?! J'avoue que je ne vois pas !

  • signaler à un administrateur
    Commentaire de TheWhiteShadow le 12/06/2003 21:49:56

    hum le '00' quand tu cherches une chaîne dans un exe (essaye Winrar)
    par exemple : 'unregistered' la chaîne hexa n'est pas forcément
    75 6E 72 65 67 69 73 74 65 72 65 64
    elle peut être aussi
    75 00 6E 00 72 00 65 00 67 00 69 00 73 00 74 00 65 00 72 00 65 00 64
    Bof, ça dépend de comment l'exe a été compilé je pense...

    ok pour les goto, je n'en mettrai plus ;) je les banni

  • signaler à un administrateur
    Commentaire de bgK le 13/06/2003 13:57:52

    Euh quand il y a des 0 entre les caractères, on appelle ça Unicode, par opposition à Ansi

  • signaler à un administrateur
    Commentaire de Bestiol le 13/06/2003 20:16:30

    Ah oui pour les '00' je vois maintenant !! Ce n'est en fait qu'une question de format de chaine...

    Merci et bonne continuation !

  • signaler à un administrateur
    Commentaire de TurboSat le 18/06/2003 12:43:36

    Utiliser des Goto montre comme l'a bien dit Bestiol, que le code est mal pensé. Deja en TurboPascal quand on utilisait des Goto c'etait le cas. Personnelement je ne les ai jamais utilisé ni en turbo ni en delphi.

    En fait ce qui est assez desagreable et deroutant avec les goto c'est que le code part dans tous les sens, on y perd en clarté, en logique, etc...

    ++@++

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Boutique

Boutique de goodies CodeS-SourceS