473,763 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetEnc oding("iso-8859-1").GetBytes(Te xtBox1.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 1284
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.GetEnc oding("iso-8859-1").GetBytes(Te xtBox1.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[] EncodeTextOrThr ow (Encoding encoding, string text)
{
byte[] ret = encoding.GetByt es(text);
string cycled = encoding.GetStr ing(ret);
if (cycled != text)
{
throw new ApplicationExce ption ("Could not encode text");
}
return ret;
}

--
Jon Skeet - <sk***@pobox.co m>
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
1396
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 the content of it, it works well (I set the page charset to utf-8). Now, I'd like to get each character code. For that, I use Encoding.GetBytes function, as following:
7
4973
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 windows-1252 and that it is not equivalent to iso-8859-1. Looking in the registry under HKEY_CLASSES_ROOT\MIME\Database\Charset and \Codepage, it seems that all variations on iso-8859-1 (latin1, etc) are mapped to code page 1252, which I'm...
4
3029
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"> This used to work just fine, but recently I migrated all of my web pages to a new server. Now I find that when I view the web pages they
8
3486
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 solve it ?? Private Sub Init() With AMSXML ..Formatting = Formatting.Indented ..Indentation = 4 ..IndentChar = " " ..WriteStartDocument()
3
5871
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 user-typed HTML, assigns it as an XML node's InnerText property (using C#: System.Xml.XmlDocument obj; obj.InnerText = myHTML). Then I later get the InnerText as a string and write to disk. When this text is displayed in a web browser, special...
1
885
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 = Encoding.GetEncoding("iso-8859-1").GetBytes(TextBox1.Text);
5
1971
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 the faultstring element has the Icelandic characters encoded like "réttindi", where "é" is an Icelandic accented character. Does anyone know why? If so, how do I deal with this?
2
2484
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 the target encoding (as described in PEP 293). The thing to note here is that the error handler itself is required to return the substitutions as Unicode strings - not as the target encoding bytestrings. Some lower-level gadgetry will silently...
2
2465
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 to their HTML numbers that < 32 or 126 decimal. Is this correct? What would you advise? Thanking you in anticipation.
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.