Picking Information our of String with Variable Lengths
alecg
Posts: 30
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.
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.
0
Comments
cBuffer with contents of vol "Channel 1" 59353\r
You can do:
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.
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...
Some of us live on the Voodoo of intrinsic functions. Really.