Belgilangan qiymat oralig'ini C belgisiz belgisiz qiymat oralig'ida ifodalash uchun

-128 dan 127 oralig'idagi qiymatni 0 uchun 255 oralig'iga qanday o'zgartiradi.

Imzolangan o'zgaruvchida qiymati bor. O'tkazilgan qiymatni ishlamaydigan o'zgaruvchiga kiritish kerak.

3
int ga yozing va 128 ni qo'shing :) Agar siz boshqa biror narsani nazarda tutsangiz, iltimos, bu savolga aniqlik kiritib bering
qo'shib qo'ydi muallif rustyx, manba
int ga yozing va 128 ni qo'shing :) Agar siz boshqa biror narsani nazarda tutsangiz, iltimos, bu savolga aniqlik kiritib bering
qo'shib qo'ydi muallif rustyx, manba
Iltimos, hozirgi kunga qadar sizning tadqiqot/nosozliklarni namoyish qiling. Iltimos, birinchi navbatda qanday qilib so'rang sahifasini o'qing.
qo'shib qo'ydi muallif Sourav Ghosh, manba
int ga aylantiring. Natijaga 128 qo'shing. Natijani int sifatida kiritish.
qo'shib qo'ydi muallif Peter, manba
int ga aylantiring. Natijaga 128 qo'shing. Natijani int sifatida kiritish.
qo'shib qo'ydi muallif Peter, manba

8 javoblar

O'RT imzolangan char dan unsigned char ga o'tishni xohlaydi. Bu OSh tomonidan berilgan javobda qo'llab-quvvatlanadi:

#include 

#define MAX (255)

unsigned char convert(signed char sv)
{
    unsigned char x;
    x = (unsigned char)(sv + 128);
}
int main(void) {

    signed char sv = -128;
    unsigned char uv = 0;
    uv = convert(sv);
    print("converted value = %d", uv);
}

convert() unsigned char qiymatini qaytarishi kerakligini unutmang, lekin yuqoridagi kodda hech qanday qiymat qaytarilmaydi. Bu standartda aniqlanmagan xatti-harakatlarga olib kelishi mumkin: § 6.9. 1 12 .

convert() funksiyasidan qaytib kelmagan va printf() print() dan foydalanishni taqiqlovchi asosiy kod muammolarini ko'rib chiqayapsiz >, mumkin bo'lgan butun sonni to'ldirishni qiziqtirishi mumkin.

Albatta, yuqoridagi echim sv = -128 yoki sv = SCHAR_MIN va SCHAR_MAX + 1 128 , lekin agar sv = SCHAR_MAX yoki hatto sv = 0 bo'lsa edi? 0 + SCHAR_MAX + 1 kabi SCHAR_MAX + 1 qiymati imzolangan char @ , va bunday to'lib ketish aniqlanmagan harakatlarga olib keladi.

Kod bu erda butun sonli reklama aktsiyalari orqali saqlanadi, bular:

C11 Draft Standard §6.3.1.1   2

     

Quyidagi biror ifoda yoki int yoki imzosiz joyda ishlatilishi mumkin   int ishlatilishi mumkin:

     
      
  • Bir tamsayı turi (int yoki unsigned int tashqari), tamsayı o'tkazish darajasi,   int va unsigned int nomlari.
  •   
  • Bit-maydon turi _Bool, int, imzolangan int yoki imzosiz int.
  •   
     

Agar int birlamchi turdagi barcha qiymatlarni aks ettirsa (cheklangan   kenglik, bit-maydon uchun), qiymat int ga aylanadi;   aks holda, imzosiz int ga aylanadi. Bunga deyiladi   to'liq reklama aktsiyalari.

Shuning uchun sv kodi sv + 128 ifodasida int ga tarjima qilinadi va natijada olingan qiymat int dir. SCHA_MAX kodi INT_MAX bilan bir xil bo'lsa, char s aslida int (bu , ehtimol, lekin ehtimol emas).

Keyin sv + 128 dan kelib chiqadigan qiymat unsigned char ga yuboriladi va ehtimol chaqiruv funksiyasiga qaytadi. Biroq, bu erda turtki kerak emas, chunki natijaning turi belgilashdan avval unsigned char ga o'zgartiriladi:

C11 Draft Standard §6.5.16.1   2

     

Oddiy topshiriqda (=), o'ng operandning qiymati aylantiriladi   belgilash iborasining turiga o'tadi va saqlangan qiymati o'zgartiradi   chap operand tomonidan belgilangan ob'ektda.

Lekin bu erda keraksiz vazifa, chunki ifoda qiymati return ifodasi unsigned char ga aylanadi:

C11 Draft Standard §6.8.6.4   3

     

