473,413 Members | 2,058 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,413 software developers and data experts.

Unicode Encoding

Hello All:

I have a windows application that I need to encode a string using Unicode.
The example I have been given to use is a Web-Version. Below is the webcode.

Response.ContentEncoding=System.Text.Encoding.Unic ode;
Response.ContentType = "application/postscript";
Response.Buffer =true;
Response.AppendHeader("Content-Disposition","attachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Encoding.Unicode.GetBytes(sPSFile);
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length;i+=2)
{
by[0]=ba[i];
Response.BinaryWrite(by);
}
Response.End();

I am having a hard time converting this code into the windows equivalent so
that the files remain the same. Would anyone be able to help?

I know what I am trying is not working. I thought I had to start down this
path:
Byte[] ba = System.Text.Encoding.Unicode.GetBytes(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);

But I know the encoding.GetString() simply converts it back to a string.
How do I get the value postScript to be the encoded value?

Thanks
Andy


Oct 25 '06 #1
8 8461
Hi, Andy.

I assume "windows version" for you means a C# WinForms application
since you're posting on a C# board.

All .NET strings are Unicode encoded. If you're getting your data into
a string, you're getting it in Unicode.

It looks like you're trying to read a Postscript file out of a MIME
attachment. I don't have the rest of your "webcode," but chances are,
your MIME attachment is Base64 encoded. If you rip the Base64-encoded
content out of the BOF and EOF markers, you'll still need to decode it
into binary bytes (as opposed to Base64 characters) before you can do
anything useful with it.

Try System.Convert.FromBase64String() on your data. That'll give you
back a byte[], which you can then turn your UnicodeEncoding on.
HTH!

Stephan


Andy wrote:
Hello All:

I have a windows application that I need to encode a string using Unicode.
The example I have been given to use is a Web-Version. Below is the webcode.

Response.ContentEncoding=System.Text.Encoding.Unic ode;
Response.ContentType = "application/postscript";
Response.Buffer =true;
Response.AppendHeader("Content-Disposition","attachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Encoding.Unicode.GetBytes(sPSFile);
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length;i+=2)
{
by[0]=ba[i];
Response.BinaryWrite(by);
}
Response.End();

I am having a hard time converting this code into the windows equivalent so
that the files remain the same. Would anyone be able to help?

I know what I am trying is not working. I thought I had to start down this
path:
Byte[] ba = System.Text.Encoding.Unicode.GetBytes(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);

But I know the encoding.GetString() simply converts it back to a string.
How do I get the value postScript to be the encoded value?

Thanks
Andy
Oct 25 '06 #2
Stephan:

Thank you very much for your help. I appreciate it immensly. You are
correct in your assumption about WinForms and C#.

Please forgive me, as this is my first forray into any type of encoding. My
google searches confused me even more.

Unfortunately: I am unsure exactly how the MIME attachment is encoded, as
this comes from a component, of which I do not have access to the code.

However: I took your suggestion and used this code:
Byte[] ba = System.Convert.FromBase64String(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);

But now I am getting an error stating: "System.FormatException: Invalid
character in a Base-64 string. at System.Convert.FromBase64String(String s)"
Any help would be greatly appreciated.

"ssamuel" wrote:
Hi, Andy.

I assume "windows version" for you means a C# WinForms application
since you're posting on a C# board.

All .NET strings are Unicode encoded. If you're getting your data into
a string, you're getting it in Unicode.

It looks like you're trying to read a Postscript file out of a MIME
attachment. I don't have the rest of your "webcode," but chances are,
your MIME attachment is Base64 encoded. If you rip the Base64-encoded
content out of the BOF and EOF markers, you'll still need to decode it
into binary bytes (as opposed to Base64 characters) before you can do
anything useful with it.

Try System.Convert.FromBase64String() on your data. That'll give you
back a byte[], which you can then turn your UnicodeEncoding on.
HTH!

Stephan


Andy wrote:
Hello All:

I have a windows application that I need to encode a string using Unicode.
The example I have been given to use is a Web-Version. Below is the webcode.

