How can I parse the euro symbol from a string?

I am trying to parse a string character to character so that I can load an image depending on each letter. Therefore, if the text is "Hello", I will print 5 images, which are the same letters, but made in Photoshop. It works great as long as I don't want to parse the € symbol.

std::string al = "Test €";

std::string letter="";
for (int i=0; i< al.length();++i)
{
    if (al[i]=='.') letter ="dot";
    else if (al[i]==',') letter ="coma";
    else if (al[i]==' ') letter ="space";
    //else if (al[i]=='€') letter ="euro";
    else letter=al[i];
}

      

This works great: letter

will ask for: values "T","e","s","t","space"

, but if I uncomment else if (al[i]=='€') letter ="euro";

and try to build it, then I get a red error in the message that reads:

warning: multi-character character constant

So, I need to know if al [i] is a € symbol to be able to convert "euro" to a letter (then my code will be able to work with it)

I searched on google and found that which says that "\u20AC"

is the C ++ code for & and I suppose the character is possibly larger than a byte, but still can't find how to deal with it and be able to parse it in my code. Any idea on how I can do this?

Thank you very much.

Note: I don't know the C ++ version used (dunno where can I check it), but I know it is not C ++ 11

+3


source to share


3 answers


The first problem is that you have to remember to use Unicode characters in your source code. Compilers only need to support a specific character set, and not all compilers may like your code. I suggest you read this answer for a more detailed explanation.

The second problem is that a character must be large in order to be represented in a character literal. You need to explicitly tell the compiler to use a wide character literal.



L'\x20AC`   // Notice the preceeding L

      

The third problem is that the rest of your code is still using narrow character strings. Change std::string

to std::wstring

.

+2


source


std :: string assumes that all characters are encoded in one byte. The character you want is a unicode character which is encoded in two bytes (why are you getting a "multi-character" error)



Your best bet is to use a library that understands unicode and sticks to it. This question may be relevant: unicode string in C ++ with boost

+1


source


"\ u20AC" is a string, so you need to split a large string into some substrings then you compare them. If they are equal then you replace it otherwise you replace every character in substrings

string al = "Test €";  (assume you declared std namespace already) 
string letter="";
char* ch = strtok(al," ");

while(ch!=NULL) {
    if(al.compare(ch)==0){ 
        letter="euro";
    }
    //your code here
}

      

0


source







All Articles