Michael
Michael

Reputation: 5335

Compare UTF-8 characters

Here is a parsing function:

double transform_units(double val, const char* s)
{
    cout << s[0];
    if (s[0] == 'm') return val * 1e-3;
    else if (s[0] == 'µ') return val * 1e-6;
    else if (s[0] == 'n') return val * 1e-9;
    else if (s[0] == 'p') return val * 1e-12;
    else return val;
}

In the line with 'µ' I'm getting the warning:

warning: multi-character character constant [-Wmultichar]

and the character 'µ' is not being catched.

How to compare multibyte characters?

Edit: a dirty workaround is to check if it is less than zero. As Giacomo mentioned, it's 0xCE 0xBC, both these bytes are greater than 127, so less than zero. It works for me.

Upvotes: 2

Views: 885

Answers (1)

eerorika
eerorika

Reputation: 238301

How to compare multibyte characters?

You can compare a unicode code point consisting of multiple bytes (more generally, multiple code units) by using multiple bytes. s[0] is only a single char which is the size of a byte and thus cannot by itself contain multiple bytes.

This may work: std::strncmp(s, "µ", std::strlen("µ")) == 0.

Upvotes: 2

Related Questions