From serin to hexadecimal

jackberg

New Member
Hello to all,

I've a simple question ( learning everyday ), about assigning variables to different format.

I'm receiving data from the serin c.3,t9600_32,b0,b1 command, example: ( b0=1 b1=F )

I'm looking to transfer b0 and b1 to b10 as one hex value with the "$" sign as show here b10=$1F

or is it simpler to convert and assign the decimal value to b10 (b10=31)

I presume this is question is all about variables and mathematics , but after looking at Picaxe manual #2 , and some others , I did not

found anything as code to get this working, also any suggestions of books about Picaxe to learn theses variables assign coding.

Thank's again to all in the forum.
 
Numbers are just numbers: in principle it does not matter whether you handle them as binary, decimal, hexadecimal or (still my favourite) octal - they are all stored in the same way and you cannot tell whether a number was stored as hexadecimal, decimal or binary (it is just a value).

I think what you are asking is to combine two hexadecimal digits (single characters) into a 2-hexadecimal-digit number, with the first digit first, and the second digit second.

The notation "$" to indicate a hexadecimal dumber is only for indicating the number base of a constant in the source code of Picaxe program (similarly, "%" indicates a binary number but, again, is only used to indicate the number base in the source code of a Picaxe program).

What you need, in programming terms is this:
Code:
b10 = b0*16 + b1
If you print b10 after this with sertxd (using your b0=$1, b1=$F), it will - of course - be 31.
 
Thank's so much Aries

I'm sure that I made some errors , just want to confirm I used :

b0="1" : b1="F" ; 1F hex to decimal = 31 ; hex to decimal on the windows calculator
b2="4" : b3="9" ; 49 hex to decimal = 73
b4="7" : b5="E" ; 7E hex to decimal = 126

; the code below gave me other results.

b10 = b0*16 + b1 ; b10 = 86
b11 = b2*16 + b3 ; b11 = 121
b12 = b4*16 + b5 ; b12 = 181

sertxd(b10,b11,b12)
sertxd(13,10)
sertxd(#b10," ",#b11," ",#b12)

what I'm doing wrong here,,..
I looking to get from hex to decimal

1F = 31
49 = 73
7E = 126

25412

Thank's again
 
The first part is from your post #3
The second part is based on the post of Aries.
Code:
#REM
b0="1" : b1="F" ; 1F hex to decimal = 31 ; hex to decimal on the windows calculator
b2="4" : b3="9" ; 49 hex to decimal = 73
b4="7" : b5="E" ; 7E hex to decimal = 126
#endrem

b0 = $1
b1 = $F

b2 = $4
b3 = $9

b4 = $7
b5 = $E


b10 = b0 *16 + b1

b11 = b2 *16 + b3

b12 = b4 *16 + b5


sertxd (#b10," ",#b11, " ",#b12)

Please run it through the Simulator
 
Thank's eclectic for your support.

(the code you sent are ok if manual entry)

the b0 and b1 etc.. are received from a "serin"
I'm receiving data from the serin c.3,t9600_32,b0,b1 command, example: ( b0=1 b1=F )

from there I need to assemble b0 and b1 to get 1F or 31
after I'll use this 31 to process other variables.

any suggestions

Thank's for your time.
 
Last edited:
Let me explain in a different way...

my code are as follows,,

I'm receiving data from the serin command :

serin c.3,t9600_32,(2),b1,b2,b3,b4,b5,b6

the data of b1,b2,b3,b4,b5,b6 is 1F497E

looking for a routine that will take the value of:

b1,b2 and place it in b10 as $1F (or decimal 31)
b3,b4 and place it in b11 as $49 (or decimal 73)
b5,b6 and place it in b12 as $7E (or decimal 126)

Hope this can help to solve this issue.

Thank's again to all
 
I see what is happening. You are receiving ASCII representations of your values (i.e. to be interpreted as characters rather than numbers).
What you need is to convert them back to numbers first, then do the arithmetic:
Code:
b0 = b0 - "0"
if b0 > 9 then
  b0 = b0 + "0" - "A" + 10
endif
b1 = b1 - "0"
if b1 > 9 then
  b1 = b1 + "0" - "A" + 10
endif
b10 = b0*16 + b1
The idea is to subtract the value of zero ("0") from each character. If the character is in the range 0-9, you are done. If not, it is in the range A-F, so you need to subtract "A" instead, and then add 10 (because $A is 10)
You can improve the IF statements but then it's not so obvious what it is doing.
If you might be receiving characters that are not 0-9,A-F, then you would need to check that first.
 
Last edited:
Code:
b0 = b0 - "0"
if b0 > 9 then
  b0 = b0 + "0" - "A" + 10
endif
I had never looked into doing it all in one line but that turned out to be reasonable easy. ASCII character in 'b0' as input, binary (hex) as output -
Code:
b0 = b0 / $40 Max 1 * 9 + b0 & $0F
The advantage of doing the masking last is it works for "A"-"F" and "a"-"f".

For a pair of ASCII characters 'b0' (msb) and 'b1' (lsb), result in 'b0' -
Code:
b0 = b0 / $40 Max 1 * 9 + b0 & $0F * 16
b0 = b1 / $40 Max 1 * 9 + b1 & $0F + b0

The first part 'b0 / $40 Max 1 * 9' delivers zero if the character is "0"- "9", a 9 if above to adjust the lowest 4-bits before returning just those. Anything outside of "0"-"9", "A"-"F", "a"-"f" will give meaningless results.

Test code which runs in the simulator -
Code:
b0 = "0" : Gosub Char2HexNibble
b0 = "1" : Gosub Char2HexNibble
b0 = "2" : Gosub Char2HexNibble
b0 = "3" : Gosub Char2HexNibble
b0 = "4" : Gosub Char2HexNibble
b0 = "5" : Gosub Char2HexNibble
b0 = "6" : Gosub Char2HexNibble
b0 = "7" : Gosub Char2HexNibble
b0 = "8" : Gosub Char2HexNibble
b0 = "9" : Gosub Char2HexNibble
b0 = "A" : Gosub Char2HexNibble
b0 = "B" : Gosub Char2HexNibble
b0 = "C" : Gosub Char2HexNibble
b0 = "D" : Gosub Char2HexNibble
b0 = "E" : Gosub Char2HexNibble
b0 = "F" : Gosub Char2HexNibble
b0 = "a" : Gosub Char2HexNibble
b0 = "b" : Gosub Char2HexNibble
b0 = "c" : Gosub Char2HexNibble
b0 = "d" : Gosub Char2HexNibble
b0 = "e" : Gosub Char2HexNibble
b0 = "f" : Gosub Char2HexNibble
b0 = "1" : b1 = "F" : Gosub TwoChars2HexByte
b0 = "f" : b1 = "f" : Gosub TwoChars2HexByte
End

Char2HexNibble:
  SerTxd( "'", b0, "' -> " )
  b0 = b0 / $40 Max 1 * 9 + b0 & $0F
  SerTxd( #b0, CR, LF )
  Return

TwoChars2HexByte:
  SerTxd( "'", b0, "' '", b1, "' -> " )
  b0 = b0 / $40 Max 1 * 9 + b0 & $0F * 16
  b0 = b1 / $40 Max 1 * 9 + b1 & $0F + b0
  SerTxd( #b0, CR, LF )
  Return
 
Back
Top