Agar ifoda bilan qaytarish so'zi bajarilsa,   ifodasi chaqiruvchiga funksiya chaqiruvi qiymati sifatida qaytariladi   ifoda qilish. Ifoda ifodasi qaytishdan boshqacha bo'lsa   paydo bo'ladigan funktsiyaning turi, qiymat xuddi xuddi aylantiriladi   funktsiyaning qaytariladigan turiga ega bo'lgan ob'ektga topshirish orqali amalga oshiriladi.

Shunday qilib, tamsaytli aktsiyalardan va qaytariladigan turdagi konvertatsiya qilishdan foydalanib, convert() funktsiyasi quyidagicha soddalashtirilishi mumkin:

unsigned char convert(signed char sv)
{
    return sv + 128;
}

Albatta, bu funktsiya juda kam, chunki butun dastur qayta yozilishi mumkin. Maksimal taşınabilirlik uchun limits.h dan SCHAR_MAX foydalanish kerak. Bundan tashqari, % hhu o'tkazish identifikatori unsigned char qiymatini chop qilish uchun foydalanilishi lozim:

#include 
#include 

int main(void) {

    signed char sv = -128;
    unsigned char uv = sv + SCHAR_MAX + 1;

    printf("converted value = %hhu\n", uv);
}

Shuni esda tutingki, sv ni faqat unsigned char ga yozib qo'yishingiz mumkin yoki siz uni aniq aylantirishdan qochishingiz mumkin.

unsigned char uv = sv;

Ushbu usul ishlaydi, ammo boshqa natijalar beradi. Mana, ikki usulning natijalarini taqqoslash:

#include 
#include 

int main(void)
{
    signed char sv;
    unsigned char uv;

    printf("Conversion by addition");
    sv = SCHAR_MIN;
    for (int i = 0; ; sv++, i++) {
        if (i % 6 == 0) {
            putchar('\n');
        }
        uv = sv + SCHAR_MAX + 1;
        printf("%4hhd --> %-4hhu", sv, uv);
        if (sv == SCHAR_MAX) break;
    }
    puts("\n");

    printf("Conversion by casting");
    sv = SCHAR_MIN;
    for (int i = 0; ; sv++, i++) {
        if (i % 6 == 0) {
            putchar('\n');
        }
        /* could use uv = sv instead of (unsigned char) sv */
        printf("%4hhd --> %-4hhu", sv, (unsigned char) sv);
        if (sv == SCHAR_MAX) break;
    }
    putchar('\n');

    return 0;
}

Dastur chiqishi:

