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

encoding question

Hello,

tried this in framework.aspnet without any luck so far, maybe someone here has
a comment ...

TIA, Jim

..net c# httphandler straight html form at browser.

GBP pound sign problem (I know I know - I *can* decode it, but I've got to
understand what and why I should be doing stuff)

I am uploading text data from a form. This data is either directly input into a
textarea, or is a file stream originating from a .txt file, (or other basic text
file (like off Mac or Unix - of course I don't necessarily know at present it's
only .txt)

The page encoding is :-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

On arrival at the server the content encoding is, sure enough UTF8.

Data input via the textarea and input to a string is displayed in the debugger
as pounds (£)

Data input as a filestream has in the stream single bytes containing 0xA3 for
the GBP pound sign.

I process the input stream like this :-

public static string StreamToString(Stream aStream)
{ {
aStream.Position = 0;
long i = aStream.Length;
byte[] buffer = new byte[i];

aStream.Read(buffer,0,(int)aStream.Length);
return BytesToUTF8String(buffer);
}

public static string BytesToUTF8String(byte[] Array)
{
Encoding utf8 = Encoding.UTF8;
char[] utf8Chars = new char[utf8.GetCharCount(Array, 0,Array.Length)];
utf8.GetChars(Array, 0, Array.Length, utf8Chars, 0);

return new string(utf8Chars);
}

The resulting string contains nothing ...

If I use ASCII instead of UTF8, I get sense except my GBP signs are query ?
marks.

If I use UTF7 I get an apparently OK decoding.

I am dubious about using UTF7 for no better reason than that it works. Is there
logic here? What should I be doing?

Thanks,
Jim
Nov 16 '05 #1
7 2505
Jim Lawton <uc**@use.your.initiative> wrote:

<snip>
I am dubious about using UTF7 for no better reason than that it works. Is there
logic here? What should I be doing?


You should probably be using Encoding.Default, or Encoding.GetEncoding
(28591) (i.e. ISO-8859-1).

You almost certainly *don't* want UTF-7 really.

--
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
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
I am dubious about using UTF7 for no better reason than that it works. Is there
logic here? What should I be doing?


You should probably be using Encoding.Default, or Encoding.GetEncoding
(28591) (i.e. ISO-8859-1).

You almost certainly *don't* want UTF-7 really.


Thinking about it further, ISO-8859-1 won't work either - basically you
need to know the original encoding of the file. It may well be Windows
CP-1252, which will be what Encoding.Default will probably return if
you're in Western Europe or the US, unless you've changed the defaults,
but really you're still going to be at the whim of files which *aren't*
written with that encoding :(

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

In addition of Jon, the A3 is the pound in the 1252
http://www.microsoft.com/globaldev/r.../sbcs/1252.htm

To use it forced you can maybe try
mystring = System.Text.Encoding.GetEncoding(1252).GetBytes(St r.ReadToEnd);

I hope this helps?

Cor
Nov 16 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
I am dubious about using UTF7 for no better reason than that it works. Is there
logic here? What should I be doing?


You should probably be using Encoding.Default, or Encoding.GetEncoding
(28591) (i.e. ISO-8859-1).

You almost certainly *don't* want UTF-7 really.


Thinking about it further, ISO-8859-1 won't work either


Apparently I didn't think about it enough. Ignore me - ISO-8859-1
*will* work for converting byte A3 into a pound sign. Whether it'll
work for the rest of the file depends on the contents of the file.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
On Wed, 12 Jan 2005 07:07:07 -0000, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> > I am dubious about using UTF7 for no better reason than that it works. Is there
> > logic here? What should I be doing?
>
> You should probably be using Encoding.Default, or Encoding.GetEncoding
> (28591) (i.e. ISO-8859-1).
>
> You almost certainly *don't* want UTF-7 really.


Thinking about it further, ISO-8859-1 won't work either


Apparently I didn't think about it enough. Ignore me - ISO-8859-1
*will* work for converting byte A3 into a pound sign. Whether it'll
work for the rest of the file depends on the contents of the file.

:-) ... thanks for all your thoughts Jon - if nothing else it gives me some
confidence that the whole encoding issue is a can of worms! It smells a bit of
the old "DLL Hell" to me - all we need is a few bytes on the front of any file
to say what encoding it is, but we'll never get it!

Jim
Nov 16 '05 #6
On Tue, 11 Jan 2005 22:22:56 +0100, "Cor Ligthert" <no************@planet.nl>
wrote:
Jim,

In addition of Jon, the A3 is the pound in the 1252
http://www.microsoft.com/globaldev/r.../sbcs/1252.htm

To use it forced you can maybe try
mystring = System.Text.Encoding.GetEncoding(1252).GetBytes(St r.ReadToEnd);

I hope this helps?

Cor


I'll give it a whirl - thanks ...
J

Nov 16 '05 #7
On Wed, 12 Jan 2005 07:07:07 -0000, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:


Apparently I didn't think about it enough. Ignore me - ISO-8859-1
*will* work for converting byte A3 into a pound sign. Whether it'll
work for the rest of the file depends on the contents of the file.

I think that's right - works well enough ... I've inspected ("watched") the
contents of the request, and I can't see anything which relates to the encoding
of the bytestream - just text/plain so I'm down to guessing. Input will always
be from the UK ...

Cheers Jim
Nov 16 '05 #8

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

Similar topics

2
by: Mark | last post by:
Sorry about the last... Anyway, here's the question: I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in...
7
by: polarz | last post by:
I wrote a front end to a command line mp4 music tagger that reads my playlist files and gets info such as artist, title, album, etc, etc. I use the info to catalog, tag, sort, etc, my files. I've...
5
by: Waldy | last post by:
Hi there, how do you set the encoding format of an XML string? When I was outputting the XML to a file you can specify the encoding format like so: XmlTextWriter myWriter; myWriter = new...
4
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am...
4
by: George | last post by:
Hi, I am puzzled by the following and seeking some assistance to help me understand what happened. I have very limited encoding knowledge. Our SAP system writes out a text file which includes...
4
by: Provost Zakharov | last post by:
Hello, I just needed some help on how the DOM is encoded by the IE parser. As per the MSDN page, http://msdn.microsoft.com/workshop/author/dhtml/reference/charsets/charset4.asp ,server encodings...
3
by: mortb | last post by:
1. How do I determine which encoding a xmldocument or xmlreader uses when opening a document? I'm not just talking about the <?xml encoding="utf-8"?attribute, but the actual encoding of the...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
0
by: deloford | last post by:
Hi This is going to be a question for anyone who is an expert in C# Text Encoding. My situation is this: I have a Sybase database which is firing back ISO-8559 encoded strings. I am unable to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.