Response.ContentEncoding=System.Text.Encoding.Unic ode;
Response.ContentType = "application/postscript";
Response.Buffer =true;
Response.AppendHeader("Content-Disposition","attachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Encoding.Unicode.GetBytes(sPSFile);
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length;i+=2)
{
by[0]=ba[i];
Response.BinaryWrite(by);
}
Response.End();

I am having a hard time converting this code into the windows equivalent so
that the files remain the same. Would anyone be able to help?

I know what I am trying is not working. I thought I had to start down this
path:
Byte[] ba = System.Text.Encoding.Unicode.GetBytes(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);

But I know the encoding.GetString() simply converts it back to a string.
How do I get the value postScript to be the encoded value?

Thanks
Andy

Oct 25 '06 #3
Andy,

What's the string (postScript) look like? Where are you getting it
from?

I'm still assuming your source component is emitting a MIME-encoded
message and that's what the variable postScript contains. Is this
correct? Do you know anything about what the source is *supposed* to be
sending? If it is a MIME message, there are some headers that you have
to strip. Have you done this?

It looks like you're trying to feed more than just some Base64-encoded
text into the decoder. A (likely) reason that this could happen is if
you're not stripping the headers.

It would be helpful if you could post the fist couple hundred
characters of the string you're trying to decode (postScript).
Stephan


Andy wrote:
Stephan:

Thank you very much for your help. I appreciate it immensly. You are
correct in your assumption about WinForms and C#.

Please forgive me, as this is my first forray into any type of encoding. My
google searches confused me even more.

Unfortunately: I am unsure exactly how the MIME attachment is encoded, as
this comes from a component, of which I do not have access to the code.

However: I took your suggestion and used this code:
Byte[] ba = System.Convert.FromBase64String(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);

But now I am getting an error stating: "System.FormatException: Invalid
character in a Base-64 string. at System.Convert.FromBase64String(String s)"
Any help would be greatly appreciated.

"ssamuel" wrote:
Hi, Andy.

I assume "windows version" for you means a C# WinForms application
since you're posting on a C# board.

All .NET strings are Unicode encoded. If you're getting your data into
a string, you're getting it in Unicode.

It looks like you're trying to read a Postscript file out of a MIME
attachment. I don't have the rest of your "webcode," but chances are,
your MIME attachment is Base64 encoded. If you rip the Base64-encoded
content out of the BOF and EOF markers, you'll still need to decode it
into binary bytes (as opposed to Base64 characters) before you can do
anything useful with it.

Try System.Convert.FromBase64String() on your data. That'll give you
back a byte[], which you can then turn your UnicodeEncoding on.
HTH!

Stephan


Andy wrote:
Hello All:
>
I have a windows application that I need to encode a string using Unicode.
The example I have been given to use is a Web-Version. Below is the webcode.
>
Response.ContentEncoding=System.Text.Encoding.Unic ode;
Response.ContentType = "application/postscript";
Response.Buffer =true;
Response.AppendHeader("Content-Disposition","attachment; filename=\"" +
sFilename + "\"");
>
Byte[] ba = System.Text.Encoding.Unicode.GetBytes(sPSFile);
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length;i+=2)
{
by[0]=ba[i];
Response.BinaryWrite(by);
}
Response.End();
>
I am having a hard time converting this code into the windows equivalent so
that the files remain the same. Would anyone be able to help?
>
I know what I am trying is not working. I thought I had to start down this
path:
Byte[] ba = System.Text.Encoding.Unicode.GetBytes(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);
>
But I know the encoding.GetString() simply converts it back to a string.
How do I get the value postScript to be the encoded value?
>
Thanks
Andy
Oct 25 '06 #4
Hi Andy,

why do you increment i by 2. Even though Unicode (or UTF-16 to be precise),
is a 2-Byte code, 1 Byte is still 1 Byte. You increnet by to, but copy only
1 Byte per iteration thereby loosing half of each character.