Conversion by addition
-128 --> 0   -127 --> 1   -126 --> 2   -125 --> 3   -124 --> 4   -123 --> 5   
-122 --> 6   -121 --> 7   -120 --> 8   -119 --> 9   -118 --> 10  -117 --> 11  
-116 --> 12  -115 --> 13  -114 --> 14  -113 --> 15  -112 --> 16  -111 --> 17  
-110 --> 18  -109 --> 19  -108 --> 20  -107 --> 21  -106 --> 22  -105 --> 23  
-104 --> 24  -103 --> 25  -102 --> 26  -101 --> 27  -100 --> 28   -99 --> 29  
 -98 --> 30   -97 --> 31   -96 --> 32   -95 --> 33   -94 --> 34   -93 --> 35  
 -92 --> 36   -91 --> 37   -90 --> 38   -89 --> 39   -88 --> 40   -87 --> 41  
 -86 --> 42   -85 --> 43   -84 --> 44   -83 --> 45   -82 --> 46   -81 --> 47  
 -80 --> 48   -79 --> 49   -78 --> 50   -77 --> 51   -76 --> 52   -75 --> 53  
 -74 --> 54   -73 --> 55   -72 --> 56   -71 --> 57   -70 --> 58   -69 --> 59  
 -68 --> 60   -67 --> 61   -66 --> 62   -65 --> 63   -64 --> 64   -63 --> 65  
 -62 --> 66   -61 --> 67   -60 --> 68   -59 --> 69   -58 --> 70   -57 --> 71  
 -56 --> 72   -55 --> 73   -54 --> 74   -53 --> 75   -52 --> 76   -51 --> 77  
 -50 --> 78   -49 --> 79   -48 --> 80   -47 --> 81   -46 --> 82   -45 --> 83  
 -44 --> 84   -43 --> 85   -42 --> 86   -41 --> 87   -40 --> 88   -39 --> 89  
 -38 --> 90   -37 --> 91   -36 --> 92   -35 --> 93   -34 --> 94   -33 --> 95  
 -32 --> 96   -31 --> 97   -30 --> 98   -29 --> 99   -28 --> 100  -27 --> 101 
 -26 --> 102  -25 --> 103  -24 --> 104  -23 --> 105  -22 --> 106  -21 --> 107 
 -20 --> 108  -19 --> 109  -18 --> 110  -17 --> 111  -16 --> 112  -15 --> 113 
 -14 --> 114  -13 --> 115  -12 --> 116  -11 --> 117  -10 --> 118   -9 --> 119 
  -8 --> 120   -7 --> 121   -6 --> 122   -5 --> 123   -4 --> 124   -3 --> 125 
  -2 --> 126   -1 --> 127    0 --> 128    1 --> 129    2 --> 130    3 --> 131 
   4 --> 132    5 --> 133    6 --> 134    7 --> 135    8 --> 136    9 --> 137 
  10 --> 138   11 --> 139   12 --> 140   13 --> 141   14 --> 142   15 --> 143 
  16 --> 144   17 --> 145   18 --> 146   19 --> 147   20 --> 148   21 --> 149 
  22 --> 150   23 --> 151   24 --> 152   25 --> 153   26 --> 154   27 --> 155 
  28 --> 156   29 --> 157   30 --> 158   31 --> 159   32 --> 160   33 --> 161 
  34 --> 162   35 --> 163   36 --> 164   37 --> 165   38 --> 166   39 --> 167 
  40 --> 168   41 --> 169   42 --> 170   43 --> 171   44 --> 172   45 --> 173 
  46 --> 174   47 --> 175   48 --> 176   49 --> 177   50 --> 178   51 --> 179 
  52 --> 180   53 --> 181   54 --> 182   55 --> 183   56 --> 184   57 --> 185 
  58 --> 186   59 --> 187   60 --> 188   61 --> 189   62 --> 190   63 --> 191 
  64 --> 192   65 --> 193   66 --> 194   67 --> 195   68 --> 196   69 --> 197 
  70 --> 198   71 --> 199   72 --> 200   73 --> 201   74 --> 202   75 --> 203 
  76 --> 204   77 --> 205   78 --> 206   79 --> 207   80 --> 208   81 --> 209 
  82 --> 210   83 --> 211   84 --> 212   85 --> 213   86 --> 214   87 --> 215 
  88 --> 216   89 --> 217   90 --> 218   91 --> 219   92 --> 220   93 --> 221 
  94 --> 222   95 --> 223   96 --> 224   97 --> 225   98 --> 226   99 --> 227 
 100 --> 228  101 --> 229  102 --> 230  103 --> 231  104 --> 232  105 --> 233 
 106 --> 234  107 --> 235  108 --> 236  109 --> 237  110 --> 238  111 --> 239 
 112 --> 240  113 --> 241  114 --> 242  115 --> 243  116 --> 244  117 --> 245 
 118 --> 246  119 --> 247  120 --> 248  121 --> 249  122 --> 250  123 --> 251 
 124 --> 252  125 --> 253  126 --> 254  127 --> 255 

