Jump to content
karl Jonson

String into TArray<Byte>

Recommended Posts

Example.

 

Start a new VCL app, drop an Edit and a Button on a form.  In the click event of the button:

 

procedure TForm7.Button1Click(Sender: TObject);
var
  x:TArray<Byte>;
begin
  x  := TEncoding.Unicode.GetBytes(Edit1.Text);
  ShowMessage(IntToStr(Length(x)));
end;

 

  • Like 1

Share this post


Link to post

maybe that way be more clear and usual!  - NOTE: not valid to CharSet like Chinese text, for example = UCS2 or UCS4!

  • RAD Studio 10.3.3 Arch (RIO)
  • VCL project for test]

 

image.thumb.png.4ec2510dcb047625f9d3da145db10267.png

 

implementation

{$R *.dfm}

procedure TForm1.btn_String_To_TArrayByteClick(Sender: TObject);
var
  lMyTArrayByte: TArray<Byte>;
begin
  Edit1.Text := Trim(Edit1.Text); // elimated all spaces: left and right text!
  //
  if (Edit1.Text = EmptyStr) then
  begin
    ShowMessage('Edit.Text = empty');
    exit;
  end;
  //
  // Call BytesOf to convert a string or a character into an array of bytes.
  // Unicode strings and WideStrings are converted using the default system locale represented by the Default property of the TEncoding class.
  //
  lMyTArrayByte := BytesOf(Edit1.Text); // TEncoding.Default.GetBytes(Val);
  //
  // Call ByteLength to obtain the size of a string in bytes.
  // ByteLength calculates the size of the string by multiplying the number of characters in that string to the size of a character.
  // For example, for the string "John", ByteLength returns 8.
  //
  ShowMessage(                                                             { }
    Format('ByteLength = %d (= Total Div 2 = %d), Char=%s, WideChar=%s', [ { }
    ByteLength(Edit1.Text),                                                { }
    ByteLength(Edit1.Text) div 2,                                          { }
    Char(lMyTArrayByte[0]),                                                {WideChar(lMyTArrayByte[0]) }
    ])                                                                     {  }
    );
  //
  // Char() Represents a word-sized (16-bits) character type.
  // Char is the equivalent of the WideChar type (because the default string type is UnicodeString).
  // The implementation of Char may be changed in future releases
  //
end;

 

hug

Edited by emailx45
  • Like 1

Share this post


Link to post
6 hours ago, karl Jonson said:

Hi,

What is the best way to convert string 'ABC' into TArray<Byte> ?

TIA

Define "best". Starting with deciding which encoding to use. 

Share this post


Link to post

In my opinion:

  • "Better", it is one that works for the purpose for which it was designed, and, for now, the system in use has not yet exploded! :classic_cheerleader: 😂😂😂😂

 

// to verify:
  caption := TEncoding.Default.ToString;
  caption := TEncoding.Default.EncodingName;
  caption := TEncoding.Default.IsSingleByte.ToString(false or true); // see System.SysUtils.BoolToStr
  caption := TEncoding.Default.IsStandardEncoding(TEncoding.Unicode).ToString(false or true); // see System.SysUtils.BoolToStr
  /// etc...

 

Edited by emailx45

Share this post


Link to post
On 11/25/2020 at 6:16 PM, Darian Miller said:

procedure TForm7.Button1Click(Sender: TObject);
var
  x:TArray<Byte>;
begin
  x  := TEncoding.Unicode.GetBytes(Edit1.Text);
  ShowMessage(IntToStr(Length(x)));
end;

 

TEncoding.Unicode represents bytes in UTF-16LE format.  VERY RARELY will you ever need to use TEncoding.Unicode (or TEncoding.BigEndianUnicode) in real production code.  Most uses of Unicode<->byte conversions involve converting between Unicode and UTFs, or Unicode and charsets.  So, it is much more common to use TEncoding.UTF8, TEncoding.Default (TEncoding.ANSI on Windows, TEncoding.UTF8 on POSIX platforms), or TEncoding.GetEncoding(<charset>).

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post
2 hours ago, Remy Lebeau said:

TEncoding.Unicode represents bytes in UTF-16LE format.  VERY RARELY will you ever need to use TEncoding.Unicode (or TEncoding.BigEndianUnicode) in real production code.  Most uses of Unicode<->byte conversions involve converting between Unicode and UTFs, or Unicode and charsets.  So, it is much more common to use TEncoding.UTF8, TEncoding.Default (TEncoding.ANSI on Windows, TEncoding.UTF8 on POSIX platforms), or TEncoding.GetEncoding(<charset>).

It was a simple example and the Encoding is selectable, but it really depends on what you are going to do with those TBytes...  If you are getting it from Edit1.text as in the example, fidding with the bytes somehow, and then shoving it into Edit2.text, why convert to UTF8?  

Share this post


Link to post
3 hours ago, Darian Miller said:

If you are getting it from Edit1.text as in the example, fidding with the bytes somehow, and then shoving it into Edit2.text, why convert to UTF8?  

In that case why convert to bytes? 

 

Not sure why we are guessing. Perhaps @karl Jonson might like to contribute. 

Share this post


Link to post

Many people, especially beginners, do not know how to formulate a question (in words) to receive an adequate answer (not necessarily the most adequate, as this depends on many factors or variables). Because they want, at any cost, just to solve a problem "in loco" (on-the-post), and, they don't have a vision of the whole. But that, with study and dedication, changes.

 

I, for example, do not know:

  • Understand the meaning of texts in English (sometimes, not even in Portuguese); -- I suffer from dyslexia!
  • Understand the dialectic itself involved in the debates;
  • Understand, not even what Google brings in its translation;
  • etc...

Anyway, anyway, I try! :classic_smile:

 

So I always recommend the old saying:

  • A picture is better than 1000 words! -- Even though I need to wear glasses! :classic_biggrin:

For this reason, I always post my vision of a possible answer with my "screenshots". - Even, because, if I'm wrong, the evidence is recorded in the eyes of the beholder! :classic_ninja:

Edited by emailx45
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×