473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Hex conversion to Int - Bug or My misunderstanding of VB.Net

I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
--
Dennis in Houston
Nov 21 '05 #1
12 8792
You have one too many of these --> "("... try this Cint("&HC5798A2F")

-Fabricio

"Dennis" wrote:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
--
Dennis in Houston

Nov 21 '05 #2
Yes, it was a typo but I get the overflow exception when I leave out the
extra (

"Fabricio" wrote:
You have one too many of these --> "("... try this Cint("&HC5798A2F")

-Fabricio

"Dennis" wrote:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
--
Dennis in Houston

Nov 21 '05 #3
I figured... just checking to see if you were still active on this thread :-)

Ok... here's the deal. It doesn't work because the hex number you're using,
C5798A2F, is too big for a 32-bit integer. The max value allowed is
2,147,483,647. Try a smaller number (string number) and see if that works.

-Fabricio
fg*************@yahoo.com

"Dennis" wrote:
Yes, it was a typo but I get the overflow exception when I leave out the
extra (

"Fabricio" wrote:
You have one too many of these --> "("... try this Cint("&HC5798A2F")

-Fabricio

"Dennis" wrote:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
--
Dennis in Houston

Nov 21 '05 #4
On 2004-09-12, Dennis <De****@discussions.microsoft.com> wrote:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?


It's just a bug, it's not your misunderstanding of anything. My
favorite inconsistency here isn't the above, it's that

Dim i as Integer = CInt("&H" & Hex(someInteger))

can throw an exception. That tends to catch folks by surprise.

Technically, I suppose that one could make the argument that since
there's virtually no documentation on the correct behavior of CInt/CLng
and all the C??? functions in general, that almost any behavior could be
deemed officially "correct". But one would hope for consistency at
least, I would think.

Something useful though, if you need to do this, I'd strip off the "&H"
and use the Int32.Parse function, which is much more robust and much
more flexible.

Int32.Parse("C5798A2F", Globalization.NumberStyles.HexNumber)

correctly returns -981890513
Nov 21 '05 #5
David,
Something useful though, if you need to do this, I'd strip off the "&H"
and use the Int32.Parse function, which is much more robust and much
more flexible.

Int32.Parse("C5798A2F", Globalization.NumberStyles.HexNumber)

correctly returns -981890513

Sure, you'll remember what's going on while you write this code, but six
months later it's incredibly easy to forget that you've got some other
variable someplace while you know that is told in the Microsoft documents
that the advices convert class in VBNet are the VisualBasic convert methods
because they are normally more reliable and much more flexible and nobody
knows anymore why you did this.

In this case I just would make a workaround the problem as this.

Dim a As String = "&HC5798A2F".Replace("&H", "&HFFFFFFFF")
'Workaround Cint cannot handle negative hexa in the righ way strings
Dim b As Integer = CInt(a)
'And better before it is changed in future
Dim c As Long = CLng(a)
Dim d As Long = CInt(c)

I hope this helps?

Cor
Nov 21 '05 #6
I never had this problem, so I tried it, however it is not generic, in my
opinion is that the fact with the routine beneath

\\\'workaround for negative hexastring to integer
Dim a As String = "&HC5798A2F".Replace("&H", "&HFFFFFFFF")
Dim b As Long = CLng(a)
Dim c As Integer
If b < 0 Then
c = CInt(b)
Else
c = CInt("&HC5798A2F")
End If
///
Nov 21 '05 #7
* =?Utf-8?B?RGVubmlz?= <De****@discussions.microsoft.com> scripsit:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and
The hexadecimal integer literal '&HC5798A2F' is treated as 'Int32':

\\\
MsgBox(&HC5798A2F.GetType().ToString()) ' 'System.Int32'.
///
Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.
FFFFFFFFC5798A2F(16) = 18446744072727661103(10)

This value cannot be stored in an 'Int64', so we interpret the number as
signed number. The sign bit is 1 which indicates a negative number, so
the result is the negative number mentioned above.
However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?


'CInt' tries to parse the number. It doesn't try to parse an 'Int32',
but it parses an integer number. The result is the 'Int64' value
3313076783 which cannot be stored in an 'Int32'. As a consequence, an
exception is thrown.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8
David,

* David <df*****@woofix.local.dom> scripsit:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
It's just a bug, it's not your misunderstanding of anything. My
favorite inconsistency here isn't the above, it's that

Dim i as Integer = CInt("&H" & Hex(someInteger))

can throw an exception. That tends to catch folks by surprise.


Visual Basic Language Reference -- Type Conversion Functions
<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vagrptypeconversion.asp>

.... says:

"
If the 'expression' passed to the function is outside the range of the
data type to which it is being converted, an error occurs.
"

I assume 'CInt' and 'CLng' work as follows (simplified):

1. Treat the string as representation of an unsigned integer number, if
no sign is specified. To do that, first parse the number into the
largest integer datatype, this is 'System.Int64'. For values larger
than 'Int64.MaxValue' treat the number as signed number to make it
smaller.

2. Then try to convert the 'Int64' to the destination datatype. If the
number does not fit into this datatype, an exception is thrown.

Sample:

\\\
Dim s As String = "&H" & Hex(Integer.MaxValue) ' "&H80000000".
Dim i As Integer = CInt(s) ' Overflow exception.
///

"&H80000000" is converted to an integer number (/not/ the 'Int32'
datatype). The result is 2147483648, which is a positive integer
number. This number is larger than the maximum value that can be stored
in an 'Int32', 2147483647. An exception is thrown.

I think this behavior is "by design", and I would have chosen this
behavior too when implementing 'C*' methods. String literals containing
string representations of integer values should always be treated as
representations of the largest possible integer datatype, independent
from the 'C*' method called on the string expression.
Technically, I suppose that one could make the argument that since
there's virtually no documentation on the correct behavior of CInt/CLng
and all the C??? functions in general, that almost any behavior could be
deemed officially "correct". But one would hope for consistency at
least, I would think.
I think the behavior is consistent. Parsing an integer number cannot be
compared with parsing a string literal representing an integer number.
Int32.Parse("C5798A2F", Globalization.NumberStyles.HexNumber)

correctly returns -981890513


Mhm... For me, the VB.NET implementation is "correct".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9
Thanks to all who replied. Apparently, there is an inconsistency in the way
VB.Net treats conversions from literals to strings. For those who are
interested, below is what I ended up with and it seems to work:

Friend Function ConvByteArraytoInteger(ByVal b As Byte(), Optional ByVal ln
As Integer = 0, Optional ByVal sidx As Integer = 0) As Long
'converts a byte array to an integer starting at sidx in byte array
of length ln
Dim i As Integer
Dim k As Integer
Dim a As String
If ln = 0 Then ln = UBound(b) + 1
Dim h As StringBuilder = New StringBuilder(32)
k = sidx + ln - 1
For i = sidx To k
h.Append(Hex(b(i)).PadLeft(2, "0"c))
Next
If CInt("&H" & h.ToString.Substring(0, 1)) > &H7 Then
a = "&H" & h.ToString.PadLeft(16, "F"c)
Else
a = "&H" & h.ToString
End If
Return CLng(a)

End Function

"Dennis" wrote:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
--
Dennis in Houston

Nov 21 '05 #10
On 2004-09-12, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
David,

* David <df*****@woofix.local.dom> scripsit:
I get the following results:

Cint(&HC5798A2F) returns -981890513 as it should and

Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should.

However when using a string

Cint("(&HC5798A2F") returns an overflow exception.

Shouldn't the Cint return the same value for the same hex format number input?
It's just a bug, it's not your misunderstanding of anything. My
favorite inconsistency here isn't the above, it's that

Dim i as Integer = CInt("&H" & Hex(someInteger))

can throw an exception. That tends to catch folks by surprise.


Visual Basic Language Reference -- Type Conversion Functions
<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vagrptypeconversion.asp>

... says:

"
If the 'expression' passed to the function is outside the range of the
data type to which it is being converted, an error occurs.
"


Which is pretty vague, as we've seen. I can look at the actual behavior
of cint/clng and come up with something like the description you've made
below, but I don't think anyone could seriously maintain that they could
read the above and say with certainty what the behavior of clng/cint
would be when faced the the type of hex strings we're dealing with.

Which is no big deal for the most part. As I've said before, the real
spec to the VB language is the behavior of the compiler and libraries,
not the written documentation. And I don't think the written spec is
intended to be used in the same way an ANSI or ECMA spec is intended to
be used.
Technically, I suppose that one could make the argument that since
there's virtually no documentation on the correct behavior of CInt/CLng
and all the C??? functions in general, that almost any behavior could be
deemed officially "correct". But one would hope for consistency at
least, I would think.


I think the behavior is consistent. Parsing an integer number cannot be
compared with parsing a string literal representing an integer number.


Well, they seem awfully comparable to me. In fact, I strongly suspect
that if you asked most VB programmers to define the functions, that's
exactly the definition they'd give: that cint/clng convert the string
representation of a literal.

But there's no way this can be resolved, since there's no standard
measure of things like "consistency" and "elegant". The fact that
Integer-to-String and String-to-Integer don't round trip seems
problematic to me, but again, anybody can just say "they aren't
problematic to me" and I have no objective measure of the importance of
orthogonality to point to.

Int32.Parse("C5798A2F", Globalization.NumberStyles.HexNumber)

correctly returns -981890513


Mhm... For me, the VB.NET implementation is "correct".


That's nice. But "correctly" here referred to the fact that it fits the
needs of the OP, not to a more general statement about the language.

Nov 21 '05 #11
On 2004-09-12, Cor Ligthert <no**********@planet.nl> wrote:
David,
Something useful though, if you need to do this, I'd strip off the "&H"
and use the Int32.Parse function, which is much more robust and much
more flexible.

Int32.Parse("C5798A2F", Globalization.NumberStyles.HexNumber)

correctly returns -981890513

Sure, you'll remember what's going on while you write this code, but six
months later it's incredibly easy to forget that you've got some other
variable someplace


What other variable? There are no other variables here.

I know you're trying to be snarky here, but it helps if your statement
makes sense.
Nov 21 '05 #12
* David <df*****@woofix.local.dom> scripsit:
[Stuff I agree with]
Int32.Parse("C5798A2F", Globalization.NumberStyles.HexNumber)

correctly returns -981890513


Mhm... For me, the VB.NET implementation is "correct".


That's nice. But "correctly" here referred to the fact that it fits the
needs of the OP, not to a more general statement about the language.


Yeah, with correct I mean that I am able to find an explanation of the
behavior, nothing more.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John Cho | last post by:
Class Cartesian { double x; double y; public: Cartesian( ) { x = 0.0 ; y = 0.0;} Cartesian(double x1, double y1) { x = x1; y = y1;} }; class Polar { double radius;
16
by: Der Andere | last post by:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be...
8
by: Steve Wasser | last post by:
I'm pulling SQL data into a dataset to be used to perform some math against. I asked this question earlier, but the answer someone gave me left me with further questions. The SQL data is stored as...
3
by: Steve Graddy | last post by:
I am trying to convert the following VB.Net code to C# and I am getting the compiler error: VB.NET code: '' Create a delegate that will be called asynchronously Private Delegate Function...
5
by: John J. Hughes II | last post by:
I need to convert a byte array to a structure with conversion on the byte order. I found the below deserilizer which works for getting the data converted to a structure but it's in the wrong byte...
6
by: Shawn Wildermuth | last post by:
I have a web project whose web.config has a ConnectionString setup. I have a second assembly with my Typed DataSets. The designer has created an App.config file with a connection string in it for...
11
by: tthunder | last post by:
Hi @all, My small example does not compile... I know, that this (as always) has reasons, but I want to know WHY? BTW: I only get errors with g++ (4.x), BCB (6.0),... VS C++ (2005) works...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.