Conversion by casting
-128 --> 128 -127 --> 129 -126 --> 130 -125 --> 131 -124 --> 132 -123 --> 133 
-122 --> 134 -121 --> 135 -120 --> 136 -119 --> 137 -118 --> 138 -117 --> 139 
-116 --> 140 -115 --> 141 -114 --> 142 -113 --> 143 -112 --> 144 -111 --> 145 
-110 --> 146 -109 --> 147 -108 --> 148 -107 --> 149 -106 --> 150 -105 --> 151 
-104 --> 152 -103 --> 153 -102 --> 154 -101 --> 155 -100 --> 156  -99 --> 157 
 -98 --> 158  -97 --> 159  -96 --> 160  -95 --> 161  -94 --> 162  -93 --> 163 
 -92 --> 164  -91 --> 165  -90 --> 166  -89 --> 167  -88 --> 168  -87 --> 169 
 -86 --> 170  -85 --> 171  -84 --> 172  -83 --> 173  -82 --> 174  -81 --> 175 
 -80 --> 176  -79 --> 177  -78 --> 178  -77 --> 179  -76 --> 180  -75 --> 181 
 -74 --> 182  -73 --> 183  -72 --> 184  -71 --> 185  -70 --> 186  -69 --> 187 
 -68 --> 188  -67 --> 189  -66 --> 190  -65 --> 191  -64 --> 192  -63 --> 193 
 -62 --> 194  -61 --> 195  -60 --> 196  -59 --> 197  -58 --> 198  -57 --> 199 
 -56 --> 200  -55 --> 201  -54 --> 202  -53 --> 203  -52 --> 204  -51 --> 205 
 -50 --> 206  -49 --> 207  -48 --> 208  -47 --> 209  -46 --> 210  -45 --> 211 
 -44 --> 212  -43 --> 213  -42 --> 214  -41 --> 215  -40 --> 216  -39 --> 217 
 -38 --> 218  -37 --> 219  -36 --> 220  -35 --> 221  -34 --> 222  -33 --> 223 
 -32 --> 224  -31 --> 225  -30 --> 226  -29 --> 227  -28 --> 228  -27 --> 229 
 -26 --> 230  -25 --> 231  -24 --> 232  -23 --> 233  -22 --> 234  -21 --> 235 
 -20 --> 236  -19 --> 237  -18 --> 238  -17 --> 239  -16 --> 240  -15 --> 241 
 -14 --> 242  -13 --> 243  -12 --> 244  -11 --> 245  -10 --> 246   -9 --> 247 
  -8 --> 248   -7 --> 249   -6 --> 250   -5 --> 251   -4 --> 252   -3 --> 253 
  -2 --> 254   -1 --> 255    0 --> 0      1 --> 1      2 --> 2      3 --> 3   
   4 --> 4      5 --> 5      6 --> 6      7 --> 7      8 --> 8      9 --> 9   
  10 --> 10    11 --> 11    12 --> 12    13 --> 13    14 --> 14    15 --> 15  
  16 --> 16    17 --> 17    18 --> 18    19 --> 19    20 --> 20    21 --> 21  
  22 --> 22    23 --> 23    24 --> 24    25 --> 25    26 --> 26    27 --> 27  
  28 --> 28    29 --> 29    30 --> 30    31 --> 31    32 --> 32    33 --> 33  
  34 --> 34    35 --> 35    36 --> 36    37 --> 37    38 --> 38    39 --> 39  
  40 --> 40    41 --> 41    42 --> 42    43 --> 43    44 --> 44    45 --> 45  
  46 --> 46    47 --> 47    48 --> 48    49 --> 49    50 --> 50    51 --> 51  
  52 --> 52    53 --> 53    54 --> 54    55 --> 55    56 --> 56    57 --> 57  
  58 --> 58    59 --> 59    60 --> 60    61 --> 61    62 --> 62    63 --> 63  
  64 --> 64    65 --> 65    66 --> 66    67 --> 67    68 --> 68    69 --> 69  
  70 --> 70    71 --> 71    72 --> 72    73 --> 73    74 --> 74    75 --> 75  
  76 --> 76    77 --> 77    78 --> 78    79 --> 79    80 --> 80    81 --> 81  
  82 --> 82    83 --> 83    84 --> 84    85 --> 85    86 --> 86    87 --> 87  
  88 --> 88    89 --> 89    90 --> 90    91 --> 91    92 --> 92    93 --> 93  
  94 --> 94    95 --> 95    96 --> 96    97 --> 97    98 --> 98    99 --> 99  
 100 --> 100  101 --> 101  102 --> 102  103 --> 103  104 --> 104  105 --> 105 
 106 --> 106  107 --> 107  108 --> 108  109 --> 109  110 --> 110  111 --> 111 
 112 --> 112  113 --> 113  114 --> 114  115 --> 115  116 --> 116  117 --> 117 
 118 --> 118  119 --> 119  120 --> 120  121 --> 121  122 --> 122  123 --> 123 
 124 --> 124  125 --> 125  126 --> 126  127 --> 127 
1
qo'shib qo'ydi
@ chqrlie-- Hmmm. Bu yaxshi taklif; Men bu haqda o'ylardim. Rahmat!
qo'shib qo'ydi muallif David Bowling, manba
Bu juda aniq javob. sv! = SCHAR_MAX shartini bosib, sv == SCHAR_MAX uchun maxsus ishni faktoring qilishni taklif qilaman va if (sv == SCHAR_MAX ) tanaffus; pastki jismlarning pastki qismida.
qo'shib qo'ydi muallif chqrlie, manba

O'RT imzolangan char dan unsigned char ga o'tishni xohlaydi. Bu OSh tomonidan berilgan javobda qo'llab-quvvatlanadi:

#include 

#define MAX (255)

unsigned char convert(signed char sv)
{
    unsigned char x;
    x = (unsigned char)(sv + 128);
}
int main(void) {

    signed char sv = -128;
    unsigned char uv = 0;
    uv = convert(sv);
    print("converted value = %d", uv);
}

convert() unsigned char qiymatini qaytarishi kerakligini unutmang, lekin yuqoridagi kodda hech qanday qiymat qaytarilmaydi. Bu standartda aniqlanmagan xatti-harakatlarga olib kelishi mumkin: § 6.9. 1 12 .

convert() funksiyasidan qaytib kelmagan va printf() print() dan foydalanishni taqiqlovchi asosiy kod muammolarini ko'rib chiqayapsiz >, mumkin bo'lgan butun sonni to'ldirishni qiziqtirishi mumkin.

Albatta, yuqoridagi echim sv = -128 yoki sv = SCHAR_MIN va SCHAR_MAX + 1 128 , lekin agar sv = SCHAR_MAX yoki hatto sv = 0 bo'lsa edi? 0 + SCHAR_MAX + 1 kabi SCHAR_MAX + 1 qiymati imzolangan char @ , va bunday to'lib ketish aniqlanmagan harakatlarga olib keladi.

