0

So unfortunately from what I have read, there isn't a hex code for an up and down arrow. There is only a "friendly code."(See bottom of linked page) Does anyone know how to use this friendly code? Ideally, I just want a printf() or std::cout statement that prints the up or down arrow, in C/C++. Thanks! http://www.yellowpipe.com/yis/tools/ASCII-HTML-Characters/

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
user2494298
  • 299
  • 3
  • 7
  • 23
  • Can you use Unicode or are you limited to ASCII? – bbarnhart Mar 12 '14 at 15:22
  • These are character codes for HTML code, there is no way to simply translate them into c++... – stefan Mar 12 '14 at 15:25
  • This so called friendly code is a HTML name for certain unicode characters. HTML, not C++. First check if are on a system which can diplay unicode in it´s terminal (ie. say what you´re working with). Windows is not good in this case (there is a UTF-8 possibility, but buggy and only possible if the user change his font and so on...) – deviantfan Mar 12 '14 at 15:26

3 Answers3

4

Answers that assume a Unicode environment will not work with the Windows console at all, because it does not use Unicode. It uses a code page to determine which characters can be displayed and what the character codes are; on my US-based Windows 7 system that is Code page 437. You can see that the arrows are at the top of the list, but unfortunately they're in the control character range. This means they are stripped out of normal output entirely. You need special console output functions to display them.

Edit: It appears you don't need special console output, only a few characters like '\x0a' are stripped. The following string should print all 4 arrows: "\x18\x19\x1a\x1b".

Disregard this answer if you're using the Windows API and not a console program.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

You need to use a std::wstring:

#include <iostream>
#include <string>

int main()
{
   std::wstring s(L"←→↑↓");
   std::wcout << s << "\n";
}

Note that there is a difference between L"←→↑↓" and "←→↑↓". The latter is invalid, as neither nor and certainly not and are valid ASCII characters.

On my machine, this gets printed as <-->??, probably because the terminal doesn't support the characters.

stefan
  • 10,215
  • 4
  • 49
  • 90
0

Simply copy and paste the text of the arrow keys from the code below and add it into your code.

#include<iostream>
using namespace std;

int main(){
    // for up, down arrows.
    cout<<" ↑ ↓ ";
return 0;
}