binary - Get RegBinary Value as its Hex Equivalent using (Delphi XE5 ) -
for reason need registry binary value hex equivalent. found 2 guide not make work. hear registry key :
[hkey_local_machine\software\testingsoft\test path\testkey] "title"=hex:32,d6,bb,e9,b3,f0,9a,f2,37,64,65,ad,d6,c8,6a,75,9f,31
i need exact hex char shown above.
i tried read binary , convert :
procedure tform3.button22click(sender: tobject); var titleregistry: tregistry; hexstringofbinaryvalue : string; binaryvalue : array[0..200] of char; begin titleregistry:= tregistry.create(key_write or key_wow64_64key); titleregistry.rootkey := hkey_local_machine; titleregistry.openkeyreadonly('software\testingsoft\test path\testkey'); titleregistry.readbinarydata('title',binaryvalue,sizeof(binaryvalue)); hexstringofbinaryvalue := convertbinertohex(binaryvalue); showmessage( hexstringofbinaryvalue ); end;
how can make convertbinertohex function returns me string 32,d6,bb,e9,b3,f0,9a,f2,37,64,65,ad,d6,c8,6a,75,9f,31 ?
there couple of problems code.
first, using array of char. in xe5, each char 2 bytes. , each value in array hold 2 bytes binary registry value. not insurmountable problem, require convertbinertohex routine separate bytes each char. simpler, use array of byte. if you're still keen on using characters represent bytes, use array of ansichar, 1 byte each, why use characters when intention read binary data.
second, you're throwing away length of binary data. convertbinertohex routine has no way know there fewer 201 bytes of data. readbinarydata function returns number of bytes read. need capture , pass conversion routine.
once you've done that, there several ways conversion, depending on how important performance is, or how large arrays be. 1 use bintohex suggested david hefferman. downside of generates uppercase no commas. if need lowercase commas, modify result, or build string directly.
if want keep simple, code below need. loops through bytes. each iteration of loop, other first, appends comma. calls inttohex 2 characters need, , calls lowercase.
function convertbinertohex ( const nbinaryvalue : array of byte; const nlength : integer ) : string; var : integer; begin result := ''; := 0 nlength-1 begin if > 0 result := result + ','; result := result + lowercase ( inttohex ( nbinaryvalue [ ], 2 ) ); end; end; procedure tform1.button1click(sender: tobject); var titleregistry: tregistry; hexstringofbinaryvalue : string; binaryvalue : array[0..200] of byte; anumberofbytes : integer; begin titleregistry:= tregistry.create(key_write or key_wow64_64key); titleregistry.rootkey := hkey_local_machine; titleregistry.openkeyreadonly('software\testingsoft\test path\testkey'); anumberofbytes := titleregistry.readbinarydata('title',binaryvalue,sizeof(binaryvalue)); hexstringofbinaryvalue := convertbinertohex(binaryvalue, anumberofbytes);
for completeness, using bintohex. in case, stick using array of char, (or array of just-about-anything).
function convertbinertohex ( const nbinaryvalue : array of char; const nlengthinbytes : integer ) : string; var : integer; begin setlength ( result, 2 * nlengthinbytes ); if nlengthinbytes > 0 begin bintohex( @(nbinaryvalue[0]), pchar(result), length(result) ); result := lowercase ( result ); := length(result) downto 3 if odd(i) insert ( ',', result, ); end; end;
here's version using bintohex. 1 precomputes length of result, there no need reallocate string values on heap.
function lowcase(ch: widechar): widechar; begin result := ch; case ch of 'a'..'z': result := widechar(word(ch) + 32 ); end; end; function convertbinertohex ( const nbinaryvalue : array of char; const nlengthinbytes : integer ) : string; var : integer; begin setlength ( result, 3 * nlengthinbytes - 1 ); if nlengthinbytes > 0 begin bintohex( @(nbinaryvalue[0]), pchar(result), 2 * nlengthinbytes ); := nlengthinbytes downto 1 begin result [ 3 * - 1 ] := lowcase ( result [ 2 * ] ); result [ 3 * - 2 ] := lowcase ( result [ 2 * - 1 ] ); if > 1 result [ 3 * - 3 ] := ','; end; end; end;
Comments
Post a Comment