Kod bu erda butun sonli reklama aktsiyalari orqali saqlanadi, bular:

C11 Draft Standard §6.3.1.1   2

     

Quyidagi biror ifoda yoki int yoki imzosiz joyda ishlatilishi mumkin   int ishlatilishi mumkin:

     
      
  • Bir tamsayı turi (int yoki unsigned int tashqari), tamsayı o'tkazish darajasi,   int va unsigned int nomlari.
  •   
  • Bit-maydon turi _Bool, int, imzolangan int yoki imzosiz int.
  •   
     

Agar int birlamchi turdagi barcha qiymatlarni aks ettirsa (cheklangan   kenglik, bit-maydon uchun), qiymat int ga aylanadi;   aks holda, imzosiz int ga aylanadi. Bunga deyiladi   to'liq reklama aktsiyalari.

Shuning uchun sv kodi sv + 128 ifodasida int ga tarjima qilinadi va natijada olingan qiymat int dir. SCHA_MAX kodi INT_MAX bilan bir xil bo'lsa, char s aslida int (bu , ehtimol, lekin ehtimol emas).

Keyin sv + 128 dan kelib chiqadigan qiymat unsigned char ga yuboriladi va ehtimol chaqiruv funksiyasiga qaytadi. Biroq, bu erda turtki kerak emas, chunki natijaning turi belgilashdan avval unsigned char ga o'zgartiriladi:

C11 Draft Standard §6.5.16.1   2

     

Oddiy topshiriqda (=), o'ng operandning qiymati aylantiriladi   belgilash iborasining turiga o'tadi va saqlangan qiymati o'zgartiradi   chap operand tomonidan belgilangan ob'ektda.

Lekin bu erda keraksiz vazifa, chunki ifoda qiymati return ifodasi unsigned char ga aylanadi:

C11 Draft Standard §6.8.6.4   3

     

Agar ifoda bilan qaytarish so'zi bajarilsa,   ifodasi chaqiruvchiga funksiya chaqiruvi qiymati sifatida qaytariladi   ifoda qilish. Ifoda ifodasi qaytishdan boshqacha bo'lsa   paydo bo'ladigan funktsiyaning turi, qiymat xuddi xuddi aylantiriladi   funktsiyaning qaytariladigan turiga ega bo'lgan ob'ektga topshirish orqali amalga oshiriladi.

Shunday qilib, tamsaytli aktsiyalardan va qaytariladigan turdagi konvertatsiya qilishdan foydalanib, convert() funktsiyasi quyidagicha soddalashtirilishi mumkin:

unsigned char convert(signed char sv)
{
    return sv + 128;
}

Albatta, bu funktsiya juda kam, chunki butun dastur qayta yozilishi mumkin. Maksimal taşınabilirlik uchun limits.h dan SCHAR_MAX foydalanish kerak. Bundan tashqari, % hhu o'tkazish identifikatori unsigned char qiymatini chop qilish uchun foydalanilishi lozim:

#include 
#include 

int main(void) {

    signed char sv = -128;
    unsigned char uv = sv + SCHAR_MAX + 1;

    printf("converted value = %hhu\n", uv);
}

Shuni esda tutingki, sv ni faqat unsigned char ga yozib qo'yishingiz mumkin yoki siz uni aniq aylantirishdan qochishingiz mumkin.

unsigned char uv = sv;

Ushbu usul ishlaydi, ammo boshqa natijalar beradi. Mana, ikki usulning natijalarini taqqoslash:

#include 
#include 

int main(void)
{
    signed char sv;
    unsigned char uv;

    printf("Conversion by addition");
    sv = SCHAR_MIN;
    for (int i = 0; ; sv++, i++) {
        if (i % 6 == 0) {
            putchar('\n');
        }
        uv = sv + SCHAR_MAX + 1;
        printf("%4hhd --> %-4hhu", sv, uv);
        if (sv == SCHAR_MAX) break;
    }
    puts("\n");

    printf("Conversion by casting");
    sv = SCHAR_MIN;
    for (int i = 0; ; sv++, i++) {
        if (i % 6 == 0) {
            putchar('\n');
        }
        /* could use uv = sv instead of (unsigned char) sv */
        printf("%4hhd --> %-4hhu", sv, (unsigned char) sv);
        if (sv == SCHAR_MAX) break;
    }
    putchar('\n');

    return 0;
}

Dastur chiqishi:

