Hex Conversion from Byte Variable

sodeaf

Senior Member
Hey guys, Been reading multiple posts fir the last couple hours. Trying several different ideas without any luck.

What I am doing I thought would be simple but is causing me to pull my hair out.

After doing math using several variables I need to send the Byte Variable Answer as a HEX number.

For instance, if B13 = 77 and I try and send it, it sends the converted value of $4D, but what i need to do is send $77

Hserout 0, ($01,$00,B13) Which I though be be as simple as that, B13 = 77, but the device receiving info gets ($01,$00,$4D) but i need it to send ($01,$00,$77)

Is there a simple solution to convert the Byte Variable to HEX

Thanks

Steve
 

rossko57

Senior Member
It's not that clear what you are trying to do. Hex 4D = decimal 77, they are exactly the same value written in two different ways.

Hserout 0, ($01,$00,77) will send exactly the same as Hserout 0, ($01,$00,$4D)
Whatever receives that data may display the value in hex or in decimal, but it is the same value.
 

sodeaf

Senior Member
For instance if I want to show the a score on a display board ..

After doing the math the score is stored in W1 and W2.
W1 = 0018
W2 = 4526

Actual Score is 184526

I than divide into Byte Variables

B10 = 00
B11 = 18
B12 = 45
B13 = 26

The device wants to see the score via 4 HEX Bytes

Hserout 0, ($00,$18,$45,$26)

Because I have to calculate this score I need to send the values located in B10,B11,B12,B13

If I send Hserout 0, (B10,B11,B12,B13) in reality it is sending ($00,$12,$2D,$1A)

So I need to figure out a way to send the Decimal byte result.. so how would I convert Decimal 18 to Hex $18 (Decimal 24)
B10 = 00 need to convert to 00 so hex = $00
B11 = 18 need to convert to 24 so hex = $18
B12 = 45 need to convert to 69 so hex = $45
B13 = 26 need to convert to 38 so hex = $26

Hope that helps...

Steve
 

westaust55

Moderator
I believe that what you are trying to do is to convert a 2 digit value (max value 99) into into BCD representation.

The PICAXE X1 and X2 parts have a BINTOBCD function (see under LET command).
For M2 parts

Temp = b13//10
B13 = b13/10*16 + Temp
 

Goeytex

Senior Member
So I need to figure out a way to send the Decimal byte result.. so how would I convert Decimal 18 to Hex $18 (Decimal 24)
B10 = 00 need to convert to 00 so hex = $00
B11 = 18 need to convert to 24 so hex = $18
B12 = 45 need to convert to 69 so hex = $45
B13 = 26 need to convert to 38 so hex = $26
Multiply the first digit by 16 then add the second digit.


B10 00 0 x 16 + 0 = 0
B11 18 1 x 16 + 8 = 24
B12 45 4 x 16 + 5 = 69
B13 26 2 x 16 + 6 = 38

Code might be something like.

Code:
[color=Navy]#Picaxe [/color][color=Black]28X2[/color]

[color=Blue]Symbol [/color][color=Purple]Tens [/color][color=DarkCyan]=  [/color][color=Purple]B0[/color]
[color=Blue]Symbol [/color][color=Purple]Ones [/color][color=DarkCyan]=  [/color][color=Purple]B1[/color]
[color=Blue]Symbol [/color][color=Purple]SendByte [/color][color=DarkCyan]= [/color][color=Purple]B27

B10 [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Purple]B11 [/color][color=DarkCyan]= [/color][color=Navy]18[/color]
[color=Purple]B12 [/color][color=DarkCyan]= [/color][color=Navy]45[/color]
[color=Purple]B13 [/color][color=DarkCyan]= [/color][color=Navy]26[/color]

[color=Blue]Do
   Gosub [/color][color=Black]SendData[/color]
[color=Blue]Loop[/color]

[color=Black]SendData:
   [/color][color=Blue]For [/color][color=Purple]bptr [/color][color=DarkCyan]= [/color][color=Navy]10 [/color][color=Blue]to [/color][color=Navy]13           [/color][color=Green]'// (B10 to B13)
      [/color][color=Purple]tens [/color][color=DarkCyan]= [/color][color=Purple]@bptr [/color][color=DarkCyan]/ [/color][color=Navy]10           [/color][color=Green]'// Get tens 
      [/color][color=Purple]ones [/color][color=DarkCyan]= [/color][color=Purple]@bptr [/color][color=DarkCyan]// [/color][color=Navy]10          [/color][color=Green]'// Get ones with Modulus divide (remainder)
      [/color][color=Purple]SendByte [/color][color=DarkCyan]= [/color][color=Purple]tens [/color][color=DarkCyan]* [/color][color=Navy]16 [/color][color=DarkCyan]+ [/color][color=Purple]ones [/color][color=Green]'// Math
      [/color][color=Blue]Sertxd ([/color][color=Black]#[/color][color=Purple]SendByte[/color][color=Black],[/color][color=Red]" "[/color][color=Blue])
   Next
   Sertxd (CR[/color][color=Black],[/color][color=Blue]LF)
Return[/color]
Run this in the simulator. In final code remove unnecessary serial formatting.
 
Last edited:
Top