0
$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);

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.

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?

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..

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

J Zersche
  • 25
  • 1
  • 10

1 Answers1

2

%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.

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); // ☃

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).

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    So I was too specific in saying "decimal"? Oh well, I thought it'd make it more understandable since decimal, hexadecimal and binary are all mixed in this post. – deceze Dec 15 '14 at 04:12
  • How could I encode this in to UTF-16 (Dec) // Snowman = 9,731 DEC UTF 16? – J Zersche Dec 15 '14 at 08:01
  • I understand now, printf('%c, %x, %d, %b', 65 ,65, 65, 65); But if what you were saying that %c converts it to binary, I thought %b did that. && How about the significance of the 0x? Is it necessary to add the 0x infront of the HEX or is that only done in the condition that PHP is expecting a DECIMAL value, due to writing %c? If you wrote it was E2, 98, 83 with a %x for HEX would that work? I see now that the 0x is necessary to indicate to PHP that the integer is HEXADECIMAL, as writing a letter PHP will not interpret it as a number due to the letters used in HEXADECIMAL values.. – J Zersche Dec 15 '14 at 08:31
  • 1
    @JZe `226` and `0xE2` are exactly the same in PHP, just different notations. They both result in the same integer. – deceze Dec 15 '14 at 09:42
  • @JZe Snowman in UTF-16 is `printf('%c%c%c%c', 0xFE, 0xFF, 0x26, 0x03)`. – deceze Dec 15 '14 at 09:44
  • @JZe `%b` outputs "11010101101" binary representation as text, `%c` outputs actual binary data. – deceze Dec 15 '14 at 09:51