0

I want to get the Latin1 code for multiply sign ×, but when I check the value inside the QChar it has -41'×'.

My code:

QString data = "×";
QChar m = data.at(0);
unsigned short ascii = (unsigned short)m.toLatin1();

When I debug, in the second line I see the QChar value is -41'×'.

I changed the code:

unsigned int ascii = c.unicode();

But I get the value 215 rather and I expect 158.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Robert
  • 10,403
  • 14
  • 67
  • 117
  • You're converting the character to Latin-1 encoding. I think ASCII table doesn't have this character. And ASCII codes are 8-bit, while it seems you intend to get 16 bits. You probably should use `m.unicode()` to get the character code in Unicode table. It returns unsigned short, so there is no need to cast it. – Pavel Strakhov Jun 23 '15 at 18:31

2 Answers2

4

The multiply sign × is not an ascii sign, as you can see when checking man ascii if you are on a unix system.

What its value is depends on the encoding, see here for its UTF representations. For example on UTF-8 it has the value 0xC397 which are two bytes. As is mentioned on the unicode page I linked 215 is the decimal value to represent this character in UTF-16 encoding, which is what c.unicode() returns. I don't know why you expect 158.

There is an ascii multiply sign though, which is *.

mfuchs
  • 2,190
  • 12
  • 20
1

If you check the Latin1 code table, it's obvious that × is indeed encoded as 215, or -41. Qt is giving you the correct result.

Your mistakes are:

  1. Assuming that Latin1 is equivalent to ASCII. Latin1 merely contains ASCII, but is the superset: it defines 2x more codes than ASCII does.

  2. Assuming that × is represented in the ASCII. It is not.

I have no clue where you got the idea that Latin1-encoded × should be 158. Surely it didn't come from the Latin1 code table! Incidentally, the Latin1 and UTF-8 encodings of × are identical.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • I asked because I am newer working with ascii code and I had doubts. I did some research, and I read that `×` is an extended ASCII code, so I did assumed it belongs to ASCII. But thank you for your answer. – Robert Jun 24 '15 at 21:52
  • @Robert ASCII is ASCII. There's no such thing as "extended" ASCII. It is something else but not ASCII anymore. – Kuba hasn't forgotten Monica Jun 24 '15 at 22:04