Announcement

Collapse
No announcement yet.

Convert String to Int

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert String to Int

    Maybe I just missed it somewhere, but I've a number stored as a string that I want to convert to a number to use in say a gauge. How does one do this with Goldelox?

    var somevariable = "12345"; //Lets not worry about how it got stuck there... just how to get it out...

  • #2
    Code:
    #platform "GOLDELOX"
    
    #inherit "4DGL_16bitColours.fnc"
    
    var somevariable[4] ;
    
    func main()
        var i, ch, num ;
        to (somevariable) ;
        print("12345") ;
    
        print([STR] somevariable,"\n\n") ;
    
        i := 0 ;
        num := 0 ;                      // init number
        repeat
            ch := somevariable[i / 2] ; // extract word
            if (i & 1)                  // keep high or low byte, as appropriate
                ch >>= 8 ;
            else
                ch &= 0xff ;
            endif
            if (ch == 0) break ;        // exit on null
            num *= 10 ;                 // mult what we have so far by 10
            num += ch & 0x0f ;          // add in current character, after converting to number
            i++ ;                       // move to next char
        forever
        print(num, "\n\n" ) ;
        repeat         // maybe replace
        forever                     // this as well
    
    endfunc
    Mark

    Comment


    • #3
      so, 'somevariable' is being populated by a string that came in from serin() using some code I stole from somewhere else around here, so I can't do "print("12345")" since I don't know what 'somevariable' is going to be, though it'll have a number from 0-1023...

      Comment


      • #4
        The first part was just to set up the information as per your example, the i:=0 onwards shoud be the solution for what you need
        Mark

        Comment

        Working...
        X