Conversion by addition
-128 --> 0   -127 --> 1   -126 --> 2   -125 --> 3   -124 --> 4   -123 --> 5   
-122 --> 6   -121 --> 7   -120 --> 8   -119 --> 9   -118 --> 10  -117 --> 11  
-116 --> 12  -115 --> 13  -114 --> 14  -113 --> 15  -112 --> 16  -111 --> 17  
-110 --> 18  -109 --> 19  -108 --> 20  -107 --> 21  -106 --> 22  -105 --> 23  
-104 --> 24  -103 --> 25  -102 --> 26  -101 --> 27  -100 --> 28   -99 --> 29  
 -98 --> 30   -97 --> 31   -96 --> 32   -95 --> 33   -94 --> 34   -93 --> 35  
 -92 --> 36   -91 --> 37   -90 --> 38   -89 --> 39   -88 --> 40   -87 --> 41  
 -86 --> 42   -85 --> 43   -84 --> 44   -83 --> 45   -82 --> 46   -81 --> 47  
 -80 --> 48   -79 --> 49   -78 --> 50   -77 --> 51   -76 --> 52   -75 --> 53  
 -74 --> 54   -73 --> 55   -72 --> 56   -71 --> 57   -70 --> 58   -69 --> 59  
 -68 --> 60   -67 --> 61   -66 --> 62   -65 --> 63   -64 --> 64   -63 --> 65  
 -62 --> 66   -61 --> 67   -60 --> 68   -59 --> 69   -58 --> 70   -57 --> 71  
 -56 --> 72   -55 --> 73   -54 --> 74   -53 --> 75   -52 --> 76   -51 --> 77  
 -50 --> 78   -49 --> 79   -48 --> 80   -47 --> 81   -46 --> 82   -45 --> 83  
 -44 --> 84   -43 --> 85   -42 --> 86   -41 --> 87   -40 --> 88   -39 --> 89  
 -38 --> 90   -37 --> 91   -36 --> 92   -35 --> 93   -34 --> 94   -33 --> 95  
 -32 --> 96   -31 --> 97   -30 --> 98   -29 --> 99   -28 --> 100  -27 --> 101 
 -26 --> 102  -25 --> 103  -24 --> 104  -23 --> 105  -22 --> 106  -21 --> 107 
 -20 --> 108  -19 --> 109  -18 --> 110  -17 --> 111  -16 --> 112  -15 --> 113 
 -14 --> 114  -13 --> 115  -12 --> 116  -11 --> 117  -10 --> 118   -9 --> 119 
  -8 --> 120   -7 --> 121   -6 --> 122   -5 --> 123   -4 --> 124   -3 --> 125 
  -2 --> 126   -1 --> 127    0 --> 128    1 --> 129    2 --> 130    3 --> 131 
   4 --> 132    5 --> 133    6 --> 134    7 --> 135    8 --> 136    9 --> 137 
  10 --> 138   11 --> 139   12 --> 140   13 --> 141   14 --> 142   15 --> 143 
  16 --> 144   17 --> 145   18 --> 146   19 --> 147   20 --> 148   21 --> 149 
  22 --> 150   23 --> 151   24 --> 152   25 --> 153   26 --> 154   27 --> 155 
  28 --> 156   29 --> 157   30 --> 158   31 --> 159   32 --> 160   33 --> 161 
  34 --> 162   35 --> 163   36 --> 164   37 --> 165   38 --> 166   39 --> 167 
  40 --> 168   41 --> 169   42 --> 170   43 --> 171   44 --> 172   45 --> 173 
  46 --> 174   47 --> 175   48 --> 176   49 --> 177   50 --> 178   51 --> 179 
  52 --> 180   53 --> 181   54 --> 182   55 --> 183   56 --> 184   57 --> 185 
  58 --> 186   59 --> 187   60 --> 188   61 --> 189   62 --> 190   63 --> 191 
  64 --> 192   65 --> 193   66 --> 194   67 --> 195   68 --> 196   69 --> 197 
  70 --> 198   71 --> 199   72 --> 200   73 --> 201   74 --> 202   75 --> 203 
  76 --> 204   77 --> 205   78 --> 206   79 --> 207   80 --> 208   81 --> 209 
  82 --> 210   83 --> 211   84 --> 212   85 --> 213   86 --> 214   87 --> 215 
  88 --> 216   89 --> 217   90 --> 218   91 --> 219   92 --> 220   93 --> 221 
  94 --> 222   95 --> 223   96 --> 224   97 --> 225   98 --> 226   99 --> 227 
 100 --> 228  101 --> 229  102 --> 230  103 --> 231  104 --> 232  105 --> 233 
 106 --> 234  107 --> 235  108 --> 236  109 --> 237  110 --> 238  111 --> 239 
 112 --> 240  113 --> 241  114 --> 242  115 --> 243  116 --> 244  117 --> 245 
 118 --> 246  119 --> 247  120 --> 248  121 --> 249  122 --> 250  123 --> 251 
 124 --> 252  125 --> 253  126 --> 254  127 --> 255 

