February 19th, 2019

How do I convert all these strange Unicode digits into the ones I remember from Sesame Street?

Suppose you have a Unicode string and you want to do something mathematical with it. You know how to parse DIGIT ZERO “0” through DIGIT NINE “9” but the string may contain Unicode digits like ARABIC-INDIC DIGIT SIX “٦” or DEVANAGARI DIGIT SEVEN ““, and you want to support those too.

Help here comes from the Fold­String function.

int result = FoldString(MAP_FOLDDIGITS, originalString, -1,
                        resultBuffer, resultBufferSize);

You give the Fold­String function an input buffer, either with an explicit character count, or with -1 to indicate that you want to process up to and including the null terminator. You also give it an output buffer. And you tell it what kind of conversion you want to perform. In our case, we ask for MAP_FOLD­DIGITS, which means to convert all decimal digits to DIGIT ZERO “0” through DIGIT NINE “9”.

Now you can do your magic mathematical thing with a known number representation.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.