如何将字符串转换为字节和数组 [英] How to convert strings to array of byte and back

查看:174
本文介绍了如何将字符串转换为字节和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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');

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

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?

推荐答案

Delphi字符串内部编码为UTF-16。有一个很大的线索,事实上, SizeOf(Char)是2.

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

所有的人物的原因在ASCII范围内的顺序是UTF-16扩展ASCII,这意味着ASCII范围内的字符0到127在UTF-16中具有相同的序数值。所有的字符都是ASCII字符。

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.

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

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

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

而且反向:

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.

这篇关于如何将字符串转换为字节和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