473,508 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing non-decimal strings as numbers

Hi all;

I'm really struggling to get C# to do the right thing when parsing
non-decimal strings as numbers. What I really want to be able to do is
take a string containing an integer in any valid format (decimal, octal
with leading zero, hex with leading "0x") and have C# get the conversion
right.

I hope I'm missing something, but it seems like you have to know in
advance what format a string is in for it to be parsed correctly. None
of the valid NumberStyles flags seem to say "ok, look at the prefix to
work out what base it's in, then just do the damn conversion for me".

One of the NumberStyles enumeration flags is "AllowHexSpecifier". Great,
I thought -- this says that it will allow a leading "0x" and it will
recognise that it's a hex string. Nope. The docs go on to say, and I
quote MSDN, "Strings parsed using this style are not permitted to be
prefixed with "0X"". Yeah, that's real helpful. The AllowHexSpecifier
flag specifically disallows a hex specifier. Neato.

Can anyone point me in the right direction here? I just want a simple
strtol() equivalent that works.

Thanks,
Andrew.

Nov 15 '05 #1
5 8952
SK
Try the following functions:

Convert.ToInt32(str);
System.Int32.Parse(str);

-----Original Message-----
Hi all;

I'm really struggling to get C# to do the right thing when parsingnon-decimal strings as numbers. What I really want to be able to do istake a string containing an integer in any valid format (decimal, octalwith leading zero, hex with leading "0x") and have C# get the conversionright.

I hope I'm missing something, but it seems like you have to know inadvance what format a string is in for it to be parsed correctly. Noneof the valid NumberStyles flags seem to say "ok, look at the prefix towork out what base it's in, then just do the damn conversion for me".
One of the NumberStyles enumeration flags is "AllowHexSpecifier". Great,I thought -- this says that it will allow a leading "0x" and it willrecognise that it's a hex string. Nope. The docs go on to say, and Iquote MSDN, "Strings parsed using this style are not permitted to beprefixed with "0X"". Yeah, that's real helpful. The AllowHexSpecifierflag specifically disallows a hex specifier. Neato.

Can anyone point me in the right direction here? I just want a simplestrtol() equivalent that works.

Thanks,
Andrew.

.

Nov 15 '05 #2
SK wrote:
Try the following functions:

Convert.ToInt32(str);
System.Int32.Parse(str);


They're of no help at all, because they bork when presented with a hex
string.

Nov 15 '05 #3
On Sun, 14 Dec 2003 08:39:56 +1100, Andrew Lighten
<re***@via.newsgroup> wrote:
SK wrote:
Try the following functions:

Convert.ToInt32(str);
System.Int32.Parse(str);


They're of no help at all, because they bork when presented with a hex
string.


Convert.ToInt32(str, 16) *will* work with the leading 0x.

In general, you can't convert without knowing something beforehand.
Take the string "123". What is it's numerical value?
In Base 10
Base 8 83
Base 10 123
Base 16 291

If you are sure that your string, if hex, comes with a leading "0x",
try:
if (str[1]=='x')
return Convert.ToInt32(str,16);
else return Convert.ToInt32(str,10);

Nov 15 '05 #4
Austin Ehlers wrote:
Convert.ToInt32(str, 16) *will* work with the leading 0x.

In general, you can't convert without knowing something beforehand.
Take the string "123". What is it's numerical value?
In Base 10
Base 8 83
Base 10 123
Base 16 291

If you are sure that your string, if hex, comes with a leading "0x",
try:
if (str[1]=='x')
return Convert.ToInt32(str,16);
else return Convert.ToInt32(str,10);


"123" has a numeric value of 123 decimal. It's a base 10 number.

If it was base 8 (octal) it would be "0123". If it was base 16 (hex) it
would be "0x123".

The whole point is that I'm not sure whether my string is a hex string
with a leading "0x", an octal string with a leading "0" or a decimal string.

What I want is the functionality that strtol() gives when you pass a
base of zero: it looks at the prefix to work it out for itself.

Nov 15 '05 #5
<sniP
What I want is the functionality that strtol() gives when you pass a
base of zero: it looks at the prefix to work it out for itself.


You should have said that sooner. ;)

[DllImport("msvcrt.dll")] //MS Visual C Runtime
private static extern int strtol(string str,out string error,int
Base); //return value is int because C# int is the same as a C long

'Lib

Nov 15 '05 #6

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

Similar topics

14
2617
by: Viktor Rosenfeld | last post by:
Hi, I need to create a parser for a Python project, and I'd like to use process kinda like lex/yacc. I've looked at various parsing packages online, but didn't find anything useful for me: -...
15
3598
by: Freddie | last post by:
Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: > moo cow "farmer john"...
3
3485
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
8
2686
by: pradeepsarathy | last post by:
Hi all, Does the SAX parser has eventhandlers for parsing xml schema. Can we parse the xml schema the same way as we parse the xml document using SAX Parser. Thanks in advance. -pradeep
4
4846
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr>...
0
1409
by: Li-fan Chen | last post by:
Hi, We work with email in a large CRM solution and one of the email-related tasks that has plagued us is our decision to make use of a 3rd-party local-sourcer to work on the parsing of inbound...
3
2691
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
13
4474
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
8
1527
by: Tarique | last post by:
Hello. #include<stdio.h> int main(void) { int i=- -2; printf("%d",i); return 0; }
1
2954
by: hd95 | last post by:
In a perfect world my xml feed source would produce perfect xml ..that is not the case I am parsing an XML feed that sometimes has ampersands and dashes in the content that messes up my parsing. ...
0
7225
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
7123
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
7326
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
7383
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...
1
7046
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
5627
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,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.