Also it may be more performant copying greater chunks.

"Andy" <An**@discussions.microsoft.comschrieb im Newsbeitrag
news:1A**********************************@microsof t.com...
Hello All:

I have a windows application that I need to encode a string using Unicode.
The example I have been given to use is a Web-Version. Below is the
webcode.

Response.ContentEncoding=System.Text.Encoding.Unic ode;
Response.ContentType = "application/postscript";
Response.Buffer =true;
Response.AppendHeader("Content-Disposition","attachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Encoding.Unicode.GetBytes(sPSFile);
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length;i+=2)
{
by[0]=ba[i];
Response.BinaryWrite(by);
}
Response.End();

I am having a hard time converting this code into the windows equivalent
so
that the files remain the same. Would anyone be able to help?

I know what I am trying is not working. I thought I had to start down
this
path:
Byte[] ba = System.Text.Encoding.Unicode.GetBytes(postScript);
UnicodeEncoding encoding = new UnicodeEncoding();
postScript = encoding.GetString(ba);

But I know the encoding.GetString() simply converts it back to a string.
How do I get the value postScript to be the encoded value?

Thanks
Andy


Oct 25 '06 #5
Stephan:

Unfortunately: I am not familiar with what is supposed to be sending as
this component was written by someone long before I was at this company, so I
don't have much knowledge of it.

What is happening is: we have item layouts that are XML files. This XML is
being passed into the component, which returns a postScript file (or
postScript of some sort). Once we get this postScript, it is then converted
to a .pdf file. I was to write a windows automated version that followed the
same logic as our web-version (which was manual and time consuming).
Everything seemed to be working wiht my version until we discovered a pdf
that didn't display correctly and it seemed to be an encoding issue.

Here are the first 200 characters of the "postScript" that is returned.

%!PS-AdobeFont-1.0: TimesNewRomanPS-Italic 001.000
%%CreationDate: 2/10/00 at 7:13 PM
%%VMusage: 1024 28747
% Generated by Fontographer 4.1
% Copyright \(c\) 1988, 1990 Adobe Systems Incorporate

I have not stripped out any messages, as it doesn't appear the web-version
was doing this. Perhaps I need to do this?

"ssamuel" wrote:
Andy,

What's the string (postScript) look like? Where are you getting it
from?

I'm still assuming your source component is emitting a MIME-encoded
message and that's what the variable postScript contains. Is this
correct? Do you know anything about what the source is *supposed* to be
sending? If it is a MIME message, there are some headers that you have
to strip. Have you done this?

It looks like you're trying to feed more than just some Base64-encoded
text into the decoder. A (likely) reason that this could happen is if
you're not stripping the headers.

It would be helpful if you could post the fist couple hundred
characters of the string you're trying to decode (postScript).
Stephan
Oct 25 '06 #6
Hello Christof:

Unfortunately: I am not able to answer this question as to why they
increment by 2 as this was written before I was with this company. I was
just given this code as an example of how it works.

Thanks
Andy

"Christof Nordiek" wrote:
Hi Andy,

why do you increment i by 2. Even though Unicode (or UTF-16 to be precise),
is a 2-Byte code, 1 Byte is still 1 Byte. You increnet by to, but copy only
1 Byte per iteration thereby loosing half of each character.

Also it may be more performant copying greater chunks.
Oct 25 '06 #7
Andy,

You say it was working fine until a file didn't work. Does this mean
that there's one file that doesn't work, or that you wrote a system and
it hasn't worked yet?

If it's just this one file, it's not an encoding issue. It's probably
because your PS refers to a PS font rather than a PS page definition.
The PS-to-PDF won't work on your source document because it describes a
different thing.
Stephan

Andy wrote:
Stephan:

Unfortunately: I am not familiar with what is supposed to be sending as
this component was written by someone long before I was at this company, so I
don't have much knowledge of it.

