Please note: We are aware of an issue affecting replies on the Arm Community forums, which may not be loading as expected.

We apologize for any inconvenience and appreciate your patience while we investigate and work to resolve the issue.

Thank you for your understanding.


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
  • From your formatting, I can clearly see that this was aimed at me (<per>...</per>) :)

    First of all: Stylistically, you should not use magic constants. Don't write 48 if you mean '0'. Don't write 55 if you mean 'A'-10.

    Second: Don't do extra divisions, if you don't need to:

    k = a[i] >= 0xA0 ? (a[i]>>4)+'A'-10 : (a[i]>>4)+'0';
    

    Third: Don't assume that a character is signed. If you declare your array:
    unsigned char arr[10];
    it will work a lot better.

    Fourth: Don't first perform a zero-assign of the array, just to follow it later with individual assignments.

    Either write:

    unsigned char arr[10];
    arr[7] = 0x59;
    


    or write

    unsigned char arr[10] = { 0x11,0x12,...,0x59,0x5f,0x90 };
    

    Remember that even if the average compilers gets smarter every day, there are still stupid compilers in existence out in the wild ;)

Reply
  • From your formatting, I can clearly see that this was aimed at me (<per>...</per>) :)

    First of all: Stylistically, you should not use magic constants. Don't write 48 if you mean '0'. Don't write 55 if you mean 'A'-10.

    Second: Don't do extra divisions, if you don't need to:

    k = a[i] >= 0xA0 ? (a[i]>>4)+'A'-10 : (a[i]>>4)+'0';
    

    Third: Don't assume that a character is signed. If you declare your array:
    unsigned char arr[10];
    it will work a lot better.

    Fourth: Don't first perform a zero-assign of the array, just to follow it later with individual assignments.

    Either write:

    unsigned char arr[10];
    arr[7] = 0x59;
    


    or write

    unsigned char arr[10] = { 0x11,0x12,...,0x59,0x5f,0x90 };
    

    Remember that even if the average compilers gets smarter every day, there are still stupid compilers in existence out in the wild ;)

Children
No data