Go Down

Topic: Convert Hex to exact Dec image (Read 858 times) previous topic - next topic

masterraf

Good Day guys!

I've got an electronic compass that sends char data through serial connection.
For example if the degrees read is 250 it will send a char 2, then a char 5, then a char 0.
I was able to use this data by converting it to HEX with this code

Code: [Select]
int asc2dec (char c){
  if (c >= '0' && c <= '9')
    return c - '0' ;
  if (c >= 'A' && c <= 'F')
    return c - 'A' + 10 ;
  if (c >= 'a' && c <= 'f')
    return c - 'a' + 10 ;
}


and then combining it all together into one variable by moving 4 bits to the left and using OR logic like this
Code: [Select]
for(byte r= 1; r < 4; r++){
          heading <<= 4 ;
          heading |= asc2dec(EC_Data[r]);


so this code returns a hexadecimal value of the read degrees from the compass

Now what I want is instead of having a hex value, I want to a have a decimal value...Like if the compass sends a char of 250. I also want to have a decimal value of 250?

Any ideas on how to do this? THX!

UKHeliBob

Have a look at Serial.parseInt()
"Looks for the next valid integer in the incoming serial stream. "
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

Here
Code: [Select]
heading <<= 4 the "<<" is multiplying by 16 (hexadecimal, remember), but you want decimal, so what value do you think you might want to multiply by?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

masterraf

Quote
the "<<" is multiplying by 16 (hexadecimal, remember), but you want decimal, so what value do you think you might want to multiply by?


I'm sorry sir AWOL but what do you mean by this?....what I did there is I moved heading 4 bits to the left so that I can use OR operations to it for the next HEX value to be recieved...like if the 1st data is 1,HEX which is equal to 0001 then I move it 4 bits to he left, it will become 0001 000 then OR it with the next value for example 2,HEX which is equal to 0010. Using OR operation on 0001 0000 and 0010. It will be 0001 0010 which is equal to 12 in HEX.

But what I want is instead of having 12 in HEX..I want to have a 12 in DEC but I can't think of an algorithm for doing so...

AWOL

Quote
.what I did there is I moved heading 4 bits to the left

No, what you did was multiply the value by 16.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up