string 如何将字符串转换为字节数组并返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21442665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:13:13  来源:igfitidea点击:

How to convert strings to array of byte and back

stringdelphidelphi-xe5firemonkey-fm3

提问by Arnold

4I must write strings to a binary MIDI file. The standard requires one to know the length of the string in bytes. As I want to write for mobile as well I cannot use AnsiString, which was a good way to ensure that the string was a one-byte string. That simplified things. I tested the following code:

4我必须将字符串写入二进制 MIDI 文件。该标准要求人们知道字符串的长度(以字节为单位)。因为我也想为移动设备编写代码,所以我不能使用 AnsiString,这是确保字符串是单字节字符串的好方法。那简化了事情。我测试了以下代码:

TByte = array of Byte;

function TForm3.convertSB (arg: string): TByte;
var
   i: Int32;
begin
   Label1.Text := (SizeOf (Char));
   for i := Low (arg) to High (arg) do
   begin
      label1.Text := label1.Text + ' ' + IntToStr (Ord (arg [i]));
   end;
end; // convert SB //

convertSB ('MThd');

It returns 2 77 84 104 100 (as label text) in Windows as well as Android. Does this mean that Delphi treats strings by default as UTF-8? This would greatly simplify things but I couldn't find it in the help. And what is the best way to convert this to an array of bytes? Read each character and test whether it is 1, 2 or 4 bytes and allocate this space in the array? For converting back to a character: just read the array of bytes until a byte is encountered < 128?

它在 Windows 和 Android 中返回 2 77 84 104 100(作为标签文本)。这是否意味着 Delphi 默认将字符串视为 UTF-8?这将大大简化事情,但我在帮助中找不到它。将其转换为字节数组的最佳方法是什么?读取每个字符并测试它是 1、2 还是 4 个字节并在数组中分配这个空间?转换回字符:只需读取字节数组,直到遇到小于 128 的字节?

回答by David Heffernan

Delphi strings are encoded internally as UTF-16. There was a big clue in the fact that SizeOf(Char)is 2.

Delphi 字符串在内部编码为 UTF-16。有一个重要的线索,那SizeOf(Char)就是 2。

The reason that all your characters had ordinal in the ASCII range is that UTF-16 extends ASCII in the sense that characters 0 to 127, in the ASCII range, have the same ordinal value in UTF-16. And all your characters are ASCII characters.

您的所有字符都在 ASCII 范围内具有序数的原因是 UTF-16 扩展了 ASCII,因为字符 0 到 127(在 ASCII 范围内)在 UTF-16 中具有相同的序数值。你所有的字符都是 ASCII 字符。

That said, you do not need to worry about the internal storage. You simply convert between string and byte array using the TEncodingclass. For instance, to convert to UTF-8 you write:

也就是说,您无需担心内部存储。您只需使用TEncoding该类在字符串和字节数组之间进行转换。例如,要转换为 UTF-8,您可以这样写:

bytes := TEncoding.UTF8.GetBytes(str);

And in the opposite direction:

而在相反的方向:

str := TEncoding.UTF8.GetString(bytes);

The class supports many other encodings, as described in the documentation. It's not clear from the question which encoding you are need to use. Hopefully you can work the rest out from here.

该类支持许多其他编码,如文档中所述。从您需要使用哪种编码的问题中不清楚。希望你能从这里解决剩下的问题。