What is happening is: we have item layouts that are XML files. This XML is
being passed into the component, which returns a postScript file (or
postScript of some sort). Once we get this postScript, it is then converted
to a .pdf file. I was to write a windows automated version that followed the
same logic as our web-version (which was manual and time consuming).
Everything seemed to be working wiht my version until we discovered a pdf
that didn't display correctly and it seemed to be an encoding issue.

Here are the first 200 characters of the "postScript" that is returned.

%!PS-AdobeFont-1.0: TimesNewRomanPS-Italic 001.000
%%CreationDate: 2/10/00 at 7:13 PM
%%VMusage: 1024 28747
% Generated by Fontographer 4.1
% Copyright \(c\) 1988, 1990 Adobe Systems Incorporate

I have not stripped out any messages, as it doesn't appear the web-version
was doing this. Perhaps I need to do this?
Stephan
Oct 25 '06 #8
Hello All:

I just wanted to let you know I found the issue. When I write the file out,
I had to set the StreamWriter to System.Text.Endoding.Default and all was
good.

The reason it worked prior was they used different characters on this other
file than other files were using.

Thanks to all for helping. I do greatly apprecaite it.

"ssamuel" wrote:
Andy,

You say it was working fine until a file didn't work. Does this mean
that there's one file that doesn't work, or that you wrote a system and
it hasn't worked yet?

If it's just this one file, it's not an encoding issue. It's probably
because your PS refers to a PS font rather than a PS page definition.
The PS-to-PDF won't work on your source document because it describes a
different thing.
Stephan

Andy wrote:
Stephan:

Unfortunately: I am not familiar with what is supposed to be sending as
this component was written by someone long before I was at this company, so I
don't have much knowledge of it.

What is happening is: we have item layouts that are XML files. This XML is
being passed into the component, which returns a postScript file (or
postScript of some sort). Once we get this postScript, it is then converted
to a .pdf file. I was to write a windows automated version that followed the
same logic as our web-version (which was manual and time consuming).
Everything seemed to be working wiht my version until we discovered a pdf
that didn't display correctly and it seemed to be an encoding issue.

Here are the first 200 characters of the "postScript" that is returned.

%!PS-AdobeFont-1.0: TimesNewRomanPS-Italic 001.000
%%CreationDate: 2/10/00 at 7:13 PM
%%VMusage: 1024 28747
% Generated by Fontographer 4.1
% Copyright \(c\) 1988, 1990 Adobe Systems Incorporate

I have not stripped out any messages, as it doesn't appear the web-version
was doing this. Perhaps I need to do this?
Stephan

Oct 27 '06 #9

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

Similar topics

30
by: aurora | last post by:
I have long find the Python default encoding of strict ASCII frustrating. For one thing I prefer to get garbage character than an exception. But the biggest issue is Unicode exception often pop up...
2
by: garykpdx | last post by:
Every time I think I understand unicode, I prove I don't. I created a variable in interactive mode like this: s = u'ä' where this character is the a-umlaut that worked alright. Then I encoded...
4
by: Majed | last post by:
Hi , all I'm trying to write unicode to a file for another app (not developed with vs2003) to read it. I used StreamWriter with unicode encoding.but I was surprised that the streamwriter adds FFFE...
6
by: Tom | last post by:
Hi I read http://dotgnu.org/pnetlib-doc/System/Text/UnicodeEncoding.html#UnicodeEncoding.GetBytes%28System.String%29%20Method and here is my code: <code string c = "Hello, How are you today?";...
3
by: Shahid | last post by:
Hi, I am trying to display the encrypted value on the console. When I use the following UnicodeEncoding encoding = new UnicodeEncoding( ); string constructedString =...
2
by: THY | last post by:
Hi, I am developing a website in english & chinese both language. whenever I save, it required I set the encoding in advanced save options. But I found there are 4 related to unicode, can anyone...
6
by: Franz Steinhaeusler | last post by:
Hello NG, a little longer question, I'm working on our project DrPython and try fix bugs in Linux, (on windows, it works very good now with latin-1 encoding). On Windows, it works good now,...
0
by: UvT | last post by:
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); Now this creates...
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: 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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.