英文:
Create UTF-16 (LE - BOM) file in PHP
问题
Sure, here's the translated part:
我想在PHP中创建一个文件,满足以下条件:
- 生成一个文件,其中包含从输入框中接收的字符串值和16进制值并排在一起
- 16进制值应为UTF-16(LE)(BOM)。
更多信息:
- 我希望通过输入框从用户那里接收值,例如姓名、姓氏、年龄和分数
- 年龄和分数的值应转换为Hex UTF-16(BOM)并与String类型的姓名等值一起存储在文件中,文件可以具有任何扩展名,例如TXT类型。
让我举个例子:
分数 = 64
姓名 = Jack
姓氏 = Saliv
年龄 = 21
默认情况下,生成的文件如下:
64JackSaliv21
而Hex值变为如下值:
36344A61636B53616C69763231
但我想得到以下值:
䃿JackSaliカ
Hex值是:
FFFEFF404A00610063006B00530061006C00690076FFFEFF15
在以下主题中,我没有得到我想要的结果!
- https://stackoverflow.com/questions/19593637/how-do-i-write-utf-8-data-to-a-utf-16le-file-using-php
- https://stackoverflow.com/questions/23848601/encode-file-in-utf-16le-in-php-with-byte-order-mask-bom
- https://stackoverflow.com/questions/48099897/php-save-file-as-utf-8-without-bom
英文:
I want to create a file in PHP with the following conditions:
- Generate a file with a combination of String values received from Input Boxes and Hex values side by side
- 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
而不是 40
或 40 00
,但是 $finalStringWithNumbersPadded
变量附加了 FF
。如果年龄或分数大于 255
,我不太确定该怎么办,因为 FF
是 1111 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论