Jump to content
RTollison

Need to convert varbinary to hex string in a dll

Recommended Posts

i am writing a dll the returns a pansichar value but on the sql table it is a varbinary of the hex value

0x3030313230303631303000

i need to return that thru the dll as a hex string of

3030313230303631303000

then in my calling program i can convert it from hex to ascii string

if it is possible to convert it to ascii string in the dll that is fine but i am not sure how to convert it in the dll from varbinary to anything usable at this point.

Share this post


Link to post

My dll is being called by a cobol program(acucobol) if i try to pass back the binary (stdcall) it thinks i am null terminated for

0x0000000361000000000000

the 00 comes back null and my string is blank in my cobol program

Share this post


Link to post

the sql column has this info

0x303031323030363130300000000000

0x000000036100000000000000000000

0x303031323030363130303031000000

 

by using the bintohex it works on the first and third row but the 2nd row is coming back blank.

the info after 0x is exactly what i want to get back in my cobol program. as long as it looks like the string shown.

Share this post


Link to post

this is what i did to get the odd ball 2nd row but i am concerned that i might be overlooking something that could cause it to not be correct...

function bintoAscii(const bin: array of byte): AnsiString;
var i: integer;
begin
  SetLength(Result, Length(bin));
  for i := 0 to Length(bin)-1 do
    Result[1+i] := AnsiChar(bin);
end;

function bintostr(const bin: array of byte): string;
const HexSymbols = '0123456789ABCDEF';
var i: integer;
begin
  SetLength(Result, 2*Length(bin));
  for i :=  0 to Length(bin)-1 do begin
    Result[1 + 2*i + 0] := HexSymbols[1 + bin shr 4];
    Result[1 + 2*i + 1] := HexSymbols[1 + bin and $0F];
  end;
end; 

.....

 

      _KeyValue := bintoAscii(dmspCreate.adsAcuLocks1.FieldbyName('KeyValue').AsBytes);
      if (_keyValue[1] = #0) then
        _keyValue := bintoStr(dmspCreate.adsAcuLocks1.FieldbyName('KeyValue').AsBytes); 

 

 

Share this post


Link to post

BinToHex should work, but you can give this a try:

Function MyBinToHex(inBinary: TArray<Byte>): String;
Var
 b: Byte;
Begin
 Result := '0x';
 For b In inBinary Do
  Result := Result + IntToHex(Ord(b), 2);
End;

..,in older Delphi's:

Function MyBinToHex(inBinary: Array Of Byte): String;
Var
 a: Integer;
Begin
 Result := '0x';
 For:= Low(inBinary) To High(inBinary) Do
  Result := Result + IntToHex(Ord(b), 2);
End;

 

Share this post


Link to post
13 minutes ago, RTollison said:

      _KeyValue := bintoAscii(dmspCreate.adsAcuLocks1.FieldbyName('KeyValue').AsBytes);
      if (_keyValue[1] = #0) then
        _keyValue := bintoStr(dmspCreate.adsAcuLocks1.FieldbyName('KeyValue').AsBytes);  

The first and the third example starts with 0x30 so bintoStr will not be called, but the byte array seems to be binary; not ASCII. If you are sure that the field always contains binary data, drop BinToAscii and call BinToStr only.

Share this post


Link to post

this is from another partys file. (micro focus acucobol) they have a lock file that will let me know what record id is in use already.

via a sample program that is using said lock table as a vision file (non sql which we do not do because we wanted all tables to sql if possible)

they have an example program that uses this to convert the data to hex format for display purposes. if i could get the raw varbinary data thru the dll i am writing then i could do the same thing.

                   perform varying idx from 1 by 2 until idx > 244
                       call "ascii2hex" using
                            lockfile-keyvalue(idx:2),
                            list_keyval(2 * idx - 1:4)
                   end-perform


the library routine ascii2hex

ASCII2HEX converts binary data to its hexadecimal format. This routine is the inverse of the HEX2ASCII routine.

Usage

CALL ""
    USING ASCII-VALUE, HEX-VALUE

Parameters

ASCII-VALUE PIC X(2)    The input data area containing the ASCII representation of a unit of data.
HEX-VALUE PIC X(4) The output data area to contain the hexadecimal value.

When you define the parameters, use the exact field sizes specified in the calling conventions above, otherwise the runtime may terminate abnormally.

Share this post


Link to post

by the way thanks this has solved my problem and i am continuing on with my dll.

i used the mybintohex solution.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×