Home AMX Forum NetLinx Studio
Options

Picking Information our of String with Variable Lengths

alecgalecg Junior Member
I'm sure I'm missing something obvious here, please help!

String from Device:
vol "Channel 1" 59353\r

I need to convert that 59535 to a number and use it elsewhere in the code.

I can remove the whole 'vol "Channel 1"', but how do I remove the carriage return?
I can't use left_string or right_string because that number could be 1 character or it could be 5.

Comments

  • viningvining X Member
    Set length string, lenght string -1
  • GregGGregG Just some guy...
    Assuming you had some variable:
    cBuffer with contents of vol "Channel 1" 59353\r

    You can do:
    Remove_String(cBuffer," '"Channel 1"' ",1)
    nVal = Atoi(cBuffer)
    

    because, by default, Atoi() ignores non-numeric characters in the string you pass it and just converts the first set of consecutive numbers it sees.
  • viningvining X Member
    GregG wrote: »
    Assuming you had some variable:
    cBuffer with contents of vol "Channel 1" 59353\r

    You can do:
    Remove_String(cBuffer," '"Channel 1"' ",1)
    nVal = Atoi(cBuffer)
    

    because, by default, Atoi() ignores non-numeric characters in the string you pass it and just converts the first set of consecutive numbers it sees.

    I also do this on occasion but I always felt that it relies on the voodoo that takes place in a function that you have no control over. Not really the intent of the function but it does indeed stop converting upon the 1st non numeric.
  • TonyAngeloTonyAngelo Code Monkey
    alecg wrote: »
    I'm sure I'm missing something obvious here, please help!

    String from Device:
    vol "Channel 1" 59353\r

    I need to convert that 59535 to a number and use it elsewhere in the code.

    I can remove the whole 'vol "Channel 1"', but how do I remove the carriage return?
    I can't use left_string or right_string because that number could be 1 character or it could be 5.
    define_function char[] fnGetNumber(char sArgString[])
    {
         stack_var integer nMyStart
         stack_var integer nMyEnd
    
         nMyStart = find_string(sArgString, '" ', 1)+2
         nMyEnd = find_string(sArgString,"$0D",1)-1
    
         RETURN mid_string(sArgString,nMyStart,nMyEnd-nMyStart);
    }
    
  • ericmedleyericmedley Senior Member - 3709 Posts
    vining wrote: »
    I also do this on occasion but I always felt that it relies on the voodoo that takes place in a function that you have no control over. Not really the intent of the function but it does indeed stop converting upon the 1st non numeric.

    I agree, I avoid it as well but mainly from muscle memory. Other development environments are kinda hit and miss on atoi; some ignoring non-numerical but others dumping you out if character one is not a number. I don't necessarily think this falls under "best practice" or not. There's no reason I can see not to use the fact that it ignores non-numerics. Atoi is a somewhat spendy projcess CPU-wise but so are any string searches. It might be intellectually interesting to see how efficient one process is over another. But I ain't got time fo' dat...
  • feddxfeddx Junior Member
    Voodoo?
    vining wrote: »
    I also do this on occasion but I always felt that it relies on the voodoo that takes place in a function that you have no control over. Not really the intent of the function but it does indeed stop converting upon the 1st non numeric.

    Some of us live on the Voodoo of intrinsic functions. Really.
Sign In or Register to comment.