WideChar to Bytes?
Asked Answered
C

2

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

Cloots answered 13/3, 2013 at 14:31 Comment(0)
D
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!
Daggna answered 13/3, 2013 at 14:54 Comment(11)
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]Orebro
@Orebro 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.Selaginella
@David, I know the fields of WordRec are bytes, but it's less readable in my view!Orebro
@Orebro 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.Selaginella
@Andreas Ah well. Looks like I should have listed all the different ways!Selaginella
@David: At least I tried not to steal your green checkmark (my first paragraph)...Daggna
@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.Selaginella
@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.Daggna
@AndreasRejbrand No need to hesitate. We all know the score. We know how it works here. Thanks a lot for the edit.Selaginella
@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?Selaginella
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.Cloots
S
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;
Selaginella answered 13/3, 2013 at 14:32 Comment(1)
I won't enumerate all the other ways to do it. Would be curious to see how many truly distinct versions there are.Selaginella

© 2022 - 2024 — McMap. All rights reserved.