Parsing string for hex values ...

C

Computer_Czar

I'm trying to figure out the best way to parse an input string from a file
for hex values. The string is actually Motorola S code produced by an
embedded assembler. For example lines contain S1142CD0XXYYZZ... I've
written similar programs in C/C++ where I save the string and use a pointer
to index along the string.

Well I've heard pointers are "evil" within C# but possible. Given that what
would be the desired method? Some of the values are 16 bit and some are 8
bit so I need to be flexible. Plus I'd also like to convert the hex values
to decimal for some math.

Can someone give me a recommendation or a "pointer" to how to approach it?
 
R

Robert Jeppesen

Hi,

You can use pointers if you need performance. This requires that your app runs in FullTrust though.
You could also just keep your own "pointer" in an int:

string s = "abcdef";
int i = 0;
char c = s;

I would only resort to using pointers if you're doing *lots* of parsing.
 
G

Greg Ewing [MVP]

Computer_Czar, here are a couple of suggestions which might help you out.

1. Could you just use Substring to extract the strings and then Parse() to
parse the strings as hex values?
2. Could you use string.ToCharArray() to get a char array and then walk
through that array? That's probably going to yield code the most similar to
your C++ pointer code.
3. If you really have to you can use pointers in an unsafe block. I
wouldn't recommend it for this though since it seems there are some good
managed options.
 
J

Jon Skeet [C# MVP]

Computer_Czar said:
I'm trying to figure out the best way to parse an input string from a file
for hex values. The string is actually Motorola S code produced by an
embedded assembler. For example lines contain S1142CD0XXYYZZ... I've
written similar programs in C/C++ where I save the string and use a pointer
to index along the string.

Well I've heard pointers are "evil" within C# but possible. Given that what
would be the desired method? Some of the values are 16 bit and some are 8
bit so I need to be flexible. Plus I'd also like to convert the hex values
to decimal for some math.

Can someone give me a recommendation or a "pointer" to how to approach it?

I can't see why you'd really need pointers here - just store how far
along the string you've gone with an integer.
 
C

Computer_Czar

Thanks for the reply ... you and everyone gave great suggestions. I'll take
a cut at it. I guess my main difficulty is or was understanding what
facilities existed within C# to perform the various tasks.


Robert Jeppesen said:
Hi,

You can use pointers if you need performance. This requires that your app runs in FullTrust though.
You could also just keep your own "pointer" in an int:

string s = "abcdef";
int i = 0;
char c = s;

I would only resort to using pointers if you're doing *lots* of parsing.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


I'm trying to figure out the best way to parse an input string from a file
for hex values. The string is actually Motorola S code produced by an
embedded assembler. For example lines contain S1142CD0XXYYZZ... I've
written similar programs in C/C++ where I save the string and use a pointer
to index along the string.

Well I've heard pointers are "evil" within C# but possible. Given that what
would be the desired method? Some of the values are 16 bit and some are 8
bit so I need to be flexible. Plus I'd also like to convert the hex values
to decimal for some math.

Can someone give me a recommendation or a "pointer" to how to approach it?
 
M

Michael Bray

I'm trying to figure out the best way to parse an input string from a
file for hex values. The string is actually Motorola S code produced
by an embedded assembler. For example lines contain S1142CD0XXYYZZ...
I've written similar programs in C/C++ where I save the string and
use a pointer to index along the string.

A good way might be to use the Regex class with a search pattern of

[0-9A-Fa-f]+

and then process the results. This will pick out strings of characters
which represent valid hex numbers. If you only want a certain number of
characters (to guarantee that you can int.Parse or long.Parse) then use
quatifiers - for example:

[0-9A-Fa-f]{1,8} -- matches 1..8 hex characters


-mbray
 
J

Jason Smith

Pointers are for something else. They really don't apply here unless you
are a masochist to yourself and a sadist to those who will maintain your
code in the future. :)

If I guess your intent correctly, you would want to use SubStr to pull out a
hex value in a known position and use Convert.ToInt32 to turn it into a
number.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top