6

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...

Little Helper
  • 2,419
  • 9
  • 37
  • 67

2 Answers2

12

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!
Community
  • 1
  • 1
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 2
    I would prefer to cast char to the native data type of the `Lo` and `Hi` function parameters, thus I would use `Lo(Integer(wc));` and the same for `Hi`. The absolute directive *magic* is the simplest in my view, but I would use `bytes: array[0..1] of Byte absolute wc;` as this question asked for bytes, not for the `WordRec`. [+1ed] – TLama Mar 13 '13 at 15:10
  • @TLama What type do you think the fields of WordRec are? Also, Lo and Hi are a bit weird. They accept 32 bit integers, but ignore the most significant 16 bits. They are a hang over from 16 bit. – David Heffernan Mar 13 '13 at 16:55
  • @David, I know the fields of `WordRec` are bytes, but it's less readable in my view! – TLama Mar 13 '13 at 16:58
  • @TLama OK I see what you mean. Advantage of Hi and Lo members is that you know which way round they are. I really don't like those intrinsic Lo and Hi 16 bit functions though. Too weird for words. Also massively error prone due to implicit type conversion. Just my view though. – David Heffernan Mar 13 '13 at 17:03
  • @Andreas Ah well. Looks like I should have listed all the different ways! – David Heffernan Mar 17 '13 at 20:01
  • @David: At least I *tried* not to steal your green checkmark (my first paragraph)... – Andreas Rejbrand Mar 17 '13 at 20:04
  • @Andreas I know. I think it's a shame that the accepted answer doesn't include the cleanest way to solve the problem. I'd be very happy if you edited to include the WordRec cast. For the benefit of future readers. – David Heffernan Mar 17 '13 at 20:11
  • @David: With some hesitation, I included the `WordRec` cast. But I still make it clear that you were the first one to give that solution. – Andreas Rejbrand Mar 17 '13 at 20:14
  • @AndreasRejbrand No need to hesitate. We all know the score. We know how it works here. Thanks a lot for the edit. – David Heffernan Mar 17 '13 at 20:20
  • @AndreasRejbrand I want to stress that I've no problem at all with the accept here. You put more effort in than I did. Why should you not get the reward? – David Heffernan Mar 17 '13 at 20:43
  • Wow, now that's hell of a lot of comments. By the way I accepted this answer because it has a lot of ways how to convert WideChar to Bytes. Thanks guys. – Little Helper Mar 17 '13 at 21:14
8

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;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490