How to convert a string that contains superscript (m²) to a regular string like m2 in C ++

In my project, I need to convert a string that contains a superscript - m² to a string m2.

My project takes a unit of measure that includes a square meter (m²) or meter (m³). And I need to convert superscripts to a normal integer or string in order to continue processing the input. However, at this point, I cannot find anything in C ++ that does this for me.

The app is in C ++ and we are using CComBSTR to store the string.

The ideal output would be m2 for m2 and m3 for m3, etc.

Any suggestions

+3


source to share


2 answers


A CComBSTR

is just a wrapper for BSTR

. This, in turn, is this WCHAR*

, which maps to a C ++ type wchar_t*

. Since you are on Windows, you should know that WCHAR

it is UTF-16.



This means that you need to search for wchar_t(0x00B2)

and wchar_t(0x00B3)

. std::find

can do it, just pass it the beginning and end of yours BSTR

.

+2


source


I suspect character encoding is key. Superscript 2 is not an ascii char. They highlight unicode characters (see http://en.wikipedia.org/wiki/Superscripts_and_Subscripts with values ​​in the range 0x207x other than 2 and 3) and they are usually encoded in 16 bits. If you have a std :: string then a character, then they are probably UTF-8 encoded, which means more than one char per character (see http://en.wikipedia.org/wiki/UTF-8 ) ...



There is a lot to read, but at the end it's pretty easy. You just need to find the substring (che UTF-8 superscript 2 and 3)

0


source







All Articles