Go Down

### Topic: Convert Hex to exact Dec image (Read 1 time)previous topic - next topic

#### masterraf

##### Mar 01, 2013, 08:04 am
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

#1
##### Mar 01, 2013, 08:14 am
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

#2
##### Mar 01, 2013, 09:03 am
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

#3
##### Mar 01, 2013, 09:31 am
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

#4
##### Mar 01, 2013, 09:37 am
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.

#### masterraf

#5
##### Mar 01, 2013, 09:51 am
Quote
No, what you did was multiply the value by 16.

I read more about bitWise operators and found this "in fact, left shifting is the equivalent of multiplying by a power of two."

You were right. Moving 4 bits to the left is also equivalent to multiplying the value by 16.. XD

Anyway, what should I do if I need to change a hex value of 12 into a dec value of 12...
Or maybe a different approach..a char 1 and a char 2 into one single decimal variable of 12...

The algorithm I can only think about is doing it manually like
}

But I've got 0-360 HEX...and that means 360 if statements =.=

#### AWOL

#6
##### Mar 01, 2013, 10:05 amLast Edit: Mar 01, 2013, 10:07 am by AWOL Reason: 1
Think about what the "1" in "100" or "10" means.
Think now about what the "1" in "0x100" or "0x10" means.
Do you still think you need 'n' "==" operations?

(Useful search terms "Binary-coded Decimal" "arithmetic")
"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.

#### UKHeliBob

#7
##### Mar 01, 2013, 10:12 am
I am confused about what format the data is in when it is read from Serial.
You say
Code: [Select]
`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`
That sounds like it is already decimal and you are converting it to HEX
So, now you need it in decimal, but it already is.  Why questions like
Quote
what should I do if I need to change a hex value of 12 into a dec value of 12.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### AWOL

#8
##### Mar 01, 2013, 10:14 am
Quote
That sounds like it is already decimal and you are converting it to HEX

Hence my response in reply #2, but the OP seems determined to pursue the BCD route.
"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

Please enter a valid email to subscribe