Stack Exchange
Python vs C: different outputs
I have a small program that converts a base 10 number into a kind of a base 36 number. Here's the Python code, and it works as expected.
def numToKey(num):
    start = 0x04;
    i = 0;
    for i in range(0,num):
        start+=1
    return start;


def largeNumToKeyboard(num):
    a = num / 1296;
    ar = num % 1296;
    b = ar / 36;
    c = ar % 36;

    a = numToKey(a);
    b = numToKey(b);
    c = numToKey(c);

    newb = b;
    if(a == b):
        newb = 0x2D;

    if(c == b):
        c = 0x2E;


    print a,newb,c
    print

largeNumToKeyboard(0)
largeNumToKeyboard(200)

The output is
4 45 46
4 9 24

"4 45 46" correctly represents 0, and "4 9 24" correctly represents 200.
But the issue is, it stops working after I convert it to C code.
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("hello world\n");
    largeNumToKeyboard(0);
    largeNumToKeyboard(200);
    return 0;
}

char numToKey(char num) {
    char start = 0x04;
    char i = 0;
    for (i = 0; i < num; i++) {
        start++;
    }
    return start;
}

void largeNumToKeyboard(int num) {
    char a = num / 1296;
    char ar = num % 1296;
    char b = ar / 36;
    char c = ar % 36;

    a = numToKey(a);
    b = numToKey(b);
    c = numToKey(c);
    char newb = b;
    if(a == b){
        newb = 0x2D;
    }
    if(c == b){
        c = 0x2E;
    }
    printf("%d ",a);
    printf("%d ",newb);
    printf("%d\r\n",c);

}

Now the output is 
4 45 46
4 45 46

I don't understand why for an input of 200, the C code gives me the wrong output, but the python code gives the correct output. I have a feeling it has something to do with the modulus, but I can't figure it out. Please help! Thanks!

0 in my base 36 would be just all zeros, and my base 36 the digits are hex codes starting with 0x04. So zero is 0x04, one is 0x05, etc.. until 35. So a decimal 0 converted to my system would be 0x04 0x04 0x04, however I don't allow repeats, so 0x2D (or 45 in decimal) represents repeat first, and 0x2E (or 46 decimal) represents repeat second. That is how 0 correctly translates to 0x04 0x2D 0x2E (or how it prints it, 4, 45 46). Similarly, 200 should be 0x04 0x09 0x24 (4 9 24)

Problem is you're using char types in the void largeNumToKeyboard(int num) function, and the values you're testing can potentially overflow a char. You'll need to change at least the first three to int...
void largeNumToKeyboard(int num) {
    int a = num / 1296;
    int ar = num % 1296;
    int b = ar / 36;
    char c = ar % 36;

    a = numToKey(a);
    b = numToKey(b);
    c = numToKey(c);
    char newb = b;
    if(a == b){
        newb = 0x2D;
    }
    if(c == b){
        c = 0x2E;
    }
    printf("%d ",a);
    printf("%d ",newb);
    printf("%d\r\n",c);

}

...then it prints the output...
4 45 46
4 9 24


In the C code you should use int types. char types only have a range from 0 to 255.
char a = num / 1296;
char ar = num % 1296;
char b = ar / 36;
char c = ar % 36; the C code 


In c code we should use int datatype as char gets overflow because it ranges from 0 to

As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
