Unicode Box Drawing chars

Aug 5, 2011 at 12:37am
Hi, i have a simple question: in a Win32 console application, how do you get Unicode characters? What about box drawing characters? Thanks.
Aug 5, 2011 at 1:02am
What to you mean by get characters?

If you mean output them, I would hope you could just use std::wcout

The box characters are in the range 2580–259F
http://en.wikipedia.org/wiki/Box_drawing_characters
Aug 5, 2011 at 4:56am
Unfortunately it is not that simple. I used to think it was, but turned out it was not. I haven't played with it myself, but you at least need to call SetConsoleOutputCP() to set the console to Unicode. See http://msdn.microsoft.com/en-us/library/ms686036(VS.85).aspx .

You can also browse the net for examples. I once read a blog about this, but I could not find it.
Aug 5, 2011 at 7:22pm
It turns out that the "magic" call is _setmode() with _O_U16TEXT....

The following compiles with Visual C++ 2008, and ran ok on Windows Vista.

Note that cout is not happy when the console is running in this mode.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Hello World! in a Box (for Windows console)

#include <iostream>
#include <string>
using namespace std;

#include <io.h>    // for _setmode()
#include <fcntl.h> // for _O_U16TEXT

int main ()
{
	_setmode(_fileno(stdout), _O_U16TEXT);

	// Box edges and corners (all double thick line)
	// T for top, B for bottom, L for left, R for right
	const wchar_t TB = L'\x2550';
	const wchar_t LR = L'\x2551';
	const wchar_t TL = L'\x2554';
	const wchar_t TR = L'\x2557';
	const wchar_t BL = L'\x255A';
	const wchar_t BR = L'\x255D';

	wstring hello  = L"Hello World!";
	wstring margin = L"  ";
	wstring line(hello.length() + 2 * margin.length(), TB);

	wcout << TL << line << TR << endl;
	wcout << LR << margin << hello << margin << LR << endl;
	wcout << BL << line << BR << endl;
	wcout << endl;

	return 0;
}


Notes
1. I think SetConsoleOutputCP() is prob the right call to use when using WriteConsole()
Last edited on Aug 5, 2011 at 7:51pm
Aug 6, 2011 at 4:47am
Most likely _setmode() in MS' CRT just maps to SetConsoleOutputCP().
Aug 6, 2011 at 5:02am
Wow, thanks so much for the help, but it turned out that I just needed to make a CLR console application. ;P I'm not exactly a great programmer... ;P
Aug 6, 2011 at 12:29pm
Just had a look at the CRT code. _setmode() is not calling SetConsoleOutputCP()

In fact, there is not sign of a (direct) call to SetConsoleOutputCP() (or SetConsoleCP()) in the CRT (well, grep doesn't find one), so it appears there using their own parallel mechanism.

The call is setting an internal flag.

Also, there's a comment warning that "the CRT codepage is different from the Console codepage".

Andy

P.S. I have Visual Studio 2008 Professional, which includes the CRT source (etc) for reference. Though it is prob. a bit outdated thanks to Microsoft update.
Topic archived. No new replies allowed.