PDA

View Full Version : String to number conversion



dj.lambert
- 12th September 2010, 21:03
Hi All
i have a project where i use a pic to communicate with a power analyser.
the problem is all i can get from the analyser is a string, the string length is dependant on the measurements i request.but the returned format is for example "1.234567E1V 1.234567E-1A"

any ideas how to convert this to real usable numbers?

i have searched the forum but nothing similar seems to have been covered.

thanks in advance.

be80be
- 13th September 2010, 02:13
You may be able to look at this and work it out in PBP

It's from sowrdfish basic


Public Function StrToDec(pValue As String) As LongWord
Dim Value As Byte
Dim IsSigned As Boolean

FTextAddress = @pValue
Result = 0
Value = FTextIn
IsSigned = false
If Value = Byte("-") Then
IsSigned = true
Value = FTextIn
EndIf
While Value <> 0
Result = 10 * Result + (Value - 48)
Value = FTextIn
Wend
If IsSigned Then
Result = Not Result
Inc(Result)
EndIf
End Function
{

Jerson
- 13th September 2010, 04:34
PBP does not support floats. So, converting those numbers will be difficult.

However, if like a challenge, this is a rough draft of how I would try my hand at it. This is not the best code that can be written for this though; just a pointer.



IntResult = 0
FractionResult = 0
Exponent = 0
Units = ""
WhichPart = 0 ' 0 = Integer part, 1 = fractional part, 2 = exponent part
ExpFlag = 0 ' set to 1 when reading the exponent
Pointer = points to start of number

while (character@Pointer != end of string)
select case WhichPart
case IntPart: ' whichpart = 0
IntResult = IntResult * 10
select case character@pointer
case "0" to "9":
IntResult = IntResult + (character@pointer-'0')
case "."
WhichPart = 1 ' now on, gather fractional part
end select
case FractionalPart: ' whichpart = 1
FractionResult = FractionResult * 10
select case character@pointer
case "0" to "9":
FractionResult = FractionResult + (character@pointer-'0')
case "e" OR "E":
WhichPart = 2 ' now on, gather Exponent
end select
case ExponentPart: ' whichpart = 2
Exponent = Exponent*10
select case character@pointer
case "0" to "9":
Exponent = Exponent + (character@pointer-'0')
case else
Units = character@pointer
end select
end select

' move to the next character
increment Pointer by 1
wend


This is purely 'pseudo code' and not PBP though it looks a bit alike. At the end of this routine, you should have 4 values - Integer result, Fractional Result, Exponent and Units either V or A. Now, you combine them as you deem fit and have your resulting answer ;)

Regards

BrianS
- 13th September 2010, 15:54
Try this works great for me. Handles string to float conversions and can be connected to other microcontrollers. Also supports Picbasic Pro with examples.

http://www.micromegacorp.com/umfpu-v3.html

Bryan

dj.lambert
- 14th September 2010, 22:29
thanks for all the ideas.