473,320 Members | 1,876 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,320 software developers and data experts.

Integer.Parse

hi All,

I have a question regarding Integer.Parse. I'm doing the following:

Dim H_cid1, H_cid2 as String
Dim I_cid1, I_cid2 as Integer

H_cid1 = "BFEBFBFF"
H_cid2 = "00000F29"

I_cid1 = Integer.Parse(H_cid1, Globalization.NumberStyles.HexNumber)
I_cid2 = Integer.Parse(H_cid2, Globalization.NumberStyles.HexNumber)

I_cid1 should be 3219913727. However, what I'm getting is -1075053569 (which
is FFFFFFFFBFEBFBFF in Hex).

I_cid2 should be 3881 and I_cid2 does equal 3881.

Anyone know why I_cid1 is not being converted correctly (or if it's being
converted correctly, why I'm not getting the value I expect!)

TIA,
Lee
Nov 21 '05 #1
3 5385
> Anyone know why I_cid1 is not being converted correctly (or if it's being
converted correctly, why I'm not getting the value I expect!)


A variable of integer type holds values from -2^31 to 2^31-1. A decimal
variable would be large enough - see the help for its range.

Andrew
Nov 21 '05 #2
Your answer comes down to 2's compliment arithematic. Yes, I'm old
enough that they still taught us about that in college.

Any value greater than 7FFFFFFF is treated as a negative number with
signed integers. The leftmost bit is the sign.

The value is calculated by subtracting your hex string from FFFFFFFF
and adding 1 for the negative value

FFFFFFFF
-FFFFFFFF
---------
00000000 + 1 = -1

FFFFFFFF
-BFEBFBFF
--------
40140400 + 1 = -1075053569

The decimal call by Andrew is one correct answer, because the decimal
uses 16 bytes instead of only four bytes, so the negative bit is not an
issue. A long may work as well; I believe that a long is 8 bytes. An
unsigned integer may still work for your needs, but I don't remember if
the CLR supports it.

Shane

Nov 21 '05 #3
Thanks guys!

I thought I had verified I was within the integer range, but apparently not.
I switched to long and all is well. Shane, thanks for the 2's compliment
math lesson! Though switching to long fixed the problem, it's nice to know
where the negative number was coming from!

Thanks Again!
Lee

"Shane" <sh***********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Your answer comes down to 2's compliment arithematic. Yes, I'm old
enough that they still taught us about that in college.

Any value greater than 7FFFFFFF is treated as a negative number with
signed integers. The leftmost bit is the sign.

The value is calculated by subtracting your hex string from FFFFFFFF
and adding 1 for the negative value

FFFFFFFF
-FFFFFFFF
---------
00000000 + 1 = -1

FFFFFFFF
-BFEBFBFF
--------
40140400 + 1 = -1075053569

The decimal call by Andrew is one correct answer, because the decimal
uses 16 bytes instead of only four bytes, so the negative bit is not an
issue. A long may work as well; I believe that a long is 8 bytes. An
unsigned integer may still work for your needs, but I don't remember if
the CLR supports it.

Shane

Nov 21 '05 #4

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

Similar topics

7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
4
by: Alpha | last post by:
Hi, I have a window based applicaton. I want to make sure that the users enter integer numbers before I save it to the database. I know to code this in the textbox_validating event but I don't...
1
by: js | last post by:
I need to test if an integer variable is null or not. I tried to do int myVairable; myVariable = Int32.Parse(txtControl1.Text.Trim()); if (myVariable != null) {// do something} C# does not...
38
by: Keith | last post by:
I've been reading and looking on the internet - but I just can't fin this. Perhaps I'm approaching this the wrong way - code-wise I'm attempting to learn how "things" work in VBNET by creating...
15
by: Brian Henry | last post by:
Which one is better to use? CInt(string) or Integer.Parse(string)? thanks!
15
by: Teresa | last post by:
1) Should I use Integer.Parse to convert a string into an integer in .NET now? CType(sUserID, Integer) OR Integer.Parse(sUserID) 2) And is it better to use the string class to trim, get length,...
5
by: Joergen Bech | last post by:
Basically, I want to convert hex values in the range "00000000" to "FFFFFFFF" to a signed, 32-bit Integer value. In VB6, I could just write lngValue = Val(hexstring$). In VB.Net, I seem to be...
7
by: Joe | last post by:
Hello All: Does anyone know the difference between CType(x,Integer) vs. Integer.Parse(x)? Is there a performance advantage to one or the other? TIA, -- Joe
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.