-1

I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.

Thanks, Ram

Kshitij
  • 361
  • 1
  • 9
Ram
  • 3
  • 1
  • 1
    Too broad. What have you tried? Did you write your own conversion method and it does not work? Are you trying to use some library and have problems with that? – Daniel Frey Mar 07 '13 at 06:52
  • I haven't write my own conversion methods and I can use some library to achieve that. – Ram Mar 07 '13 at 07:00
  • Possible Dup: http://stackoverflow.com/questions/154536/encode-decode-urls-in-c – Daniel Frey Mar 07 '13 at 07:03
  • I'm pretty sure that's not a duplicate; this isn't asking about URL's... – Kyle Strand Mar 07 '13 at 07:04
  • How is the original string represented and encoded? And is it a srring literal in a source file, or is it read in from a text file, or does it come from keyboard input or something else? – Bingo Mar 07 '13 at 07:05
  • Yes. Its not encoding the URL. Converting unicode characters to xxx and vice versa. – Ram Mar 07 '13 at 07:06
  • User will key-in 100 € ; from the keyboard (command prompt) and need to store that into the database. If I change the value in the database to 200 € then it should be sent to the command prompt in the way that 200 € ; – Ram Mar 07 '13 at 07:11
  • Oh, sorry, it's called HTML encoding, not URL encoding, right? – Daniel Frey Mar 07 '13 at 07:30
  • yes. you are right Daniel – Ram Mar 07 '13 at 07:44
  • Whoa, you're using unicode to let users specify a currency type? – Kyle Strand Mar 07 '13 at 07:55
  • yes. This is will be received from some other application through tcp/ip protocol to my application. so the other application will send xxx instead of unicode characters. In my app I need to encode it and store into the db. – Ram Mar 07 '13 at 08:00

1 Answers1

0

I think this answer may be platform-dependent, though I don't know for sure.

You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:

char fixchar(string lookup){
    stringstream converter (lookup);
    int i;
    converter >> dec >> i;
    return (char)i

(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).

You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:

while (fixd.find("&#x")!=string::npos){
    tag = int(fixd.find("&#"));
    endtag = int(fixd.find(";"));
    fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}

Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • Thanks Kyle. I am testing this and let you know my result shortly. – Ram Mar 07 '13 at 07:52
  • Thanks and its working fine. I just changed fixchar() method to return back wchar_t instead of char. Also fixd is treated as wstring. – Ram Mar 12 '13 at 07:46