This char is 254 in ASCII Extended Table, and 25A0 in Unicode. If I run putchar(254) the terminal does not recognize the char, as I think it utilizes not extended ASCII.
- 625
3 Answers
printf("■\n"); works for me - putchar('■'); gives me a warning about multi-character character constants. putchar(254); gives me a 'þ' character.
Also, make sure the terminal emulator you are using supports unicode.
- 66
There is no such thing as “the extended ASCII”. There are tens, if not hundreds, of 8-bit encodings based on ASCII. Many of them are available on Ubuntu, but they are not commonly used. The modern computing world, Ubuntu or not, uses the Unicode character set, generally encoded as UTF-8. Unicode is a 32-bit character set and UTF-8 encodes each 32-bit code point as one to four 8-bit bytes.
If character 254 is ■, you probably want one of the 8-bit encoding used in the text mode of PC computers, sometimes known as DOS code pages. I can't find a simple way to run a program that wants a DOS code page. (Some terminals, including xterm but not Gnome-terminal, support 8-bit code pages via luit. But Ubuntu does not come with locale definitions for DOS code pages.) You can use iconv or recode to convert between a DOS code page and UTF-8, e.g.
my_program_that_outputs_cp437 | iconv -f CP437
If you're writing a program today, unless you need compatibility with 20th century software, use Unicode and UTF-8. So if you want ■ (BLACK SQUARE), it's code point 25A0. Since UTF-8 uses more than one byte per character, you need puts(), not putchar(): a “char” in C is a single byte.
puts("■");
putchar(254) prints nothing because it's an unfinished UTF-8 encoding.
- 61,858
C (also C++, etc) is quite annoying -- a single Unicode character isn't one "character". As Unicode uses more than one byte to store a single character, you cannot use functions that only support one "character" like putchar. Consider other printing functions, like printf as Alex said.
Of course, your terminal has to support Unicode, lol.
- 625