0

I cannot get my input character value properly. Here is my code:

#include <stdlib.h>
void main()
{
    char buf_in[32] = { 0 };
    printf("input: ");
    gets_s(buf_in, 32);
    for (int i = 0; i < 32; i++)
        printf("%02x ", buf_in[i]);
    getchar();
}

I input the character by <alt-254>, but the result is 74 00 00 ..., not fe 00 00 ....

Here is my local environment: chcp 936.

And I test in some different local environments (ANSI code page 936):
1. OEM code page 936 result : 74 00 00 ...
2. OEM code page 65001 result : 00 00 00 ... (weird)
3. OEM code page 1250 result : 3f 00 00 ...
4. OEM code page 437 result : 5f 00 00 ...

dcnh35
  • 354
  • 2
  • 13
  • How about unsigned char instead of char. Here is an article. http://stackoverflow.com/questions/4337217/difference-between-signed-unsigned-char – user2443447 Nov 21 '16 at 13:14
  • @user2443447 no, it doesn't work – dcnh35 Nov 22 '16 at 01:34
  • Tried almost all input methods; the closest seem to be `std::wcin` or `wscanf` from [How to use Unicode in C++?](http://stackoverflow.com/q/3010739/3439404) thread… – JosefZ Nov 22 '16 at 17:06

1 Answers1

0

I think ALT-codes are not supported by windows console. Please look here: Alt Codes in Batch

You can try to put ALT-code (or any other data...) in file and then redirect input to Your program:

./prog.exe < input.txt

To be sure that you put proper value in file i suggest to use hex editor (like XVI32)

Does it work for other characters?

Community
  • 1
  • 1
Mazi
  • 182
  • 8
  • 1
    Nope. [ALT-codes **are supported** by windows console](http://superuser.com/a/1047961/376602). Maybe using [`wchar` instead of `char`](http://stackoverflow.com/questions/7496203/char-vs-wchar-t) could help? – JosefZ Nov 21 '16 at 10:08