String to number conversion


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    14

    Default String to number conversion

    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.

  2. #2
    Join Date
    Sep 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    You may be able to look at this and work it out in PBP

    It's from sowrdfish basic

    Code:
    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
    {

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    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.

    Code:
    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
    Last edited by Jerson; - 13th September 2010 at 04:37. Reason: Added some info

  4. #4
    Join Date
    Jul 2010
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Oct 2008
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    thanks for all the ideas.

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts