Skip to main content
Extended code example
Source Link
Code Gorilla
  • 5.7k
  • 1
  • 17
  • 31
((tc[0]&0xF0)>>4)+0x30

Will generate values in the range x30 to x3F, which are 0 to 9 :;<=>? This is what you are seeing in you output (because you are adding in x2D '-')

I'm not sure what you are expecting to see, but as far as I can tell there are no mistakes and your code is working fine.

If you are trying to turn tc[0] into a hex digit then I would use this function

char TCToHexToHex (const char input)
{
    char output = '0';
    const int number = ((input & 0xF0) >> 4);
    if (numberinput > 9)
    { 
        output = (numberinput - 10) + 'A';
    }
    else
    {
        output = numberinput + '0';
    }
    return output;
}
char TCToHex (const char input)
{
    return ToHex(((input & 0xF0) >> 4));
}
char* CharToHex(const char input, char buffer[3])
{
    buffer[0] = ToHex((input & 0xF0) >> 4);
    buffer[1] = ToHex((input & 0x0F));
    buffer[2] = '\0'; // Always null terminate a string
    return buffer;
}

So what you will get if you call the functions is

    char buffer[3];
printf("CharToHex 0x%s\n", CharToHex('A', buffer));  //0x41 65 in decimal, ASCII A
printf("TCToHex = 0x%c \n", TCToHex(0x50)); // 5, because this function only takes the high nibble.
printf("ToHex = 0x%c \n", ToHex(0x0f)); // F

Hope that helps.

((tc[0]&0xF0)>>4)+0x30

Will generate values in the range x30 to x3F, which are 0 to 9 :;<=>? This is what you are seeing in you output (because you are adding in x2D '-')

I'm not sure what you are expecting to see, but as far as I can tell there are no mistakes and your code is working fine.

If you are trying to turn tc[0] into a hex digit then I would use this function

char TCToHex (char input)
{
    char output = '0';
    const int number = ((input & 0xF0) >> 4);
    if (number > 9)
    { 
        output = (number - 10) + 'A';
    }
    else
    {
        output = number + '0';
    }
    return output;
}

Hope that helps.

((tc[0]&0xF0)>>4)+0x30

Will generate values in the range x30 to x3F, which are 0 to 9 :;<=>? This is what you are seeing in you output (because you are adding in x2D '-')

I'm not sure what you are expecting to see, but as far as I can tell there are no mistakes and your code is working fine.

If you are trying to turn tc[0] into a hex digit then I would use this function

char ToHex (const char input)
{
    char output = '0';
    if (input > 9)
    { 
        output = (input - 10) + 'A';
    }
    else
    {
        output = input + '0';
    }
    return output;
}
char TCToHex (const char input)
{
    return ToHex(((input & 0xF0) >> 4));
}
char* CharToHex(const char input, char buffer[3])
{
    buffer[0] = ToHex((input & 0xF0) >> 4);
    buffer[1] = ToHex((input & 0x0F));
    buffer[2] = '\0'; // Always null terminate a string
    return buffer;
}

So what you will get if you call the functions is

    char buffer[3];
printf("CharToHex 0x%s\n", CharToHex('A', buffer));  //0x41 65 in decimal, ASCII A
printf("TCToHex = 0x%c \n", TCToHex(0x50)); // 5, because this function only takes the high nibble.
printf("ToHex = 0x%c \n", ToHex(0x0f)); // F

Hope that helps.

Source Link
Code Gorilla
  • 5.7k
  • 1
  • 17
  • 31

((tc[0]&0xF0)>>4)+0x30

Will generate values in the range x30 to x3F, which are 0 to 9 :;<=>? This is what you are seeing in you output (because you are adding in x2D '-')

I'm not sure what you are expecting to see, but as far as I can tell there are no mistakes and your code is working fine.

If you are trying to turn tc[0] into a hex digit then I would use this function

char TCToHex (char input)
{
    char output = '0';
    const int number = ((input & 0xF0) >> 4);
    if (number > 9)
    { 
        output = (number - 10) + 'A';
    }
    else
    {
        output = number + '0';
    }
    return output;
}

Hope that helps.