convert Int32 Unicode character code to its value

J

John

I have a simple question which, surprisingly a google search nor MSDN
can help me with. What is the best stream to use to parse a string,
character by character. All the streams return Int32, but I cannot find
a way to convert the Int32 to a character value. For instance,
StringReader.Read("1") returns 49, the Unicode character code. How do I
convert this unicode code to its value '1'?

Also, off the main question, but here are a couple more:

1) Why doesn't StringReader take an Encoding?
2) If StringReader does not take an Encoding, how does it actually work?
3) If String has a Trim function, why isn't there an
Encoding.xxx.IsSpace(Int32 ch)?

thanks very much in advance
 
L

Luc E. Mistiaen

you can try with a StreamReader and an Encoding of 'utf-32' (code page
12000).


/LM
 
J

John

Peter said:
Why not just enumerate all the characters in the string? For example:

string strInput = "my string";

foreach (char chInput in strInput)
{
// do something with each character
}

Is there some specific reason you want to use StringReader?

Well, the code uses a system of building token types and objects out of
the string. I am porting some C++ code that uses putback on the stream
during the processing which can be converted to Peek(). Your suggestion
is valid but I'm not sure it's going to work.
Second, the Encoding class has _no_ methods of that nature. Why would
you expect to find an IsSpace() method in the Encoding class, when there
aren't any methods to classify characters?

You can, of course, call Char.IsWhiteSpace() on characters once you've
got a valid UTF-16 character to look at.

Of course, the Char class, how did I miss that, thanks very much.

Sound advice as usual Peter.
 

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

Top