[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
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.
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
Re: Convert string to number
vb.net Code:
Debug.WriteLine(Convert.ToUInt64("0010101000000000000000000000000001000001001011011111001000011011", 2))
'Output: 3026418950686503451
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).
Re: Convert string to number
is there a simple way of going from 2A000000412DF21B to 3026418950686503451 ?
Thanks,
Jim
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.
Re: Convert string to number
Code:
Function GetValue(ByVal hexV As String) As Long
Return CLng("&H" & hexV)
End Function
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.
Re: Convert string to number
Quote:
Originally Posted by
SJWhiteley
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
Quote:
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
Re: Convert string to number
Quote:
Originally Posted by
Shaggy Hiker
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
Re: Convert string to number
Quote:
Originally Posted by
Dark Anima
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
Re: Convert string to number
Quote:
Originally Posted by
jim@satcomtechnology
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.
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
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.
Re: Convert string to number
Thanks, and thank you all for the help!
Jim