Menu

converting from WordToBin to VAL

Help
MBB
2020-01-09
2020-01-09
  • MBB

    MBB - 2020-01-09

    I have this simple program:

    Dim Input_Value as word
    Dim My_STR as string
    '
    Input_Value = 2098

      locate 0, 0
      print Input_Value ;Correctly prints out 2098
    
        My_STR = WordToBin(Input_Value)
    
          locate 1, 0
          print My_STR ;Correctly prints out 0000100000110010 which is eqivalent to 2098
    
            locate 2, 0
              print Val(My_STR) ;prints out 38330 - Shouldn't it print out the Input_Value
    

    The "print Val(My_STR)" gives an unexpected value. What am I doing wrong?

     
  • Anobium

    Anobium - 2020-01-09

    Dont know. Post some code that compiles for us. Needs chip etc.

     
  • Anobium

    Anobium - 2020-01-09

    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

     
  • Jim giordano

    Jim giordano - 2020-01-09

    The val of string "0000100000110010" would be what?

     

    Last edit: Jim giordano 2020-01-09
  • Anobium

    Anobium - 2020-01-09

    ohhh. yes... ‭100000110010‬ not 00001000000110010

     
  • Jim giordano

    Jim giordano - 2020-01-09

    and certainly not 2098

     

    Last edit: Jim giordano 2020-01-09
  • Anobium

    Anobium - 2020-01-09

    I just spotted it...

    locate 2, 0
    print Val(My_STR) ;prints out 38330 - Shouldn't it print out the Input_Value
    

    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

    locate 2, 0
    print My_STR 
    
     

    Last edit: Anobium 2020-01-09
  • MBB

    MBB - 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

    locate 0, 0
      print Input_Value ;Correctly prints out 2098
    
        My_STR = WordToBin(Input_Value)
    
          locate 1, 0
          print My_STR ;Correctly prints out 0000100000110010 which is eqivalent to 2098
    
            locate 2, 0
              print Val(My_STR) ;prints out 38330 - Shouldn't it print out the Input_Value (2098) ?
    
     
  • Anobium

    Anobium - 2020-01-09

    See my previous post....

     
  • Jim giordano

    Jim giordano - 2020-01-09

    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

     
  • MBB

    MBB - 2020-01-09

    Anobium,

    Thats the same statement I have under locate 1, 0. I'm trying to convert back to a decimal value.

     
  • Jim giordano

    Jim giordano - 2020-01-09

    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.

     
  • Jim giordano

    Jim giordano - 2020-01-09

    This works but could probably be optimized for size or speed, whichever is more important-

       locate 3,0
       dim t2 as word
       dim ii
       for ii=1 to len(My_STR)    ' same as My_STR(0)
          t2=t2*2+(My_STR(ii)-48)
       next
       print t2
    
     
  • Anobium

    Anobium - 2020-01-09

    Correct. Very simple to write a function to do this

     
  • MBB

    MBB - 2020-01-09

    I'll try it. Thanks for your help.

     
  • MBB

    MBB - 2020-01-09

    Jim,

    Your code worked. Thanks!

     

Log in to post a comment.