Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WideChar to Bytes?

I have simple question here. How to convert WideChar to 2xByte in Delphi - 7? I searched the internet and the StackOverflow but with no results...

like image 736
Little Helper Avatar asked Mar 13 '13 14:03

Little Helper


2 Answers

David gave you the preferable way, namely,

var
  b1, b2: Byte;
  wc: WideChar;

...

b1 := WordRec(wc).Lo;
b2 := WordRec(wc).Hi;

A few other options (just for fun):

b1 := Lo(Word(wc));
b2 := Hi(Word(wc));

and

b1 := Byte(wc);
b2 := Byte(Word(wc) shr 8);

and

b1 := PByte(@wc)^;
b2 := PByte(NativeUInt(@wc) + 1)^;

and

var
  wc: WideChar;
  bytes: WordRec absolute wc;

begin

  // Magic! The bytes are already found in bytes.Lo and bytes.Hi!
like image 137
Andreas Rejbrand Avatar answered Oct 10 '22 12:10

Andreas Rejbrand


Lots of ways to do this. For example my personal choice would be:

var
  b1, b2: Byte;
  wc: WideChar;

....

b1 := WordRec(wc).Lo;
b2 := WordRec(wc).Hi;
like image 37
David Heffernan Avatar answered Oct 10 '22 13:10

David Heffernan