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

8- or 10-character hex string to signed 32-bit Integer value - any better ways?


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 forced to do something like

---snip---
Private Function HexToInteger(ByVal hexValue As String) As Integer
If hexValue.ToLower.StartsWith("&h") Then
hexValue = hexValue.Substring(2)
End If
Dim value As Long = Long.Parse(hexValue,
Globalization.NumberStyles.HexNumber)
If value > Integer.MaxValue Then
value = value - 4294967296&
End If
Return CType(value, Integer)
End Function
---snip---

Have I overlooked a function in the framework?

Surely there is a faster/shorter/better way?

TIA,

Joergen Bech

Dec 21 '05 #1
5 8013

Joergen Bech wrote:
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 forced to do something like

---snip---
Private Function HexToInteger(ByVal hexValue As String) As Integer
If hexValue.ToLower.StartsWith("&h") Then
hexValue = hexValue.Substring(2)
End If
Dim value As Long = Long.Parse(hexValue,
Globalization.NumberStyles.HexNumber)
If value > Integer.MaxValue Then
value = value - 4294967296&
End If
Return CType(value, Integer)
End Function
---snip---

Have I overlooked a function in the framework?

Surely there is a faster/shorter/better way?


Well, you found Long.Parse but you missed Integer.Parse :(

Dim s1 As String = "1234"
Dim s2 As String = "FEDCBA98"

Dim i1 As Integer = Integer.Parse(s1, _
Globalization.NumberStyles.HexNumber)
Dim i2 As Integer = Integer.Parse(s2, _
Globalization.NumberStyles.HexNumber)

--
Larry Lard
Replies to group please

Dec 21 '05 #2
Joergen,

See this complete message from Herfried,

http://groups.google.com/group/micro...225b927c8e1634

I hope this helps,

Cor
Dec 21 '05 #3
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> schrieb

Basically, I want to convert hex values in the range
"00000000" to "FFFFFFFF" to a signed, 32-bit Integer
value.


return System.Convert.ToInt32(hexvalue, 16)
Armin
Dec 21 '05 #4
>
Well, you found Long.Parse but you missed Integer.Parse :(

Dim s1 As String = "1234"
Dim s2 As String = "FEDCBA98"

Dim i1 As Integer = Integer.Parse(s1, _
Globalization.NumberStyles.HexNumber)
Dim i2 As Integer = Integer.Parse(s2, _
Globalization.NumberStyles.HexNumber)


Thanks. Knew I had overlooked something. Started off with
CInt, which does not handle 80000000-FFFFFFFF very well.

Because of that observation, along with this thread
http://www.dotnet247.com/247referenc...58/293488.aspx
I was thrown off course and thought I had to make use of
the Long (64-bit) data type in my solution - never checking if
Integer.Parse(...HexNumber) worked correctly. Which it does.

Regards,

Joergen Bech

Dec 21 '05 #5
On Wed, 21 Dec 2005 12:58:42 +0100, "Armin Zingler"
<az*******@freenet.de> wrote:
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> schrieb

Basically, I want to convert hex values in the range
"00000000" to "FFFFFFFF" to a signed, 32-bit Integer
value.


return System.Convert.ToInt32(hexvalue, 16)
Armin


Excellent. Even better than Integer.Parse(...HexNumber).

Now, if only System.ParseNumbers was exposed so I could
skip the base check ...

But this is good enough for me.

Regards,

Joergen Bech

Dec 21 '05 #6

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

Similar topics

12
by: Terry Richards | last post by:
i have macosx 10.2.8. i am trying to install php-4.3.10. i am getting: make gcc -Iext/standard/ -I/Users/terryr/tmp/php-4.3.10/ext/standard/ -DPHP_ATOM_INC...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
4
by: Bill | last post by:
Hi, I would be grateful if someone could clarify my rather confused ideas of the 10 connection limit on XP/2000 when its being used as a server. (I realise that XP is really a client op sys with...
6
by: Christo | last post by:
I have this script for showing news on a page, but i want it to only show the last 10 records, as in the 10 records that were added to the database last. the script shows the entries in descending...
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
2
by: benj | last post by:
Anybody has a good explaination to following question? what is the difference between: int *a and int (*a)
4
by: manjina3 | last post by:
Hi guys! How do i display 10 numbers per line when i run this code? #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { long NumberOfLines = 10; long count = 10; ...
2
by: iritchie | last post by:
*(Apologies, I posted this in the SQL Server forum first) Hello all, I am trying to write a query which breaks down a single address field into individual fields, with char(10) or a...
8
by: mauro | last post by:
Hi all, I found this line in a C file, but I cannot understand it! what's the meaning of it? cheers, Mauro
0
by: mg | last post by:
When make gets to the _ctypes section, I am getting the following in my output: building '_ctypes' extension creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.