How to get a getch( )

Bummer, your small c apps compiler doesn’t have a getch().

We can overcome this by installing ncurses or create our own getch() function.

  • ncurses

If you’re using cygwin, we should install ncurses first (it’s not included in cygwin’s default setup). First of all, you have to get libncurses-devel package.

Here is example how to use ncurses

Manual compile: gcc test_ncurses.c -lncurses

CMake example to compile

  • Here is the solution for creating getch() function.

Have fun.


Convert string to wide, and wide to string C++

For us who working on *nix platform, we will show a way to ocnvert between wide string to string and vice versa, and also to convert to char *.

The emerge of 64 bit architecture on the processor and operating system make a lot of application need to move to the more advance platform. Applications and libraries which build on 32 bit platform is not to say it’s obsolete, they in fact are still able to run on top of 64 bit, but it will not utilize fully the extra 32 bit.

The issue mostly lie down on libraries part, which they have many interdependency for another libraries and applications. It’s like an ugly intertangled string, developer need to rewrite a seperate version for it’s libraries, a slow transition but should facing to the right directions,this is inevitable because upgrade it merely their library from 32 to 64 bit might broke the current working applications.
Nobody want to disturb working applications, not us, our boss, and especially our customer, they are happy as ever just to see the application working and can do what they intend to do.

One biggest obstacle I found in this transition is the unicode string, they utilize the wide-string.
If you’re working on Windows platform using Visual C/C++ compiler, and your libraries/applications just supporting Windows, you might not find this issues, Visual C/C++ compiler do a good job to deal with string and wide-string, they can interchange seamlessly between them and their standart output.

On *nix platform with their GCC compiler, it strictly treat the string and wide-string, in conjuction of it’s output, string for cout, wide-string for wcout. If you have dependency with other library which require

char *,

we have no choice to convert it to match it’s parameter. The strict string handling are infact ideal, and the correct way to do a clean code, but this what hapen in real life, at least for now, we would say it’s the transition periode, but if during the transition we embrace 128 bit, then we’re doomed 🙂

std::wstring stringToWstring( std::string str )
std::wstring wStr( str.length(), L' ' );
std::copy( str.begin(), str.end(), wStr.begin() );
return wStr;

std::string WstringTostring( std::wstring wStr )
std::string str( wStr.length(), ' ' );
std::copy( wStr.begin(), wStr.end(), str.begin() );
return str;

and for char *,
there is 2 way, with a hint, look closely for the comment

char * varCh[];
std::string str = "a string";

varCh[0] = (char *);
// .data() doesn't have null termination

memcpy( &varCh[0], str.c_str(), str.length() );
// .c_str() have null termination

I believe we get the idea, and can utilize and modify between this tricks to get proper expected string.

Have fun!