Reputation: 7045
I'm trying to do print a square mark in following code:
code 1:
char* exp = new char[300];
*(exp) = 178// which is 2's extended ascii code
*(exp+1) = '\0'// end of string
printf("%s",exp);`
it will print "?"
and seems like the little 2 will join the next letter automatically:
code 2:
char* exp = new char[300];
*(exp) = 178// which is 2's extended ascii code
*(exp+1) = '4'// or anything '5' 'a' '#'...
*(exp+2) = '\0'// end of string
printf("%s",exp);
it will print a Chinese word or maybe not but really likes.
What supposed to do? I just want to print the "little 2".
PS:
In WindowsXP console, really thanks for your help.
Upvotes: 1
Views: 15290
Reputation: 114569
The ASCII charset only covers English letters, numbers and a bunch of other characters, but nothing fancy like squared symbols, diamonds or clubs and the like. Those characters are outside the ASCII specs and depends on which coding will be assumed by whoever reads your output. Also all non-English characters (e.g. Chinese, Japanese, Russian, Northern Europe accented letters, Hebrew, ...) are outside the ASCII specifications.
When a computer produces some output you can safely assume that if you stick only to the ASCII subset your bytes will be unambiguous (there was a time when the use of different encodings was common even for just the English alphabet, but now those times are mostly gone). If however you output contains any byte higher than 127 then the interpretation depends on which encoding will be considered by the reader.
Very common encodings are for example Latin-1 (ISO8859-1) where the squared symbol is the code 178, CP850 (DOS) where the squared symbol is 253 or UTF-8 where the squared symbol is instead the sequence 194+178.
Given that your terminal is showing Chinese characters I guess that probably it's interpreting your output as UTF-8 because it's an encoding that uses a variable number of bytes for each character, but that is able to represent any unicode character; neither iso-8859-1 nor cp850 can represent Chinese characters.
This is however just guessing because there are other very common encodings for e.g. Japanese characters... (Shift-JIS)
Upvotes: 3
Reputation: 7103
Here are three different ways to print the superscript 2:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(int argc, char** argv)
{
setlocale(LC_CTYPE, "");
wchar_t *foo = L"²";
wprintf(L"%ls\n", foo);
wprintf(L"²\n");
wchar_t bar[2];
bar[0] = 178;
bar[1] = 0;
wprintf(L"%ls\n", bar);
return 0;
}
Upvotes: 7
Reputation: 67283
178 is not a small two in the ASCII table, it's a block. Isn't it more like 253?
Also, how numbers translate to characters depends on the platform you're using. But you've said nothing about this.
Upvotes: 1