Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #10  
Antiguo 12-04-2011
Lizeth Lizeth is offline
Miembro
 
Registrado: ago 2005
Ubicación: Mexico D.F
Posts: 50
Poder: 19
Lizeth Va por buen camino
Hola, bueno pues despúes de muchas semanas resolvimos el problema. Es en delphi 7. He aqui una posible solución para pasar el contenido de un puntero de un PSafeArray a String en hexadecimal y de String en hexadecimal a PSafeArray. Espero le sirva a alguien.

Código Delphi [-]
unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, OleServer, StdCtrls, ActiveX; // Activex más lo que se necesite
  
type
   TByteDynArray = array of Byte;//Declaro mi tipo TBytes 
   TBytes = TByteDynArray;
  TForm2 = class(TForm)   
    Button1: TButton;
    //...
  private
    { Private declarations }
    otoSafe, otoSafe2 : PSafeArray; //Mi variables tipo PSafeArray; 
  public
    { Public declarations }  
  
  end;
var
  Form2: TForm2;
implementation

{$R *.dfm}
 //-------------------------- funciones propias ------------------
function SafeArrayAsByteArray(Arr: PSafeArray): TBytes;
// Convierto de PSafeArray a TBytes
var
 LBound, UBound, i: Integer;
begin
 SafeArrayGetLBound(Arr, 1, LBound);
 SafeArrayGetUBound(Arr, 1, UBound);
 SetLength(result, UBound-LBound+1);
 for i := LBound to UBound do 
   SafeArrayGetElement(Arr,i,result[i]); 
end;

function ByteToHex(InByte: byte): shortstring;
// Pasa de Byte a un shortstring en hexadecimal
const Digits:array[0..15] of char='0123456789ABCDEF';
begin
   result:=digits[InByte shr 4] + digits[InByte and $0F];
end;

function ByteToStr(const Value: TBytes): String;
// Paso de Byte a String
var
    I: integer;
    S, CR : String;
    Letra: char;
begin
    S := '';
    for I := 0 to Length(Value)-1 do
     begin
        CR:= ByteToHex(Value[i])+':';
        S := S+CR;
    end;
    Result := S;
end;
function StrToByte(const Value: String): TBytes;
// Hacemos la inversa de mi string a TByte 
var
   I,j: integer;
   iPos  : integer;
   Hex : TBytes;
   sCaden, sCopia : string;
begin
   SetLength(Result, Length(Value)div 3);
    j:= 0;
    for I := 1 to Length(Value) - 1 do
    begin
      if Value[i] = ':' then
       begin
         Result[j]:= strToInt('$'+ sCaden);
         sCaden := '';
         inc(j);
       end
      else
       sCaden:= sCaden + Value[i];
    end;
end;
function ByteArrayAsSafeArray(Arr: TBytes): PSafeArray;
// La inversa de TBytes a PSafeArray;
var
  pvData: PByteArray;
  Bounds: TVarArrayBoundArray;
begin
   Bounds[0].LowBound:=0;
   Bounds[0].ElementCount:=Length(Arr);
   Result := SafeArrayCreate(varByte, 1, Bounds);
   SafeArrayAccessData(Result, Pointer(pvData));
   Move(Arr[0], pvData[0], Length(Arr));
end;
//--------------------------------------- funciones <-------------------------------
// Como las uso
procedure TForm2.Button2Click(Sender: TObject);
var 
   mibyte, prueba : TBytes;  
begin
  
    otoSafe   := coderResult2.Template; // yo tengo esto que es de tipo PSafeArray;
    mibyte    := SafeArrayAsByteArray(otoSafe);  
    Edit1.Text:= ByteToStr(mibyte);
    prueba    := StrToByte(Edit1.Text);//la inversa
    otoSafe2  := ByteArrayAsSafeArray(prueba);
    
end;
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
convertir string a Twwdbgrid Nieto OOP 1 03-12-2007 19:32:41
convertir string jsanchez Varios 3 31-07-2006 22:23:19
Meter un Array de strings con Datos en un PSafearray camomilass OOP 1 28-06-2006 12:09:39
convertir un String en TTreeNode jmlifi Varios 3 26-08-2005 15:56:01
Convertir un Tfield a String Ricsato Varios 2 14-09-2004 19:51:50


La franja horaria es GMT +2. Ahora son las 19:07:18.


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
Copyright 1996-2007 Club Delphi