Convert unsigned int to signed int C

后端 未结 9 2113
忘掉有多难
忘掉有多难 2020-12-05 14:31

I am trying to convert 65529 from an unsigned int to a signed int. I tried doing a cast like this:

unsigned int x = 65         


        
9条回答
  •  醉梦人生
    2020-12-05 15:02

    To answer the question posted in the comment above - try something like this:

    unsigned short int x = 65529U;
    short int y = (short int)x;
    
    printf("%d\n", y);
    

    or

    unsigned short int x = 65529U;
    short int y = 0;
    
    memcpy(&y, &x, sizeof(short int);
    printf("%d\n", y);
    

提交回复
热议问题