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

Easy Hex string "0x1234" to integer conversion?

Is there an easy conversion from a hex string, for example "0x1234" to an
int?

I have written my own many times in other languages, I was just wondering if
there might be something already available for doing this.

-Ed
Nov 15 '05 #1
4 17790
Off the top of my head, couldn't you do this:

int intMyVal = int.Parse(0x1234);

?

"Ed Sutton" <es*****@nomadics.com> wrote in message
news:u3**************@TK2MSFTNGP11.phx.gbl...
Is there an easy conversion from a hex string, for example "0x1234" to an
int?

I have written my own many times in other languages, I was just wondering if there might be something already available for doing this.

-Ed

Nov 15 '05 #2
Maybe something like this?

System.Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber)

Lars

"Ed Sutton" <es*****@nomadics.com> skrev i en meddelelse
news:u3**************@TK2MSFTNGP11.phx.gbl...
Is there an easy conversion from a hex string, for example "0x1234" to an
int?

I have written my own many times in other languages, I was just wondering if there might be something already available for doing this.

-Ed

Nov 15 '05 #3
Thanks Lars!
Maybe something like this?

System.Int32.Parse(hexString,
System.Globalization.NumberStyles.HexNumber)


This threw an exception until I read that it did not support the "0x"
prefix.
hexString="55AA00" works great!

Thanks again,

-Ed
Nov 15 '05 #4
Thank you Otis. Very good.
Here is the answer:
uint i = Convert.ToUInt32("0xabc", 16);


So now I have two methods to convert a hex string to an int:

1 - The Convert class supports an optional "0x" or "0X" prefix. For
example, it will convert "0x64", "0X64", and "64" to decimal value of 100.
It does not tolerate leading or trailing white-space.

2 - The System.Int32.Parse approach tolerates white-space, but does not
support the "0x" or "0X" prefix. This approach will convert "64", " 64",
and "64 " to decimal value of 100.

So with C#, I can use the Convert class in conjunction with a string Trim.
I no longer have any reason to write my own hexString-to-int functions.

uint i = Convert.ToUInt32(hexString.Trim(), 16);

Thanks again!

-Ed
Nov 15 '05 #5

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

Similar topics

2
by: Cosmin Prund | last post by:
This is supposed to be very easy, only I did not find this in MSDN so I'll ask here: How do I enter a char constant using the char's ASCII number? Specifically I want to be able to compare a...
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
1
by: CptDondo | last post by:
I am trying to pack a string. The data consists of an array of integers, something like this: e 0x1234 e 0x0001 e 0x0002 What I want in my resulting packed $binrow is 0x1234 0x01 0x02
1
by: benn686 | last post by:
Id like to group 16 booleans into a u16 such that I can either set all 16 variables at once with just a single u16 assigment, or I can individually change a bit by setting that boolean manually. ...
5
by: Filipe Teixeira | last post by:
Hi. I have to open a binary file from an old computer and recover the information stored (or at least try to). I use: f=open('file.bin','rb') a=f.read() f.close() a in now a string full...
4
by: mostafaAlderea | last post by:
the function converts the first parameter into a string containing the hexadecimal equivalent of the number
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?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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...

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.