View Full Version : Turning Text Numbers into a 32bit number
01-12-2008, 02:57 AM
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 := 2
MyArray := 4
MyArray := 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.
01-12-2008, 03:57 AM
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.
01-12-2008, 06:41 AM
Great. Thanks mike. Here's a simple function I made that does it pretty well(I think).
if(num => 48 AND num =< 57) 'If the character input has a hex value between 48 and 57(0-9)
return (num - 48)
01-12-2008, 06:55 AM
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
"9" rather than 57
which makes it more readable!
01-12-2008, 06:59 AM
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...
01-12-2008, 07:17 AM
If you use·the Format object, you can just do
· fmt: "Format"
· long i
· long j
· byte value
· value = "2"· 'simulate a number as string
· value = "4"
· value = "1"
· value = 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
· then j will hold 241
but in this case atoi is simpler.
01-12-2008, 07:17 AM
We also know what to do against it:
surround your code with [ code ] [ /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
Post Edited (deSilva) : 1/11/2008 11:24:05 PM GMT
01-12-2008, 07:49 AM
I love it. Thanks deSilva.
Again, I love it Peter!
(Oh, and thanks Phil for the formatter!)