Unicode code points

I needed to get a raw unicode code point in a couple languages.

# Bash

echo -e "\u2591\u2592\u2593"

You have to tell Perl that STDOUT is utf8, otherwise it will complain about "Wide character".

# Perl

binmode(STDOUT, ":utf8"); 
print "\x{2591}\x{2592}\x{2593}\n";

Here is a small PHP function to make utf-8 chars in PHP

# PHP

function utf8($num) {
    if($num<=0x7F)       return chr($num);
    if($num<=0x7FF)      return chr(($num>>6)+192).chr(($num&63)+128);
    if($num<=0xFFFF)     return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
    if($num<=0x1FFFFF)   return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
    return '';
}

print utf8(0x2591) . utf8(0x2592) . utf8(0x2593) . "\n";
Leave A Reply
All content licensed under the Creative Commons License