Recent

Author Topic: String to Binary  (Read 1457 times)

nugax

  • Full Member
  • ***
  • Posts: 232
String to Binary
« on: April 06, 2022, 04:16:45 pm »
If i have a string say:

String := 'This is a string';

How can i change the to binary(byte) to write to a byte array?
-Nugax

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: String to Binary
« Reply #1 on: April 06, 2022, 04:36:50 pm »
So you mean getting the raw data? Easiest way is probably to simply use move:
Code: Pascal  [Select][+][-]
  1. var
  2.   arr: Array of Byte;
  3.   str: String;
  4. begin
  5.   SetLength(arr, Length(str) * SizeOf(str[1]));
  6.   Move(Str[1], arr[0], Length(arr));
  7. end;

But if you just need to use this data, without relying on the array structure (i.e. reference counted lazy copy) you could cast the address to the first element to a byte pointer and use this as array access. This avoids copying of the data.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: String to Binary
« Reply #2 on: April 06, 2022, 05:01:55 pm »
Check if length() is >0 first? Otherwise dereferencing a NIL array could happen ?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: String to Binary
« Reply #3 on: April 06, 2022, 05:16:13 pm »
Not really, because the SizeOf is at compile time and the Move parameters are (untyped) const and var so this just takes the pointers to the array elements which no problem as long as the pointers will never be dereferenced (which for length = 0 argument in move they won't).
That said, if range checks are enabled it will fire an exception so while this is a false positive, this could be annyoing so it probably would be better to check
« Last Edit: April 06, 2022, 05:19:31 pm by Warfley »

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: String to Binary
« Reply #4 on: April 06, 2022, 08:16:28 pm »
we should also add the disclaimer that there are more to strings that what meets the eye. 
Some of the older types have a length byte as the first byte and then there are the strings made of wide (16bit) characters.
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: String to Binary
« Reply #5 on: April 07, 2022, 12:38:42 am »
The great thing about dynamic strings is that they are still compatible with short strings, this is why even with dynamic strings, indexing starts at the index 1. So accessing via pointer or array notation from Str[1] to Length(str) does work on both. About the length, this is why in my example used SizeOf. That said, text is not just text, and there are different encodings and codepages, which is something one should be careful about when accessing the raw binary data of a string, because two strings that look alike might not have the same binary representation, and something that looks well in one program or on one machine, read in it's raw binary format by another application (either stored as file or transmitted to a network) not aware of the encoding, might come out as complete garbage on the other end.

Every european who tried writing a program that prints something containing a € sign probably know that struggle

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: String to Binary
« Reply #6 on: April 07, 2022, 09:04:37 am »
The great thing about dynamic strings is that they are still compatible with short strings, this is why even with dynamic strings, indexing starts at the index 1.

Except if one uses {$ZeroBasedStrings On}  ;)

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: String to Binary
« Reply #7 on: April 07, 2022, 11:04:26 am »
Interesting, didn't know about that. So this is the fixed version:
Code: Pascal  [Select][+][-]
  1. var
  2.   arr: Array of Byte;
  3.   str: String;
  4. begin
  5.   SetLength(arr, Length(str) * SizeOf(str[Low(Str)]));
  6.   if Lentgth(arr) > 0 then // to make rangechecks shut up
  7.     Move(Str[Low(Str)], arr[Low(arr)], Length(arr));
  8. end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: String to Binary
« Reply #8 on: April 07, 2022, 11:39:03 am »
Except if one uses {$ZeroBasedStrings On}  ;)

But that was for compatibility with the limitations of the nextgen compiler that has been fixed ? So there is no reason to use that :-)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: String to Binary
« Reply #9 on: April 07, 2022, 01:07:01 pm »
Except if one uses {$ZeroBasedStrings On}  ;)

But that was for compatibility with the limitations of the nextgen compiler that has been fixed ? So there is no reason to use that :-)

Reason or not, it exists and can be used.

nugax

  • Full Member
  • ***
  • Posts: 232
Re: String to Binary
« Reply #10 on: April 07, 2022, 03:06:45 pm »
move worked fine. Thank you!


So you mean getting the raw data? Easiest way is probably to simply use move:
Code: Pascal  [Select][+][-]
  1. var
  2.   arr: Array of Byte;
  3.   str: String;
  4. begin
  5.   SetLength(arr, Length(str) * SizeOf(str[1]));
  6.   Move(Str[1], arr[0], Length(arr));
  7. end;

But if you just need to use this data, without relying on the array structure (i.e. reference counted lazy copy) you could cast the address to the first element to a byte pointer and use this as array access. This avoids copying of the data.
-Nugax

 

TinyPortal © 2005-2018