type-conversionmicrocontrollerpicmikroc

Convert from int to binary or hex in mikroc


I got an int value from 0 to 255 and I want to convert that value to hex or binary so i can use it into an 8 bit register(PIC18F uC). How can i do this conversion? I tried to use IntToHex function from Conversion Library but the output of this function is a char value, and from here i got stuck. I'm using mikroc for pic. Where should I start? Thanks!


Solution

  • This is a common problem. Many don't understand that, Decimal 15 is same as Hex F is same as Octal 17 is same as Binary 1111.

    Different number systems are for Humans, for CPU, it's all Binary!

    When OP says,

    I got an int value from 0 to 255 and I want to convert that value to hex or binary so i can use it into an 8 bit register(PIC18F uC).

    It reflects this misunderstanding. Probably, because debugger is configured to show "decimal" values and sample code/datasheet shows Hex value for register operations.

    So, when you get "int" value from 0 to 255, you can directly write that number to 8-bit register. You don't have to convert it to hex. Hex is just representation which makes Human's life easy.

    What you can do is - this is good practise --

    REG_VALUE = (unsigned char) int_value;