创建PHP中的UTF-16(小尾序 – BOM)文件

huangapple go评论57阅读模式
英文:

Create UTF-16 (LE - BOM) file in PHP

问题

Sure, here's the translated part:

我想在PHP中创建一个文件,满足以下条件:

  1. 生成一个文件,其中包含从输入框中接收的字符串值和16进制值并排在一起
  2. 16进制值应为UTF-16(LE)(BOM)。

更多信息:

  • 我希望通过输入框从用户那里接收值,例如姓名、姓氏、年龄和分数
  • 年龄和分数的值应转换为Hex UTF-16(BOM)并与String类型的姓名等值一起存储在文件中,文件可以具有任何扩展名,例如TXT类型。

让我举个例子:

分数 = 64
姓名 = Jack
姓氏 = Saliv
年龄 = 21
默认情况下,生成的文件如下:

64JackSaliv21
而Hex值变为如下值:
36344A61636B53616C69763231
但我想得到以下值:

䃿JackSaliカ
Hex值是:
FFFEFF404A00610063006B00530061006C00690076FFFEFF15

在以下主题中,我没有得到我想要的结果!

英文:

I want to create a file in PHP with the following conditions:

  1. Generate a file with a combination of String values received from Input Boxes and Hex values side by side
  2. Hex value should be UTF-16 (LE) (BOM).

More information:

  • I want to receive values from the user through input boxes, for example name, last name, age and score
  • The value of age and score should be converted into Hex UTF-16 (BOM) and stored with the values of name and... which are of String type in a file with any extension, it doesn't matter (for example, of TXT type).

Let me give an example:

Score = 64
Name = Jack
Last Name = Saliv
Age = 21
By default, this file is generated as follows:

64JackSaliv21
And the Hex value becomes this value:
36344A61636B53616C69763231
But I want to get the following value:

䃿JackSaliカ
And the Hex value is:
FFFEFF404A00610063006B00530061006C00690076FFFEFF15

.

In the following topics, I didn't get the results I wanted!

答案1

得分: 2

Reading through your Delph questions, along with your comments, I really don't fully understand what all this is supposed to do. However, given the input data along with the output, I can at least get pretty close, and exact with some additional corrections.

基本上,你可以使用 mb_convert_encoding 来获取 UTF-16LE,然后可以使用 unpack 将字节转换为十六进制字符串。要将十进制数转换为十六进制字符串,只需使用 dechex

从你的示例中,我不确定为什么将十进制数 64 转换为十六进制字符串 40 后,为什么会写成 FF 40 而不是 4040 00,但是 $finalStringWithNumbersPadded 变量附加了 FF。如果年龄或分数大于 255,我不太确定该怎么办,因为 FF1111 1111,已经完全填满了。

另外,我假设你在期望的字节中有一个拼写错误,姓氏的字符应该是 76 00 而不只是 76

希望代码已经说明了一切。

英文:

Reading through your Delph questions, along with your comments, I really don't fully understand what all this is supposed to do. However, given the input data along with the output, I can at least get pretty close, and exact with some additional corrections.

Basically, you can use mb_convert_encoding to get UTF-16LE, and then you can get the bytes as hex strings using unpack. To convert the decimal numbers to hex strings, just use dechex.

From your sample, I'm not certain why the decimal number 64 when converted to the hex string 40 is written as FF 40 and not just 40 or 40 00, but the $finalStringWithNumbersPadded variable has the FF appended. If the age or score were greater than 255 I'm not really sure what to do since FF is 1111 1111 and completely full.

Also, I'm assuming you have a typo in your expected bytes, the character for the last name should by 76 00 and not just 76.

Hopefully the code speaks for itself:

$score = 64;
$firstName = 'Jack';
$lastName = 'Saliv';
$age = 21;

$expected = 'FFFEFF404A00610063006B00530061006C00690076FFFEFF15';
$expectedCorrected = 'FFFEFF404A00610063006B00530061006C0069007600FFFEFF15';

// Convert to UTF-16LE and get hex, https://stackoverflow.com/a/16080439/231316
$nameAsUtf16LE = unpack('H*', mb_convert_encoding($firstName.$lastName, 'UTF-16LE', 'UTF-8'));

// UTF-16LE BOM
$utf16LeBom = 'FFFE';

// Convert to hex strings
$scoreAsHex = dechex($score);
$ageAsHex = dechex($age);

$finalString = sprintf(
    '%1$s%2$s%3$s',
    $utf16LeBom.$scoreAsHex,
    strtoupper($nameAsUtf16LE[1]), // This is 1-based, not 0-based
    $utf16LeBom.$ageAsHex,
);

$numberPadding = 'FF';
$finalStringWithNumbersPadded = sprintf(
    '%1$s%2$s%3$s',
    $utf16LeBom.$numberPadding.$scoreAsHex,
    strtoupper($nameAsUtf16LE[1]), // This is 1-based, not 0-based
    $utf16LeBom.$numberPadding.$ageAsHex,
);

echo 'Calculated         : '.$finalString;
echo PHP_EOL;
echo 'Padded             : '.$finalStringWithNumbersPadded;
echo PHP_EOL;
echo 'Expected           : '.$expected;
echo PHP_EOL;
echo 'Expected Corrected : '.$expectedCorrected;

assert($finalStringWithNumbersPadded === $expectedCorrected);

/* Output:
Calculated         : FFFE404A00610063006B00530061006C0069007600FFFE15
Padded             : FFFEFF404A00610063006B00530061006C0069007600FFFEFF15
Expected           : FFFEFF404A00610063006B00530061006C00690076FFFEFF15
Expected Corrected : FFFEFF404A00610063006B00530061006C0069007600FFFEFF15
*/

Demo: https://3v4l.org/XTdjA#v8.2.6

Edit

To write a string of hex characters to disk as binary data you can use pack:

$bindata = pack('H*', 'FFFEFF404A00610063006B00530061006C0069007600FFFEFF15');
file_put_contents('testing.data', $bindata);

huangapple
  • 本文由 发表于 2023年5月15日 00:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定