How to convert string with double high/wide characters to normal string [VC++6]
- by Shaitan00
My application typically recieves a string in the following format:
" Item $5.69 "
Some contants I always expect:
- the LENGHT always 20 characters
- the start index of the text always [5]
- and most importantly the index of the DECIMAL for the price always [14]
In order to identify this string correctly I validate all the expected contants listed above ....
Some of my clients have now started sending the string with Doube-High / Double-Wide values (pair of characters which represent a single readable character) similar to the following:
" Item $x80x90.x81x91x82x92 "
For testing I simply scan the string character-by-character, compare char[i] and char[i+1] and replace these pairs with their corresponding single character when a match is found (works fine) as follows:
[Code]
for (int i=0; i < sData.length(); i++)
{
char ch = sData[i] & 0xFF;
char ch2 = sData[i+1] & 0xFF;
if (ch == '\x80' && ch2 == '\x90')
zData.replace("\x80\x90", "0");
else if (ch == '\x81' && ch2 == '\x91')
zData.replace("\x81\x91", "1");
else if (ch == '\x82' && ch2 == '\x92')
zData.replace("\x82\x92", "2");
...
...
...
}
[/Code]
But the result is something like this:
" Item $5.69 "
Notice how this no longer matches my expectation: the lenght is now 17 (instead of 20) due to the 3 conversions and the decimal is now at index 13 (instead of 14) due to the conversion of the "5" before the decimal point.
Ideally I would like to convert the string to a normal readable format keeping the constants (length, index of text, index of decimal) at the same place (so the rest of my application is re-usable) ... or any other suggestion (I'm pretty much stuck with this)... Is there a STANDARD way of dealing with these type of characters?
Any help would be greatly appreciated, I've been stuck on this for a while now ...
Thanks,