Displays hexadecimal value in cmd.exe

Asked

Viewed 211 times

4

I’m trying to show the sharp letter 'is' on cmd.exe through the hexadecimal, but the printf() only shows the value of the character itself.

I’m trying the following:

unsigned char eh = 0x82;
printf("%x", eh)

2 answers

8


The printf is showing hexadecimal as you used "%x", which is just that! Show the hexadecimal value :)

Just use this way in the case of your example:

printf("%c" ,eh);

Remember that in C++11 you can use UTF-8 directly in the code:

char* acentuada = u8"Acentuação";
printf("%s", acentuada);

It is important this second to ensure that your code editor is configured for UTF-8.

Remember that in both cases, the way your environment is configured (code page, encoding pattern) can affect the output, even if the input and code are right.

Of curiosity here are the main formats of the printf:

d ou i  Inteiro decimal com sinal                         392
u       Inteiro decimal sem sinal                        7235
o       Octal sem sinal                                   610
x       Inteiro hexadecimal sem sinal                     7fa
f       Ponto flutuante decimal                        392.65
e       Notação científica                          3.9265e+2
g       %e ou %f automático (o que for mais curto)     392.65
c       Caractere                                           b
s       Cadeia (string) de caracteres                   teste
p       Endereço de ponteiro                         ba000000

Note that several formats return letters, such as x and e, for example, they can be written as X and E if you want the output in upper case, such as 7FA and 3.9265E+2.

  • I think it should be printf("%c" ,eh); since the variable eh is not a string but a character.

  • @Osvaldo actually adapted to longer strings, as in the case of U8"...", anyway, it was good to comment. If this is the case, I separate the two examples so as not to confuse anyone.

  • 1

    In the case of C++11, this is problematic because, although the code actually generates output in UTF-8, CMD is hardly configured to recognize this format. It has a whole non-standard way of being...

  • @Guilhermebernal is, I even put the note in yellow thinking about it. Actually even in the original example, the code page will vary a lot from environment to environment (and system version).

  • Thank you very much, I had confused myself a little but now I understood right, very well formulated answer! and vlw by the UTF-8 tip, but @Bacco, my editor should use UTF-8 or UTF-8 without GOOD?

  • @Olimonf compilers usually don’t like BOM. BOM makes more sense in UTF16. Remember that this is just to use U8".... because if you’re going to put the characters in hexa, the editor doesn’t matter. Look at Guilherme’s comment too, CMD doesn’t usually respect UTF, even if the code is right.

Show 1 more comment

4

The correct is to use the %c to display the character, %s is used to display a string of strings.

unsigned char eh = 0x82;
printf("%c", eh);

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.