Conversion by casting
-128 --> 128 -127 --> 129 -126 --> 130 -125 --> 131 -124 --> 132 -123 --> 133 
-122 --> 134 -121 --> 135 -120 --> 136 -119 --> 137 -118 --> 138 -117 --> 139 
-116 --> 140 -115 --> 141 -114 --> 142 -113 --> 143 -112 --> 144 -111 --> 145 
-110 --> 146 -109 --> 147 -108 --> 148 -107 --> 149 -106 --> 150 -105 --> 151 
-104 --> 152 -103 --> 153 -102 --> 154 -101 --> 155 -100 --> 156  -99 --> 157 
 -98 --> 158  -97 --> 159  -96 --> 160  -95 --> 161  -94 --> 162  -93 --> 163 
 -92 --> 164  -91 --> 165  -90 --> 166  -89 --> 167  -88 --> 168  -87 --> 169 
 -86 --> 170  -85 --> 171  -84 --> 172  -83 --> 173  -82 --> 174  -81 --> 175 
 -80 --> 176  -79 --> 177  -78 --> 178  -77 --> 179  -76 --> 180  -75 --> 181 
 -74 --> 182  -73 --> 183  -72 --> 184  -71 --> 185  -70 --> 186  -69 --> 187 
 -68 --> 188  -67 --> 189  -66 --> 190  -65 --> 191  -64 --> 192  -63 --> 193 
 -62 --> 194  -61 --> 195  -60 --> 196  -59 --> 197  -58 --> 198  -57 --> 199 
 -56 --> 200  -55 --> 201  -54 --> 202  -53 --> 203  -52 --> 204  -51 --> 205 
 -50 --> 206  -49 --> 207  -48 --> 208  -47 --> 209  -46 --> 210  -45 --> 211 
 -44 --> 212  -43 --> 213  -42 --> 214  -41 --> 215  -40 --> 216  -39 --> 217 
 -38 --> 218  -37 --> 219  -36 --> 220  -35 --> 221  -34 --> 222  -33 --> 223 
 -32 --> 224  -31 --> 225  -30 --> 226  -29 --> 227  -28 --> 228  -27 --> 229 
 -26 --> 230  -25 --> 231  -24 --> 232  -23 --> 233  -22 --> 234  -21 --> 235 
 -20 --> 236  -19 --> 237  -18 --> 238  -17 --> 239  -16 --> 240  -15 --> 241 
 -14 --> 242  -13 --> 243  -12 --> 244  -11 --> 245  -10 --> 246   -9 --> 247 
  -8 --> 248   -7 --> 249   -6 --> 250   -5 --> 251   -4 --> 252   -3 --> 253 
  -2 --> 254   -1 --> 255    0 --> 0      1 --> 1      2 --> 2      3 --> 3   
   4 --> 4      5 --> 5      6 --> 6      7 --> 7      8 --> 8      9 --> 9   
  10 --> 10    11 --> 11    12 --> 12    13 --> 13    14 --> 14    15 --> 15  
  16 --> 16    17 --> 17    18 --> 18    19 --> 19    20 --> 20    21 --> 21  
  22 --> 22    23 --> 23    24 --> 24    25 --> 25    26 --> 26    27 --> 27  
  28 --> 28    29 --> 29    30 --> 30    31 --> 31    32 --> 32    33 --> 33  
  34 --> 34    35 --> 35    36 --> 36    37 --> 37    38 --> 38    39 --> 39  
  40 --> 40    41 --> 41    42 --> 42    43 --> 43    44 --> 44    45 --> 45  
  46 --> 46    47 --> 47    48 --> 48    49 --> 49    50 --> 50    51 --> 51  
  52 --> 52    53 --> 53    54 --> 54    55 --> 55    56 --> 56    57 --> 57  
  58 --> 58    59 --> 59    60 --> 60    61 --> 61    62 --> 62    63 --> 63  
  64 --> 64    65 --> 65    66 --> 66    67 --> 67    68 --> 68    69 --> 69  
  70 --> 70    71 --> 71    72 --> 72    73 --> 73    74 --> 74    75 --> 75  
  76 --> 76    77 --> 77    78 --> 78    79 --> 79    80 --> 80    81 --> 81  
  82 --> 82    83 --> 83    84 --> 84    85 --> 85    86 --> 86    87 --> 87  
  88 --> 88    89 --> 89    90 --> 90    91 --> 91    92 --> 92    93 --> 93  
  94 --> 94    95 --> 95    96 --> 96    97 --> 97    98 --> 98    99 --> 99  
 100 --> 100  101 --> 101  102 --> 102  103 --> 103  104 --> 104  105 --> 105 
 106 --> 106  107 --> 107  108 --> 108  109 --> 109  110 --> 110  111 --> 111 
 112 --> 112  113 --> 113  114 --> 114  115 --> 115  116 --> 116  117 --> 117 
 118 --> 118  119 --> 119  120 --> 120  121 --> 121  122 --> 122  123 --> 123 
 124 --> 124  125 --> 125  126 --> 126  127 --> 127 
