Shop OBEX P1 Docs P2 Docs Learn Events
Turning Text Numbers into a 32bit number — Parallax Forums

Turning Text Numbers into a 32bit number

PhilldapillPhilldapill Posts: 1,283
edited 2008-01-11 23:49 in Propeller 1
I'm tying to make a routine that can translate a·character·array into a real number. The array will only consist of characters 0-9, so no letters or other characters will be included. What I want to do, is make a routine that will go through the array, element by element and turn the character into a real number that I can later use in the code. Basically, if I had an array like this:
MyArray[noparse][[/noparse]0] := 2
MyArray[noparse][[/noparse]1] := 4
MyArray[noparse][[/noparse]2] := 1

I would want to turn it into the number 241 in a decimal. How do I do this, other than making a routine that just matches the character code up to a 1 digit number, e.g., char "1" = character 49, char "2" = character 50, etc. etc.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-11 19:57
    The "format" object (or the "numbers" object) in the object exchange should be able to do what you want. Download it and have a look. It essentially implements the input and output string to/from number routines in C. It works for integers, but you could use the floating point package (also in the object exchange) to convert the integers to floating point. If you want to include embedded decimal points, you'll probably have to write your own routines using the floating point package or modify the routines in the "numbers" object to work with floating point rather than integer arithmetic.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-11 22:41
    Great. Thanks mike. Here's a simple function I made that does it pretty well(I think).
    PUB CharToDec(num)
    if(num => 48 AND num =< 57) 'If the character input has a hex value between 48 and 57(0-9)
    return (num - 48)
    else
    return -1
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-11 22:55
    Up to this moment is was not quite clear to me what you intended to do.. But note:
    You can also write
    "0" rather than 48
    and
    "9" rather than 57
    which makes it more readable!
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-11 22:59
    Really? Yeah, that would be more readable... Btw, excuse the formatting. I'm sure you guys now about the forum deleting spaces when you post code...
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-11 23:17
    If you use·the Format object, you can just do
    OBJ
    · fmt: "Format"
    VAR
    · long i
    · long j[noparse][[/noparse]0]
    · byte value[noparse][[/noparse]4]
    PUB
    · value[noparse][[/noparse]0] = "2"· 'simulate a number as string
    · value[noparse][[/noparse]1] = "4"
    · value[noparse][[/noparse]2] = "1"
    · value[noparse][[/noparse]3] = 0·· 'must have a closing null
    · i := fmt.atoi(@value) 'convert signed decimal string to integer value

    Now i holds the value 241

    You can also use
    · fmt.sscanf(@value,string("%d"),@j)
    · then j[noparse][[/noparse]0] will hold 241
    but in this case atoi is simpler.

    regards peter
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-11 23:17
    We also know what to do against it:
    surround your code with [noparse][[/noparse] code ] [noparse][[/noparse] /code ] and leave a space after each opening bracket within your code.

    You can also use this fine link to convert your text into pretty printing
    www.phipi.com/format

    Post Edited (deSilva) : 1/11/2008 11:24:05 PM GMT
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-11 23:49
    I love it. Thanks deSilva.

    Again, I love it Peter!

    (Oh, and thanks Phil for the formatter!)
Sign In or Register to comment.