6

I have the following code:

cout << "String that includes a £ sign";

However, the £ sign is not being recognised by the compiler, and instead an accented u is being displayed. I'm able to insert one using the following method:

cout << "String that includes a " << char(156) << " sign";

where 156 is the ASCII code of the £ sign. Is there a way I can include this ASCII code in the string itself without having to separate it out like this? For example:

cout << "String that includes a <some way of iserting the £ sign> sign";
Paul R
  • 208,748
  • 37
  • 389
  • 560

4 Answers4

14

£ does not have an "ASCII code" - ASCII is 7 bits (0..127). There are various extended 8 bit characters sets which include £ but there is no single standard for this and the £ symbol may have various different values in different environments. You should probably be using Unicode if you need international symbols with maximum portability.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 3
    Yep. However, you might try `std::wcout << L"String that includes a £ sign";`. Depending on your platform, this might work. – sbi Sep 30 '10 at 14:06
  • 2
    For clarification. In Visual Studio: Project -> Properties ->Configuration Properties -> General -> Change "Character Set" from "Use Multi-Byte Character Set" To "Use Unicode Character Set" – AndyG Sep 30 '10 at 14:10
10

You can use the \x escape sequence to insert a character with its hexadecimal code. In your case

cout << "String that includes a \x9C sign";

Like Paul says, this just outputs a character with code 156, but gives no guarantee that it is actually the pound sign on different systems.

Carvellis
  • 3,992
  • 2
  • 34
  • 66
3

ASCII only defines characters up to 127. (The 8th bit was intened for parity, which was needed for the noisy transmission line of the 1960s when ASCII was designed) Beyond that, the characters vary by code page and font. There is a discrepancy between the font used by the editor you use to write your C++ code, and the font used by whatever is displaying the output.

So, you need to display character 156 to display a £ on your output device. You can explicitly enter a character 156 on a WIndows PC, by holding done the Alt key and pressing "0159" on the numeric keyped. Alternately, you can use a character code in the string in hex:

 cout << "String that includes a \x9C sign"; 
James Curran
  • 101,701
  • 37
  • 181
  • 258
0

I've been trying to answer the same question in C.

One of the answers in Displaying wide chars with printf gives a clue to a possibly more robust method. When using the Code::Blocks editor neither \x9C or \xA5 works. This might be due to the default POSIX local being set for its console output, this is ASCII-only, so no characters above \x7f work. To set the locale to UTF-8 we can use setlocale(LC_ALL,"").

#include <stdio.h>
#include <locale.h>

int main() {
    setlocale(LC_ALL,"");
    printf("%lc",163);
} 
 

In C++ we can use

setlocale(LC_ALL,"");
cout << char(163) << 123.45 << endl;

With a UTF-8 locale set we need to convert the unicode character \x9c into the two-byte UFT-8 encoding. Which is \xc2a3. So

printf("\xc2a3%.2f",123.456);
Salix alba
  • 7,536
  • 2
  • 32
  • 38