简体   繁体   中英

Array of Byte value in Delphi

Hello I am studying an internet code , and called my attention this part :

// ~ Your Dll Here ~ Ex : 'C:\MS10046.dll'
  SizeNameDll : integer = 28;
  Dllx  : ARRAY [1..28] OF Byte =  ($00,$43,$00,$3A,$00,$5C,$00,$4D,$00,$53,$00,$31,$00,$30,$00,$30,$00,$34,$00,$36,$00,$2E,$00,$64,$00  ,$6C,$00,$6C);

Supposedly the contents of the array has the value of "C: \\ MS10046.dll " I do not understand two things , why SizeNameDll value is 28 and this is not the exact length of "C: \\ MS10046.dll "? How I can change the value of " C: \\ MS10046.dll " to all code by another route such as " c: /xampp/test.dll "?

Could someone help me?

The bytes represent the text 'C:\\MS10046.dll' in big endian UTF-16 Unicode. In UTF-16, "characters" (or correctly called: code points) are made up of 16 bit wide code units, or in Delphi, WideChar s. So each code unit is two bytes in size. In little-endian, the default for Intel-based platforms, this means that 'C' is encoded as $0043 , which is a byte $43 , followed by a byte $00 . In big-endian, this is reversed, so 'C' (or $0043 ) is $00 followed by $43 . The same for ':' : this is $3A,$00 in little-endian, but $00,$3A in big-endian. So 14 "characters" result in the array you showed.

Since the array seems to start with a $00 , this must be big-endian.

If the function you must use requires an array of bytes and a length, then you should probably call it like:

var
  Dllx: TBytes;
  str: string;
begin
  str := 'c:\xampp\test.dll'; // I removed the spaces - they are wrong
  Dllx := TEncoding.BigEndianUnicode.GetBytes(str);
  if Length(Dllx) > 0 then
    YourFunction(... PByte(Dllx), Length(Dllx), ...);

It would help if you showed how the array is supposed to be used, IOW, show a little more code. It is highly unusual that a DLL on Windows requires big-endian encoding.

It could well be that, if big-endian is not required at all, that you could do without the conversion:

if Length(str) > 0 then
  YourFunction(..., PChar(str), Length(str) * Sizeof(WideChar), ...);

or even (it all depends on the declaraton of the function):

if Length(str) > 0 then
  YourFunction(..., PChar(str), Length(str), ...);

The latter looks far more likely, but without more information, this is all guesswork, sorry.

These 28 bytes represent your 14 character string encoded as UTF-16BE. Each character is represented by a 16 bit character element. That's why a string of length 14 consumes 28 bytes.

To encode a general string as UTF-16BE you would write:

var
  Bytes: TBytes;
....
Bytes := TEncoding.BigEndianUnicode.GetBytes(str);

where str is your string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM