WideChar到字节?

时间:2013-03-13 14:31:06

标签: delphi bytearray byte widechar

我这里有一个简单的问题。如何在Delphi - 7中将WideChar转换为2x Byte?我搜索了互联网和StackOverflow,但没有结果......

2 个答案:

答案 0 :(得分:12)

David为您提供了更好的方式,即

var
  b1, b2: Byte;
  wc: WideChar;

...

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

其他一些选项(仅为了好玩):

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

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

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

var
  wc: WideChar;
  bytes: WordRec absolute wc;

begin

  // Magic! The bytes are already found in bytes.Lo and bytes.Hi!

答案 1 :(得分:7)

有很多方法可以做到这一点。例如,我个人的选择是:

var
  b1, b2: Byte;
  wc: WideChar;

....

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