0

To get certain special characters in C++ we can enclose an escape sequence of octal values within single quotes thus '\nnn' where nnn is an octal code. The only example I currently know of is '\370' for the degree symbol (°). Where is a list of other such values for printing non-simple-ascii characters in C++?

In a code table at ascii-code.com are these two entries

DEC OCT HEX BIN Symbol  HTML Number HTML Name   Description
176 260 B0  10110000    °    ° °       Degree sign
248 370 F8  11111000    ø    ø ø    Latin small letter o with slash

which are clearly not correct for my problem – i.e., assigning '\nnn' to a char variable or inserting to an outputstream via cout << in Visual Studio 2015. Where is a correct table? It has something to do with "codesets" but I don't know what codeset is the default in VS 2015 or how to change the codeset.

It was suggested below that I "use \u#### where #### is a number representing the Unicode value of the wanted character. For example \u2014 will print an — (em-dash)." However, when I try that example – or any others from rapidtables.com/code/text/unicode-characters or wikipedia.org/wiki/List_of_Unicode_characters – I don't get the character expected for the given code. For example, \20AC should produce a Euro sign (€) but I get a big C with cedilla (Ç). Where is the table of Unicode values for printing special characters in C++ in Visual Studio 2015?

4
  • Define "my problem". The table on ascii-code.com gives the characters in the ISO-8859-1 codeset. What codeset are you using? Jun 5, 2016 at 18:58
  • @SamVarshavchik - See extra info.
    – Martin F
    Jun 5, 2016 at 19:07
  • 1
    I recommend you to use \u#### where #### is a number representing the Unicode value of the wanted character. For example \u2014 will print an (em-dash).
    – sergiol
    Jun 5, 2016 at 21:57
  • @sergiol - See extra info.
    – Martin F
    Jun 8, 2016 at 17:49

1 Answer 1

1

how to change the codeset

Next code for a C++ console application prepared in Visual Studio 2013 could help. Based on this answer.

// 37645602.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"     // added by Visual Studio
#include <fstream>
#include <iostream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>

const wchar_t* testArray[] =
{
    L"€ ° ø — ď π щ",                                      // for debugging
    L"\u20AC \u00b0 \u00F8 \u2014 \u010f \u03c0 \u0449"    // the same characters
};

int _tmain(int argc, _TCHAR* argv[])
{
    _setmode(_fileno(stdout), _O_U16TEXT);

    for (int j = 0; j < 2; ++j)
    {
        std::wcout << testArray[j] << L'\n';
    }
    return 0;
}

Output:

==> 37645602.exe
€ ° ø — ď π щ
€ ° ø — ď π щ

Tested using a mix of Latin, Slavic, Cyrillic and Greek characters (output from a script similar to this Alt KeyCode Finder):

==> altcodes "€°ø—ďπщ"
Ch Unicode    Alt?      mod 256     UTF-8   IME 0405/cs-CZ; CP852; ANSI 1250
 €  U+20AC    8364        …172…  0xE282AC   Euro Sign
 °  U+00B0     176        …176…    0xC2B0   Degree Sign
 ø  U+00F8     248        …248…    0xC3B8   Latin Small Letter O With Stroke
 —  U+2014    8212         …20…  0xE28094   Em Dash
 ď  U+010F     271         …15…    0xC48F   Latin Small Letter D With Caron
 π  U+03C0     960        …192…    0xCF80   Greek Small Letter Pi
 щ  U+0449    1097         …73…    0xD189   Cyrillic Small Letter Shcha
 €°ø—ďπщ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.