1

First of all I am sorry that I cannot better to describe my problem.

What I have is Word number 65025 which is 0xFE01 or 11111110 00000001 in binary. And I want to pass the value to wstr Word => 11111110 00000001.

I found that using typecast does not work.

And one more question here. If I want to add another number like 10000 => 0x03E8 how to do it. So in the result the widestring should refer to values 0xFE01 0x03E8.

And then, how to retrieve the same numbers from widestring to word back?

var wstr: Widestring;
wo: Word;
begin
  wo := 65025;
  wstr := Widestring(wo);
  wo := 10000;
  wstr := wstr + Widestring(wo);
end

Edit:

I'm giving another, simpler example of what I want... If I have word value 49, which is equal to ASCII value 1, then I want the wstr be '1' which is b00110001 in binary terms. I want to copy the bits from word number to the string.

John Boe
  • 3,501
  • 10
  • 37
  • 71
  • 1
    Try to google with int to string binary pascal delphi – Zamrony P. Juhara Jul 07 '18 at 01:04
  • @Zamrony P. Juhara: is it possible to copy all 8 or 16 bites on one go? I found example which copies bits in a loop which has 8 steps. – John Boe Jul 07 '18 at 01:19
  • 1
    Yes you can using pass 8 bit binary string as 16 bit binary string by putting zeroes on high bits – Zamrony P. Juhara Jul 07 '18 at 01:23
  • So your question is how to convert an integer to its binary representation? – David Heffernan Jul 07 '18 at 03:26
  • @David Heffernan: I want to copy bits from word to widestring. – John Boe Jul 07 '18 at 07:23
  • Nobody understands what you want to do. It's not that we aren't understanding you. You aren't explaining yourself. – David Heffernan Jul 07 '18 at 07:43
  • @David Heffenan: I have described it in binary terms: And I want to pass the value to wstr Word => 11111110 00000001. I want to copy the bits from word to wstr. So the first two chars of wstr should have the binary value as I discribed. The result is not '1111111000000001' but binary 1111111000000001 or in the hexadecimal: 0xFE01 0x03E8 ... Or another example if word is 49, then the wstr must be '1' because ascii code for '1' is 49 or b00110001 – John Boe Jul 07 '18 at 08:03
  • OK, that's clear now. I have given you the answer. – David Heffernan Jul 07 '18 at 08:34

2 Answers2

3

It looks like you want to interpret a word as a UTF-16 code unit. In Unicode Delphi you would use the Chr() function. But I suspect you use an ANSI Delphi. In which case cast to WideChar with WideChar(wo).

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

You are casting a Word to a WideString. In Delphi, casting usually doesn't convert, so you are simply re-interpreting the value 65025 as a pointer (a WideString is a pointer). But 65025 is not a valid pointer value.

You will have to explicitly convert the Word to a WideString, e.g. with a function like this (untested, but should work):

function WordToBinary(W: Word): WideString;
var
  I: Integer;
begin
  Result := '0000000000000000';
  for I := 0 to 15 do // process bits 0..15
  begin
    if Odd(W) then
      Result[16 - I] := '1';
    W := W shr 1;
  end;
end;

Now you can do something like:

wo := 65025; 
wstr := WordToBinary(wo);
wo := 10000;
wstr := wstr + ' ' + WordToBinary(wo);

For the reverse, you will have to write a function that converts from a WideString to a Word. I'll leave that exercise to you.

Again, you can't cast. You will have to explicitly convert. Both ways.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Unfortunately you did not understand me. In the example you should copy the 16 bits from number word to widestring. I was just looking for the correct therms. Something like this pseudocode: ` function WordToBinary(num: Word): WideString; begin for i:=0 to 15 do Result := num shr 1; end; ` But this is generating an error...[Error] Incompatible types: 'WideString' and 'Integer' – John Boe Jul 07 '18 at 07:40
  • see you accepted David's answer. If that is what you want, then you should have said so. The mention of the binary values and the cast to WideString very much confused things. Simply do `wstr := WideChar(65025) + WideChar(10000);`. – Rudy Velthuis Jul 07 '18 at 10:53
  • Yes. This all the stuff :-) – John Boe Jul 07 '18 at 11:00