1
qo'shib qo'ydi
@ chqrlie-- Hmmm. Bu yaxshi taklif; Men bu haqda o'ylardim. Rahmat!
qo'shib qo'ydi muallif David Bowling, manba
Bu juda aniq javob. sv! = SCHAR_MAX shartini bosib, sv == SCHAR_MAX uchun maxsus ishni faktoring qilishni taklif qilaman va if (sv == SCHAR_MAX ) tanaffus; pastki jismlarning pastki qismida.
qo'shib qo'ydi muallif chqrlie, manba

Sizning savolingiz etarlicha aniq emas: [- 128..127] ni [0..255] ga aylantirish turli xil usullar bilan amalga oshirilishi mumkin.

  • 0..127 oralig'ida qiymatlarni saqlashni istaysizmi va salbiy qiymatlarni 128..255 oralig'iga aylantirmoqchimisiz? Bu osonlik bilan (unsigned char) yoki (uint8_t) kabi qiymatni oddiygina bajarish mumkin.

  • -128 kodini 0 , -127 1 ga o'zgartirmoqchimisiz? 127 ga 255 ga qadar qancha? Imzo qo'yilgan qiymatga 128 qo'shing.
1
qo'shib qo'ydi

Sizning savolingiz etarlicha aniq emas: [- 128..127] ni [0..255] ga aylantirish turli xil usullar bilan amalga oshirilishi mumkin.

  • 0..127 oralig'ida qiymatlarni saqlashni istaysizmi va salbiy qiymatlarni 128..255 oralig'iga aylantirmoqchimisiz? Bu osonlik bilan (unsigned char) yoki (uint8_t) kabi qiymatni oddiygina bajarish mumkin.

  • -128 kodini 0 , -127 1 ga o'zgartirmoqchimisiz? 127 ga 255 ga qadar qancha? Imzo qo'yilgan qiymatga 128 qo'shing.
1
qo'shib qo'ydi
    #include 

    #define MAX (255)

    unsigned char convert(signed char sv)
    {
        unsigned char x;
        x = (unsigned char)(sv + 128);
    }
    int main(void) {

        signed char sv = -128;
        unsigned char uv = 0;
        uv = convert(sv);
        print("converted value = %d", uv);
    }
0
qo'shib qo'ydi
Bu erda eslatib o'tilishi kerak bo'lgan bir nechta narsalar bor. printf() o'rniga print() dan foydalanganingizdan oldin, bu kod kompilyatsiya qilinmaydi. Bundan tashqari, convert() vazifasi aniqlanmagan xatti-harakatga olib keladigan qiymatni qaytarmaydi. Batafsil ma'lumot uchun javobni ko'ring.
qo'shib qo'ydi muallif David Bowling, manba
    #include 

    #define MAX (255)

    unsigned char convert(signed char sv)
    {
        unsigned char x;
        x = (unsigned char)(sv + 128);
    }
    int main(void) {

        signed char sv = -128;
        unsigned char uv = 0;
        uv = convert(sv);
        print("converted value = %d", uv);
    }
0
qo'shib qo'ydi
Bu erda eslatib o'tilishi kerak bo'lgan bir nechta narsalar bor. printf() o'rniga print() dan foydalanganingizdan oldin, bu kod kompilyatsiya qilinmaydi. Bundan tashqari, convert() vazifasi aniqlanmagan xatti-harakatga olib keladigan qiymatni qaytarmaydi. Batafsil ma'lumot uchun javobni ko'ring.
qo'shib qo'ydi muallif David Bowling, manba

Siz bergan ma'lumotlarning barchasi biz uchun kerak deb hisoblaymiz sign o'zgaruvchiga 128 qo'shing va unsigned o'zgaruvchini yozib oling

Misol uchun,

s.v = -50

u.v = 128 + s.v

u.v = 78
0
qo'shib qo'ydi
Rahmat roboboykm..im dastur to'g'ri ishlayapti ..
qo'shib qo'ydi muallif Basha, manba

Siz bergan ma'lumotlarning barchasi biz uchun kerak deb hisoblaymiz sign o'zgaruvchiga 128 qo'shing va unsigned o'zgaruvchini yozib oling

Misol uchun,

s.v = -50

u.v = 128 + s.v

u.v = 78
0
qo'shib qo'ydi
Rahmat roboboykm..im dastur to'g'ri ishlayapti ..
qo'shib qo'ydi muallif Basha, manba