printf()扩展Unicode字符? [英] printf() Extended Unicode Characters?

查看:275
本文介绍了printf()扩展Unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$formatthis = 219;
$printthis = 98;
// %c - the argument is treated as an integer, and presented as the character with 
        that ASCII value.

$string = 'There are %c treated as integer %c';
echo printf($string, $formatthis, $printthis);

我试图了解printf(). 我不太了解这些参数.

I'm attempting to understand printf(). I don't quite understand the parameters.

我可以看到第一个参数似乎是要应用格式设置的字符串.

I can see that the first parameter seems to be the string that the formatting will be applied to.

第二个是要格式化的第一个变量,第三个似乎是要格式化的第二个变量.

The second is the first variable to format, and the third seems to be the second variable to format.

我不了解的是如何获取它来打印特殊的unicode字符. 例如.除了a-z,A-Z之外,!@#$%^& *(){}"ETC.

What I don't understand is how to get it to print unicode characters that are special. E.G. Beyond a-z, A-Z, !@#$%^&*(){}" ETC.

为什么还要将它与字符串中最后一个引号的位置放在一起?

Also, why does it out put with the location of the last quote in the string?

输出: 有``被视为整数``32

OUTPUT: There are � treated as integer �32

How could I encode this in to UTF-16 (Dec) // Snowman = 9,731 DEC UTF 16? 

UTF-8 'LATIN CAPITAL LETTER A' (U+0041) = 41, but if I write in PHP 41 I will get ')' I googled     an ASCII table and it's showing that the number for A is 065...

ASCII is a subset of UTF-8, so if a document is ASCII then it is already UTF-8

If it's already in UTF-8, why are those two numbers different? Also the outputs different..

编辑,好的,所以我正在查看的图表显然显示的是十六进制值中的数字,我没有立即注意到,十六进制中的41是ASCII 065

EDIT, Okay so the chart I'm looking at is obviously displaying the digits in HEX value which I didn't immediately notice, 41 in HEX is ASCII 065

推荐答案

%c基本上是一个int2bin函数,这意味着它将数字格式化为二进制表示形式.最高到十进制数字255,将作为字节0xFF输出.

%c is basically an int2bin function, meaning it formats a number into its binary representation. This goes up to the decimal number 255, which will be output as the byte 0xFF.

要输出例如雪人字符☃,您需要输出在选择的编码中表示它所需的确切字节.如果选择UTF-8对其进行编码,则必需的字节为E2 98 83:

To output, say, the snowman character ☃, you'd need to output the exact bytes necessary to represent it in your encoding of choice. If you chose UTF-8 to encode it, the necessary bytes are E2 98 83:

printf('%c%c%c', 226, 152, 131); // ☃
// or
printf('%c%c%c', 0xE2, 0x98, 0x83); // ☃

在您的情况下的问题是1)您输出的字节在您将结果解释为的编码中没有任何含义(意味着98的字节在UTF-8中并不意味着任何内容)在这一点上,这就是为什么您看到一个"..."和2)您正在echo的结果printf的结果,该结果输出32(printf返回其输出的字节数).

The problem in your case is 1) that the bytes you're outputting don't mean anything in the encoding you're interpreting the result as (meaning the byte for 98 doesn't mean anything in UTF-8 at this point, which is why you're seeing a "�") and 2) that you're echoing the result of printf, which outputs 32 (printf returns the number of bytes it output).

这篇关于printf()扩展Unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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