character set encoding: how to generate errors

O

Oliver

Hi -

I am using the Encoding class to encode unicode strings.

I am encoding into a variety of target encodings eg utf-8,
'iso-8859-1' or 'iso-8859-5' etc

like this:
encodedBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(TextBox1.Text);

anyone know a technique to cause exceptions to be thrown if there is
no corresponding mapping possible for any of the characters?

what is happening in practice is that they get converted into a '?'
symbol - but I'd rather an exception were thrown....

ta,
Oliver.
 
J

Jon Skeet [C# MVP]

Oliver said:
I am using the Encoding class to encode unicode strings.

I am encoding into a variety of target encodings eg utf-8,
'iso-8859-1' or 'iso-8859-5' etc

like this:
encodedBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(TextBox1.Text);

anyone know a technique to cause exceptions to be thrown if there is
no corresponding mapping possible for any of the characters?

what is happening in practice is that they get converted into a '?'
symbol - but I'd rather an exception were thrown....

The usual way of doing that is by reversing the process, and seeing
whether or not you end up with the same string:

byte[] EncodeTextOrThrow (Encoding encoding, string text)
{
byte[] ret = encoding.GetBytes(text);
string cycled = encoding.GetString(ret);
if (cycled != text)
{
throw new ApplicationException ("Could not encode text");
}
return ret;
}
 

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