Press "Enter" to skip to content

Tag: java

Code Snippets: UTF-8 to UTF-16 revisited and expanded

Yes, I know I posted yesterday about converting from UTF-8 to UTF-16BE/UTF-16LE – but I wasn’t happy with the code to convert to UTF16. It relied on mb_convert_encoding which whilst clear, it did mean that sequences sent could be silently “fixed”, lone high/low surrogate code points would be refused to be output (which I actually needed as they were one thing I was trying to test against!), and I just wanted more “insight” into the whole UTF8 to UTF16 system.

Unicode is NOT UTF-8

One thing to remember (and which caused me a timesink) was that Unicode is NOT UTF-8! Unicode is a collection of characters which are usually represented in UTF-8 byte sequences (but can be represented in UTF-16BE/UTF-16LE and UTF32 and others).

Code Snippets: PHP: Converting to/from UTF-8 to UTF16-BE

I needed to convert some Unicode UTF16-BE strings (as used in Java) to UTF-8 (which is “byte-orientated” and so doesn’t need to worry about endianness) – however, there didn’t seem to many examples online. I’m not going to say these methods are robust, 100% accurate for every use case or even the best way to do it – but just ways to do it.

It’s worth noting that Java uses the escape sequence \uXXXX (where X is a hexadecimal code), and PHP uses the nearly similar escape sequence \u{XXXX} . Both seem to use \xXX for a single character.

ADDED: I’ve improved the UTF-8 to UTF-16 code conversion in a newer post.

Game: Insaniquarium

[Insaniquarium screenshot]
*sigh* It’s been ages since I’ve played the Java Insaniquarium Fishtank game. Basically, in the game (written by those folks at “Flying Bear Entertainment”) you’ve just got to feed your fishes and get them to grow up. Once they reach a certain size, they start dropping money which you can use to buy more food, buy more guppies or buy egg-shells (once you’ve got 3 pieces of egg-shell you move onto the next level). Sounds simple – but once you get onto the harder levels and you’ve got around 15 guppies, 5 carnivores, various star catchers and then 2 damn aliens come along – things begin to get slightly tricky…