We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
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...
"i am not getting proper out put."
So what output are you getting? In what way is it not "proper"?
Answering those questions should give you a clue to what's wrong...
What's not proper mean?
unsigned char arr[10];
void puthex(unsigned char b) { putchar("0123456789ABCDEF"[b]); } for( i=0; i<10; i++) { puthex(arr[i] >> 4); puthex(arr[i] & 0xF); }
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 ;)
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.
Hi Andy and also i am sorry that i did not mention my out put
when my array contains 10 the out put is 31 and 30 ie 1 & 0 in char
when my array contains 12 the out put is 31 and 32 ie 1 & 2 in char
when my array contains 1A the out put is 31 and 41 ie 1 and A in char
when my array contains 3B the out put is 33 and 42 ie 3 and B in char
but when my input is 90 the out put is 29 and 30 ie ( and 0 in char which i was expecting 39 and 30 ie 9 and 0 in char.
for all the values above 90 the out put is messed up.
Hi Dan , Thank you sir , you solved my problem. Thank you very much.
Thanks Ajay.