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

Raymond Chen

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.

0 comments

Discussion is closed.

Feedback usabilla icon