A guess. You need to dimension the string to 16 characters? The default string length is chip dependent - hence, the ask for a simple compilable piece of code.
Dim My_STR as string * 16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
val(My_STR)=val("100000110010")
if you look at the hex of 100000110010, it is 17487895BA
A word can only hold 4 hex characters, so it comes out to hex 95BA which equals 38330
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have this simple program:
Dim Input_Value as word
Dim My_STR as string
'
Input_Value = 2098
The "print Val(My_STR)" gives an unexpected value. What am I doing wrong?
Dont know. Post some code that compiles for us. Needs chip etc.
A guess. You need to dimension the string to 16 characters? The default string length is chip dependent - hence, the ask for a simple compilable piece of code.
Dim My_STR as string * 16
The val of string "0000100000110010" would be what?
Last edit: Jim giordano 2020-01-09
ohhh. yes... 100000110010 not 00001000000110010
and certainly not 2098
Last edit: Jim giordano 2020-01-09
I just spotted it...
And, where My_STR is a string of that contains the string 00001000000110010....... You have set the string to the binary value of Input_value
Try
Last edit: Anobium 2020-01-09
I'm using an 18F4550 with GCBversion 98.06.
The "0000100000110010" is what My_STR = WordToBin(Input_Value) prints out when Input_Value = 2098.
I just tried " Dim My_STR as string * 16" but got the same results'
Here is the compilable code:
;Chip Settings
#chip 18F4550,48
#config PLLDIV=1, CPUDIV=OSC1_PLL2, OSC=HSPLL_HS, LVP=OFF, MCLRE=ON
;Defines (Constants)
#define LCD_IO 2
#define LCD_CB PORTE.1
#define LCD_DB PORTE.0
'
;Defines (Constants)
#define LCD_IO 2
#define LCD_CB PORTE.1
#define LCD_DB PORTE.0
Dim Input_Value as word
Dim My_STR as string * 16
'***********
Input_Value = 2098
See my previous post....
val(My_STR)=val("100000110010")
if you look at the hex of 100000110010, it is 17487895BA
A word can only hold 4 hex characters, so it comes out to hex 95BA which equals 38330
Anobium,
Thats the same statement I have under locate 1, 0. I'm trying to convert back to a decimal value.
you want to convert from a binary string to decimal? There is no built in function for that, you will have to write one. Shouldn't be too hard.
This works but could probably be optimized for size or speed, whichever is more important-
Correct. Very simple to write a function to do this
I'll try it. Thanks for your help.
Jim,
Your code worked. Thanks!