Char automatically converts to int (I guess)

Олег

I have following code

char temp[] = { 0xAE, 0xFF };
printf("%X\n", temp[0]);

Why output is FFFFFFAE, not just AE?

I tried

printf("%X\n", 0b10101110);

And output is correct: AE.

Suggestions?

Jose Fernando Lopez Fernandez

The answer you're getting, FFFFFFAE, is a result of the char data type being signed. If you check the value, you'll notice that it's equal to -82, where -82 + 256 = 174, or 0xAE in hexadecimal.

The reason you get the correct output when you print 0b10101110 or even 174 is because you're using the literal values directly, whereas in your example you're first putting the 0xAE value in a signed char where the value is then being sort of "reinterpreted modulo 128", if you wanna think of it that way.

So in other words:

    0   =    0 = 0x00 
    127 =  127 = 0x7F
    128 = -128 = 0xFFFFFF80
    129 = -127 = 0xFFFFFF81
    174 =  -82 = 0xFFFFFFAE
    255 =   -1 = 0xFFFFFFFF
    256 =    0 = 0x00

To fix this "problem", you could declare the same array you initially did, just make sure to use an unsigned char type array and your values should print as you expect.

#include <stdio.h>
#include <stdlib.h>


int main()
{
    unsigned char temp[] = { 0xAE, 0xFF };

    printf("%X\n", temp[0]);
    printf("%d\n\n", temp[0]);

    printf("%X\n", temp[1]);
    printf("%d\n\n", temp[1]);

    return EXIT_SUCCESS;
}

Output:

AE
174

FF
255

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I convert a char [] to an int?

分類Dev

If I want to promote a char to an int, should I use static_cast<int>(char variable) or +(char variable) and why?

分類Dev

Why returning fgetc() to char i.s.o. int?

分類Dev

How can I convert unsigned int to unsigned char

分類Dev

Mysql query is not working but i could not guess why

分類Dev

「int i =(byte)+(char)-(int)+(long)-1」が1である理由

分類Dev

Fasterxml Jackson automatically converts non-boolean value to a boolean value

分類Dev

Automatically display `[Char]` as `String` in ghci?

分類Dev

haskell [[Char]]-[[Int]]

分類Dev

Converting from char to int

分類Dev

Void * to char or int in C

分類Dev

Unshuffle an array of int/char

分類Dev

`i2c_smbus_read_block_data(int、unsigned char、unsigned char *) 'への未定義の参照

分類Dev

int main(int carg、const char ** varg){

分類Dev

Convert char to int in C#

分類Dev

Convert char to int in C#

分類Dev

Initialize vector <char> with int values

分類Dev

What is the result of adding a char and an int?

分類Dev

haskell [[Char]]から[[Int]]

分類Dev

Why char* and int* behaves differently

分類Dev

LINQ Join On Int = Char DataTypes?

分類Dev

iOS concatenate const char with int

分類Dev

Char to Int Pointer Casting Not Working

分類Dev

Arduino unsigned long int to char *

分類Dev

C++ char* と int*

分類Dev

Assigning to 'void (*)(int, const char *, int)' from incompatible type 'void (^__strong)(int, const char *, int)'

分類Dev

Should I use wchar_t or int16_t or char16_t for a bit map.

分類Dev

Java-char、int変換

分類Dev

Declaring a variable with two types: "int char"

Related 関連記事

ホットタグ

アーカイブ