unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtnrs, OleServer, StdCtrls, ActiveX;
type
TByteDynArray = array of Byte; TBytes = TByteDynArray;
TForm2 = class(TForm)
Button1: TButton;
private
otoSafe, otoSafe2 : PSafeArray; public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
function SafeArrayAsByteArray(Arr: PSafeArray): 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;
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;
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;
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;
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;
procedure TForm2.Button2Click(Sender: TObject);
var
mibyte, prueba : TBytes;
begin
otoSafe := coderResult2.Template; mibyte := SafeArrayAsByteArray(otoSafe);
Edit1.Text:= ByteToStr(mibyte);
prueba := StrToByte(Edit1.Text); otoSafe2 := ByteArrayAsSafeArray(prueba);
end;