E
E
Edward D2020-10-28 23:19:09
C++ / C#
Edward D, 2020-10-28 23:19:09

Character representation in char?

Sorry for the possibly stupid question.

Characters in ASCII are encoded with values ​​from 0 to 255 (256 in total). The symbol ╬ has the code 206.
If you output

char c = "╬";
printf("%d", (int)c);

it will be -50

How does this code compare with the code in the ASCII table?
It depends on features of the compiler, system?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2020-10-28
@jfaFan

It's just that the char in the sling is signed, you convert it to uint8_t instead of int and there will be a correct value.

W
wisgest, 2020-10-29
@wisgest

Use unsigned type for characters:

unsigned char c = '╬';
printf("%d", c);

O
OCTAGRAM, 2020-10-29
@OCTAGRAM

Correction: characters in ASCII are encoded from 0 to 127, and char sign does not apply to them.
Anything beyond 127 is Latin-1, OEM, ANSI, UTF-8, anything but ASCII. They write that all this is called extended ASCII, but how can you talk about something if it is KOI-8, and other encodings.
The provided code is not portable. For example, in Windows 10 it is possible for old programs to turn on the UTF-8 encoding instead of all sorts of 1251 and 866. There your ╬ are encoded in a completely different way. Use only Unicode. The quality of the standard library varies between different programming languages ​​and different compilers. If the standard library is bad, you will have to work through the API of the operating system until it comes to these giraffes.
It's good practice to find the console and write to it via Unicode APIs, and when you find a redirect, write UTF-8 bytes, ignoring ACP_OEM, etc., to quickly bury the zoo of everything other than Unicode.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question