Results 1 to 16 of 16

Thread: [RESOLVED] Convert string to number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Resolved [RESOLVED] Convert string to number

    Hi,

    First the CINT etc commands will not work for this.

    I have the following value 2A000000412DF21B in string displayed here as hex. It should convert to 3026418950686503451 but instead I'm gettting 3026418950686503424.

    I take the string and convert it to bits 0010101000000000000000000000000001000001001011011111001000011011 then using a simple function I loop through and calculate the value.

    I think I'm hitting the max of the double, I'm not sure but I can't figure out how to convert what I have in string to the number above.

    Code:
    Dim dbA As Double, dbB As Double, intX As Integer
            Try
                dbA = 1 : dbB = 0
                For intX = Len(strB) To 1 Step -1
                    dbB = dbB + (Mid(strB, intX, 1) * dbA)
                    dbA = dbA * 2
                Next
            Catch ex As Exception
                  msgbox ex.message.tostring
            End Try
            Return dbB
    Any advise should be appreciated

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Convert string to number

    You state that you have it displayed as Hex, but what IS it? The value is too large for an integer, but not too large for a long. However, if it is a string representation of a hex number, then I would split it into two chunks. The low four bytes and the high four bytes. I would then convert each into a Long (for ease of use), then take the long that had the high four bytes, bit shift them by 32 places, and add them to the low four bytes.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Re: Convert string to number

    Hi,

    It is a unique identifier for a GPS tracking unit. The data is sent from the unit in binary format, 8 bytes represent this unique ID.

    I don't follow how breaking it up to 4 bytes and converting them works. If I do that I get 704643072 and 1093530139. I don't see how that relates to 3026418950686503451. By the way how would you bit shift in vb.net?

    The odd thing for me is that I'm only out by 27 on my number compared to what it should be.

    Thanks for the advise so far,
    Jim
    Last edited by jim@satcomtechnology; Jun 8th, 2009 at 09:53 AM.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Convert string to number

    vb.net Code:
    1. Debug.WriteLine(Convert.ToUInt64("0010101000000000000000000000000001000001001011011111001000011011", 2))
    2. 'Output: 3026418950686503451

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Convert string to number

    Yeah, don't convert something like that to a double. A lot of numbers cannot be accurately represented by a double.

    But, it should convert directly as ForumAccount demonstrates (you could use the string directly and use base 16).
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Re: Convert string to number

    is there a simple way of going from 2A000000412DF21B to 3026418950686503451 ?

    Thanks,
    Jim

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Convert string to number

    Yes, do as ForumAccount shows, but use base 16 instead and your 'raw' string. It'll convert to an unsigned 64bit integer.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

    Re: Convert string to number

    Code:
    Function GetValue(ByVal hexV As String) As Long
            Return CLng("&H" & hexV)
        End Function

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Convert string to number

    I'm still a bit unclear on what is coming in. Normally, I would leave it alone, but when you convert to a 64 bit integer (a Long or unsigned long), you will end up with an 8 byte integer. My concern is that you are taking an 8 byte item and turning it into an 8 byte item. Is that an 8 byte string that is being turned into an 8 byte integer, or is it an 8 byte integer that is being turned into a string that is being turned into an integer? It isn't clear yet, whether you are actually getting a string representation of the number, or the number itself.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Question Re: Convert string to number

    Quote Originally Posted by SJWhiteley View Post
    Yes, do as ForumAccount shows, but use base 16 instead and your 'raw' string. It'll convert to an unsigned 64bit integer.
    I tried that, as I have the string in HEX I converted back to "raw" string which is what I would normally receive, then I did the convertion to base 16.

    Code:
                Dim strTMP As String
                strTMP = "2A000000412DF21B"
    
                Dim intX As Integer, strEvent As String = ""
    
                For intX = 1 To strTMP.Length Step 2
                    strEvent &= Chr(Val("&H" & Mid(strTMP, intX, 2)))
                Next
    
                Debug.WriteLine(Convert.ToUInt64(strEvent, 16))
    I get the following error

    Convert.ToUInt64(strEvent, 16) Run-time exception thrown : System.FormatException - Could not find any recognizable digits.
    What am I doing wrong?

    Thanks for all the help.
    Jim

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Re: Convert string to number

    Quote Originally Posted by Shaggy Hiker View Post
    I'm still a bit unclear on what is coming in. Normally, I would leave it alone, but when you convert to a 64 bit integer (a Long or unsigned long), you will end up with an 8 byte integer. My concern is that you are taking an 8 byte item and turning it into an 8 byte item. Is that an 8 byte string that is being turned into an 8 byte integer, or is it an 8 byte integer that is being turned into a string that is being turned into an integer? It isn't clear yet, whether you are actually getting a string representation of the number, or the number itself.
    Shaggy,

    This is the hex format of the string I receive, &H2A000000412DF21B, convert that to the raw string and you will see what data I receive.

    I now need to convert that so it turns into 3026418950686503451.

    Jim

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Re: Convert string to number

    Quote Originally Posted by Dark Anima View Post
    Code:
    Function GetValue(ByVal hexV As String) As Long
            Return CLng("&H" & hexV)
        End Function
    Doh! Why didn't I think of that, I have all the functions to convert my string to a HEX value!

    MsgBox(CLng("&H" & StringToHex(strEvent).ToString))

    And that gives me 3026418950686503451!

    Thank you very much!

    Jim

  13. #13
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Convert string to number

    Quote Originally Posted by jim@satcomtechnology View Post
    I tried that, as I have the string in HEX I converted back to "raw" string which is what I would normally receive, then I did the convertion to base 16.

    Code:
                Dim strTMP As String
                strTMP = "2A000000412DF21B"
    
                Dim intX As Integer, strEvent As String = ""
    
                For intX = 1 To strTMP.Length Step 2
                    strEvent &= Chr(Val("&H" & Mid(strTMP, intX, 2)))
                Next
    
                Debug.WriteLine(Convert.ToUInt64(strEvent, 16))
    I get the following error



    What am I doing wrong?

    Thanks for all the help.
    Jim
    &h is not recognizable as base 16 character(s). There's no need to convert a string to a hex, back to a string then again back to a long.

    Just use:

    Convert.ToUInt64("2A000000412DF21B", 16)

    No need to add the &h, looping, string conversions, or any superfluous stuff. This is a very common thing to do, that's why there's the ability to convert directly from various bases.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Re: Convert string to number

    I see, when you said raw string I presumed the string that I received. It's only in HEX so that I that I can read it.

    So what is best the CLNG Dark Anima or ToUInt64?

    Jim

  15. #15
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Convert string to number

    CLng is a hold-over from VB6, and are designed to replicate the results generated by old VB6 code, which may or may not be important. the Convert methods are 'native' to .NET (They may give different results under some circumstances). Ultimately, the CLng function will call the Convert.ToInt64() method as it goes through various validation and checking routines.

    I'd always use the .NET methods, personally, if only to avoid snarky 'toy language' comments from the C# programmers.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Location
    Bweeng, Ireland
    Posts
    69

    Resolved Re: Convert string to number

    Thanks, and thank you all for the help!

    Jim

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
  •  



Click Here to Expand Forum to Full Width