Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   convertir hex a Byte (https://www.clubdelphi.com/foros/showthread.php?t=73184)

Lizeth 06-04-2011 02:03:47

convertir hex a Byte
 
Hola espero me puedan ayudar. Tengo una cadena en hexadecimal así:
Value = 0:10:8:0:E0:6:0:0:2:10:8:0:4:0:0:0 (supongamos)

Quiero extraer cada elemento y convertirlo a byte.
Se que debo primero saber el Length(Value) que es variable, luego el pos de cada ":" y hacer un for que solo me tome los valores entre cada ":" y luego convertir ese valor ejem "E0" a byte. y al final meterlo en un array of bytes.

Pero solo tengo la idea no se como hacerlo.

Gracias

Keiso 06-04-2011 03:29:28

Para la primera parte podría servirte este procedimiento:
Código Delphi [-]
procedure CommaTextToStrs( AStrs: TStrings;const Value: string ;const AchDelim : Char );
var
 P, P1     : PChar;
 S         : string;
 chDelim   : char ;
begin
 chDelim := AchDelim ;
 AStrs.BeginUpdate;
 try
  AStrs.Clear;
  P := PChar(Value);
  while P^ in [#1..' '] do
   P := CharNext(P);
  while P^ <> #0 do
   begin
    if ( P^ = '"' ) then
     S := AnsiExtractQuotedStr(P, '"')
    else
     begin
      P1 := P;
      while (P^ > ' ') and ( P^ <> chDelim ) do
       P := CharNext(P);
      SetString(S, P1, P - P1);
     end;
    AStrs.Add(S);
    while P^ in [#1..' '] do
     P := CharNext(P);
    if P^ = chDelim then 
     repeat
      P := CharNext(P);
     until not (P^ in [#1..' ']);
    end;  // while
  finally
   AStrs.EndUpdate;
  end;
end;

Su uso sería el como sigue:
Código Delphi [-]
var
Strs1: TStringList; // Donde estarán guardados tus HEX individuales
inStr: String;        // Tus HEX agrupados 0:10:8:0:E0:6:0:0:2:10:8:0:4:0:0:0

CommaTextToStrs( Strs1, inStr, ':' );
para lo segundo, supongo que con byte te refieres en decimales, esto es lo primero que encontré:

http://www.greatis.com/delphicb/tips...s-hex2dec.html

o para cambiar entre cualquier base este es un buen componente:

http://wenwen.soso.com/z/q156828058.htm

ecfisa 06-04-2011 04:01:58

Hola Lizeth.

Código Delphi [-]
...
type
  TArrayOfByte= array of Byte;

procedure HexaToByte(Cadena: string; var Vec: TArrayOfByte);
var
  i,p: Integer;
  s: string;
begin
  if Cadena[1] = ':' then
    Delete(Cadena, 1, 1);
  if Cadena[Length(Cadena)] <> ':' then
    Cadena:= Cadena + ':';
  i:= 1;
  repeat
    p:= Pos(':', Cadena);
    s:= Copy(Cadena, 1, p-1);
    if p > 0 then
    begin
      SetLength(Vec, i+1);
      try
        Vec[i]:= StrToInt('$'+ s);
      except
        raise Exception.Create('Error, ( '+s+' ) no es un número hexadecimal');
      end;
      Inc(i);
      Delete(Cadena, 1, p);
    end;
  until p = 0;
end;
...

Llamada:
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
var
  Vec: TArrayOfByte;
  i: Integer;
begin
  HexaToByte('01:02:03:04:0C:0A:0B:FF:40',Vec);
  (* Mostrar resultado en un Memo  *)
  Memo1.Clear;
  for i:= Low(Vec) to High(Vec) do
    Memo1.Lines.Add(IntToStr(Vec[i]))
end;

Un saludo.

Delfino 06-04-2011 15:35:54

Código Delphi [-]
function TextToBuffer(Command: string): TBytes;
  var I: Integer;
      slBuffer: TStringList;
begin
 slBuffer := TStringList.Create;
 try
  slBuffer.Delimiter := ':';
  slBuffer.DelimitedText := Command;
  SetLength(Result, slBuffer.Count);
  for I := 0 to slBuffer.Count - 1 do
    Result[i] := StrToInt('$' + slBuffer[i]);
 finally
  slBuffer.Free;
 end;
end;

Lizeth 07-04-2011 20:23:41

Gracias me han ayudado mucho, gracias gracias


La franja horaria es GMT +2. Ahora son las 09:37:06.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi