I bought an lcd and a joystick shield for my arduino, with the hopes of making a primitive game system. My only problem, however, is that the joystick speaks in decimal nums, and the lcd uses hex. I wrote some code to convert the decimals to hex, but it only ever returns zero. Can anyone give me some help? Or better yet, a working decimal-to-hex function?

Here is my code: (Yes, I know that it only accepts two digits. I made it that way ON PURPOSE.)

```
int dec2hex(int decNum){
int remainder;
int ans = decNum;
char hexNum[2];
int counter = 0;
while((ans > 0) && (counter >= 0)){
remainder = ans % 16;
ans = (ans - remainder) / 16;
if(remainder == '0'){
hexNum[1 - counter] = '0';
}else if(remainder == '1'){
hexNum[1 - counter] = '1';
} else if(remainder == '2'){
hexNum[1 - counter] = '2';
} else if(remainder == '3'){
hexNum[1 - counter] = '3';
} else if(remainder == '4'){
hexNum[1 - counter] = '4';
} else if(remainder == '5'){
hexNum[1 - counter] = '5';
} else if(remainder == '6'){
hexNum[1 - counter] = '6';
} else if(remainder == '7'){
hexNum[1 - counter] = '7';
} else if(remainder == '8'){
hexNum[1 - counter] = '8';
} else if(remainder == '9'){
hexNum[1 - counter] = '9';
} else if(remainder == '10'){
hexNum[1 - counter] = 'A';
} else if(remainder == '11'){
hexNum[1 - counter] = 'B';
} else if(remainder == '12'){
hexNum[1 - counter] = 'C';
} else if(remainder == '13'){
hexNum[1 - counter] = 'D';
} else if(remainder == '14'){
hexNum[1 - counter] = 'E';
} else if(remainder == '15'){
hexNum[1 - counter] = 'F';
}
counter++;
}
return hexNum[1];
return hexNum[0];
}
```