I wrote the below program where I am trying to convert a binary string variable to its equivalent number value,
but all I get is wrong values.
Is this possible or am I doing something wrong ?
;Chip Settings
chip 18F4550,48
config PLLDIV=1, OSC=HSPLL_HS, MCLRE=ON, LVP=OFF
;Defines (Constants)
define LCD_IO 2
define LCD_CB PORTC.1
define LCD_DB PORTC.2
'
Dim D1str, D1, D2, D3, D4, D5, D6, D7, D8, HEXstr, HEXstr2 as string
Dim Dinput1 as word
Dim Dinput2 as byte
'
wait 300 ms
'
'Values used to create my binary string
D1 = "1" ;D1 to D8 = 11010110b = 214 = D6h
D2 = "1"
D3 = "0"
D4 = "1"
D5 = "0"
D6 = "1"
D7 = "1"
D8 = "0"
'
D1str = D1 + D2 + D3 + D4 + D5 + D6 + D7 + D8 ;The binary string to be converted
locate 0, 0
print D1str; prints out 11010110b which is correct
'
'Tried these to do the conversion
Dinput1 = VAL(D1str)
Dinput2 = BcdToDec_GCB (D1str)
HEXstr = HEX(Dinput1)
HEXstr2 = HEX(Dinput2)
'
'My results - which are all wrong
locate 1, 0 ; ALL the below values do NOT = 11010110b
print Dinput1 ; prints out 62
print ":"
print Dinput2 ; prints out 143
print ":"
print Hexstr ; prints out 3E
print ":"
print HEXstr2 ; prints out 8F
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have taken Evan's function code above and simplified. ( I could not get it working as is)
This tested ok on 16F18855 with LCD
CLS
Wait 1 s
Dim result as Byte
Dim D1, D2, D3, D4, D5, D6, D7, D8 as string * 1 Dim D1str as string * 8
D1 = "1" ;D1 to D8 = 11010110b = 214 = D6h
D2 = "1"
D3 = "0"
D4 = "1"
D5 = "0"
D6 = "1"
D7 = "1"
D8 = "0"
D1Str = D1+D2+D3+D4+D5+D6+D7+D8
Result = BinaryStr2Byte(D1STR)
Locate 1,0
Print result
Do
Loop
Function BinaryStr2Byte( SysInString as String ) as Byte
BinaryStr2Byte = 0
For SysStringIndex = 1 to 8 '8 bytes
SysStrData = SysInString(SysStringIndex)
Rotate BinaryStr2Byte left
IF (SysStrData - 48) = 1 then Set binaryStr2Byte.0 ON
IF (SysStrData - 48) = 0 then Set binaryStr2Byte.0 OFF
Next
End Function
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using version 95.009.
I wrote the below program where I am trying to convert a binary string variable to its equivalent number value,
but all I get is wrong values.
Is this possible or am I doing something wrong ?
;Chip Settings
chip 18F4550,48
config PLLDIV=1, OSC=HSPLL_HS, MCLRE=ON, LVP=OFF
;Defines (Constants)
define LCD_IO 2
define LCD_CB PORTC.1
define LCD_DB PORTC.2
'
Dim D1str, D1, D2, D3, D4, D5, D6, D7, D8, HEXstr, HEXstr2 as string
Dim Dinput1 as word
Dim Dinput2 as byte
'
wait 300 ms
'
'Values used to create my binary string
D1 = "1" ;D1 to D8 = 11010110b = 214 = D6h
D2 = "1"
D3 = "0"
D4 = "1"
D5 = "0"
D6 = "1"
D7 = "1"
D8 = "0"
'
D1str = D1 + D2 + D3 + D4 + D5 + D6 + D7 + D8 ;The binary string to be converted
locate 0, 0
print D1str; prints out 11010110b which is correct
'
'Tried these to do the conversion
Dinput1 = VAL(D1str)
Dinput2 = BcdToDec_GCB (D1str)
HEXstr = HEX(Dinput1)
HEXstr2 = HEX(Dinput2)
'
'My results - which are all wrong
locate 1, 0 ; ALL the below values do NOT = 11010110b
print Dinput1 ; prints out 62
print ":"
print Dinput2 ; prints out 143
print ":"
print Hexstr ; prints out 3E
print ":"
print HEXstr2 ; prints out 8F
You need a different method.
VAL is string to byte
BCDtoDEC_GCB is Binary encoded work to string
HEX is byte to String
You need BinaryStr2Byte. An 8 character string in Binary notation to byte. See below.
And, consist this for declarations:
Dim D1, D2, D3, D4, D5, D6, D7, D8 as string * 1
Dim D1str as string * 8
Enjoy
Last edit: Anobium 2017-01-23
Thanks!
I'll try this.
I have taken Evan's function code above and simplified. ( I could not get it working as is)
This tested ok on 16F18855 with LCD
Thanks!
I will try it.
Thanks!
I will try it.
William,
It works with the 18F4550, too.
Thanks.