Article 6G1EC CodeSOD: UTF-16 Encoding

CodeSOD: UTF-16 Encoding

by
Remy Porter
from The Daily WTF on (#6G1EC)

Let's say you were browsing through some code, and saw a function signature like this:

function changeUTF16( $string )

What do you think that function does? Given the name, I suspect it converts a UTF-16 string into some other encoding, or perhaps it changes some other encoding to UTF-16. Either way, the name is fairly unclear, and probably could be better.

P-O sent this in, and let's take a look at what the function actually does.

///////////////////////////////////////////////////////////////////////////////////////// changeUTF16($string)// method called to convert LDAP string from UTF-8 to UTF-16//// IN: $string// OUT: returns a UTF-8 string as a UTF-16 one//// NOTE : in fact, this method returns the UTF-8 chain converted in ISO///////////////////////////////////////////////////////////////////////////////////////function changeUTF16( $string ){ return mb_convert_encoding( $string, "ISO-8859-1", "UTF-8" )."\n" ;}

Thank the gods for that NOTE there, which awkwardly describes what the function actually does- it converts a UTF-8 string to an ISO-8859-1 string. ISO-8859 is, notably, not UTF-16, and in fact, isn't 16 bits at all- it's just a different 8-bit encoding from UTF-8.

Clearly, none of this ever mattered, because the program worked, but equally clearly the person behind this method had no clue what character encodings actually meant.

P-O did find that converting this function to a no-op had no impact on the application.

buildmaster-icon.png [Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments