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

character set encoding: how to generate errors

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.
Nov 16 '05 #1
1 1274
Oliver <sp*************@yahoo.com> wrote:
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;
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

2
by: seb | last post by:
Hi all, I work on a translation module and I need to encode different characters from different charset. I've got an Access database that countains the different translations. When I display...
7
by: Mark | last post by:
Hi... I've been doing a lot of work both creating and consuming web services, and I notice there seems to be a discontinuity between a number of the different cogs in the wheel centering around...
4
by: HeroOfSpielburg | last post by:
Hello, I am trying to using the Shift_JIS character set in my web pages, and have specified it as such in the <head>. <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> ...
8
by: Agnes | last post by:
In my .net ,i need to generate an xml file , however, user may input a chinese character, Then , the xml will got something unknow characters. the following is my code, Does anyone know how to...
3
by: Jon Davis | last post by:
I have a software application I've written called PowerBlog (PowerBlog.net) that takes the editing capability of the Internet Explorer WebBrowser control (essentially a DHTMLTextBox), extracts the...
1
by: Oliver | last post by:
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 =...
5
by: Dadi | last post by:
Hi, My native language is Icelandic and I´m making a web service that returns results that contain many Icelandic characters. This works fine, however, when I return a soap:Fault, the string in...
2
by: Jukka Aho | last post by:
When converting Unicode strings to legacy character encodings, it is possible to register a custom error handler that will catch and process all code points that do not have a direct equivalent in...
2
by: withers | last post by:
XML gives an error when I have a £ sign (GBP) - £ - in a string. I've fixed this by converting it to its HTML number. In case other characters may cause the same error, I'm converting characters...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.