Pages: [1]   Go Down
Author Topic: Convert Hex to exact Dec image  (Read 755 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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!
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4337
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a look at Serial.parseInt()
"Looks for the next valid integer in the incoming serial stream. "
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here
Code:
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?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
.what I did there is I moved heading 4 bits to the left
No, what you did was multiply the value by 16.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.. smiley-lol

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
if(heading == 0x12){
decheading=12;
}

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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")
« Last Edit: March 01, 2013, 04:07:28 am by AWOL » Logged

"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.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4337
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am confused about what format the data is in when it is read from Serial.
You say
Code:
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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: