This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Hex To Character Conversion Problem

I am Trying to Split a hexa value and read it over the serial port let me show you what i mean by that :

<per>
char arr[10]=(0);
int i,k,l;

arr[0]=0x11;
arr[1]=0x12;
: :
arr[7]=0x59;
arr[8]=0x5F;
arr[9]=0x90;

for(i=0;i<10;i++)
{ k=(arr[i]/0x10)>0x09?(arr[i]/0x10)+55:(arr[i]/0x10)+48;
l=(arr[i]%0x10)>0x09?(arr[i]%0x10)+55:(arr[i]%0x10)+48;
putchar(k);
putchar(l);
}

</per>

I am Getting Proper out put for values which are below 90 ie 1A or 2F or 44 or 39 but when value becomes 90 or 91 or A1 i am not getting proper out put. I just cant locate my mistake. Please Tell me where i am going wrong.

Thank You.
Ajay

Parents
  • Oops - the <per> and </per> tags should say "pre" - not "per"

    Remember to use the 'Preview' to check formatting before posting!

    char arr[10]=(0);
    int i,k,l;
    
    arr[0]=0x11;
    arr[1]=0x12;
    : :
    arr[7]=0x59;
    arr[8]=0x5F;
    arr[9]=0x90;
    
    for(i=0;i<10;i++)
    {
       k= (arr[i]/0x10)>0x09 ? (arr[i]/0x10)+55
                             : (arr[i]/0x10)+48;
    
       l= (arr[i]%0x10)>0x09 ? (arr[i]%0x10)+55
                             : (arr[i]%0x10)+48;
    
       putchar(k);
       putchar(l);
    }
    

    "I just cant locate my mistake"

    Start by commenting your code, using meaningful names instead of 'k' and 'l', and replacing the "magic numbers" with something more meaningful.

    Simply by going through that exercise you may well spot the problem yourself...

Reply
  • Oops - the <per> and </per> tags should say "pre" - not "per"

    Remember to use the 'Preview' to check formatting before posting!

    char arr[10]=(0);
    int i,k,l;
    
    arr[0]=0x11;
    arr[1]=0x12;
    : :
    arr[7]=0x59;
    arr[8]=0x5F;
    arr[9]=0x90;
    
    for(i=0;i<10;i++)
    {
       k= (arr[i]/0x10)>0x09 ? (arr[i]/0x10)+55
                             : (arr[i]/0x10)+48;
    
       l= (arr[i]%0x10)>0x09 ? (arr[i]%0x10)+55
                             : (arr[i]%0x10)+48;
    
       putchar(k);
       putchar(l);
    }
    

    "I just cant locate my mistake"

    Start by commenting your code, using meaningful names instead of 'k' and 'l', and replacing the "magic numbers" with something more meaningful.

    Simply by going through that exercise you may well spot the problem yourself...

Children
  • Hi Andy Thanks for That i'll make sure i get a preview before i post. I actually am not putting random numbers i t seems in the sample code but it is not that way i started from 10,11,12 and went on to see if output is coming . it was ok till 90 after that i was having problems.