Home > Net >  code ascii for '0' shifts from 48 to 0 how do i fix it?
code ascii for '0' shifts from 48 to 0 how do i fix it?

Time:11-05

I'm trying to create a table containing the ASCII code for all numbers contained in the base 16 (they are characters), but the character '0' is not working as intended, in the loop 'for' I can see that the code ASCII stays at 48 but once I exit it and I try to see the code ASCII for '0' once more, it has now moved to 0

for (int i = 0; i < 16;   i) {
    if(i>9){
        tableaubase[i]=48 i 7;
    } else{
        tableaubase[i]=48 i;
    }
    printf("tableaubase[%d] : %d \n", i, tableaubase[i]);
    printf("tableaubase[%d] : %d \n", 0, tableaubase[0]);

}

printf("tableaubase[%d] : %d \n", 0, tableaubase[0]);

CodePudding user response:

I believe you want

if (i < 10) {
    tableaubase[i] = '0'   i;
} else{
    tableaubase[i] = 'A'   (i-10);
}

Note that this expects an ASCII-based machine. Letters aren't consecutive on EBCDIC-based machines.

This won't change what happens for i == 0, which was already correct. This just makes the code more readable.

CodePudding user response:

Keep it simple.

char base16[16] = "0123456789ABCDEF";
  •  Tags:  
  • c
  • Related