473,698 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Conten tEncoding=Syste m.Text.Encoding .Unicode;
Response.Conten tType = "applicatio n/postscript";
Response.Buffer =true;
Response.Append Header("Content-Disposition","a ttachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Enc oding.Unicode.G etBytes(sPSFile );
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length ;i+=2)
{
by[0]=ba[i];
Response.Binary Write(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.Enc oding.Unicode.G etBytes(postScr ipt);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);

But I know the encoding.GetStr ing() 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 8486
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. FromBase64Strin g() 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.Conten tEncoding=Syste m.Text.Encoding .Unicode;
Response.Conten tType = "applicatio n/postscript";
Response.Buffer =true;
Response.Append Header("Content-Disposition","a ttachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Enc oding.Unicode.G etBytes(sPSFile );
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length ;i+=2)
{
by[0]=ba[i];
Response.Binary Write(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.Enc oding.Unicode.G etBytes(postScr ipt);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);

But I know the encoding.GetStr ing() 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. FromBase64Strin g(postScript);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);

But now I am getting an error stating: "System.FormatE xception: Invalid
character in a Base-64 string. at System.Convert. FromBase64Strin g(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. FromBase64Strin g() 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.Conten tEncoding=Syste m.Text.Encoding .Unicode;
Response.Conten tType = "applicatio n/postscript";
Response.Buffer =true;
Response.Append Header("Content-Disposition","a ttachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Enc oding.Unicode.G etBytes(sPSFile );
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length ;i+=2)
{
by[0]=ba[i];
Response.Binary Write(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.Enc oding.Unicode.G etBytes(postScr ipt);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);

But I know the encoding.GetStr ing() 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. FromBase64Strin g(postScript);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);

But now I am getting an error stating: "System.FormatE xception: Invalid
character in a Base-64 string. at System.Convert. FromBase64Strin g(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. FromBase64Strin g() 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.Conten tEncoding=Syste m.Text.Encoding .Unicode;
Response.Conten tType = "applicatio n/postscript";
Response.Buffer =true;
Response.Append Header("Content-Disposition","a ttachment; filename=\"" +
sFilename + "\"");
>
Byte[] ba = System.Text.Enc oding.Unicode.G etBytes(sPSFile );
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length ;i+=2)
{
by[0]=ba[i];
Response.Binary Write(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.Enc oding.Unicode.G etBytes(postScr ipt);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);
>
But I know the encoding.GetStr ing() 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**@discussio ns.microsoft.co mschrieb im Newsbeitrag
news:1A******** *************** ***********@mic rosoft.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.Conten tEncoding=Syste m.Text.Encoding .Unicode;
Response.Conten tType = "applicatio n/postscript";
Response.Buffer =true;
Response.Append Header("Content-Disposition","a ttachment; filename=\"" +
sFilename + "\"");

Byte[] ba = System.Text.Enc oding.Unicode.G etBytes(sPSFile );
Byte[] by = new System.Byte[1];
for(int i=0;i<ba.Length ;i+=2)
{
by[0]=ba[i];
Response.Binary Write(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.Enc oding.Unicode.G etBytes(postScr ipt);
UnicodeEncoding encoding = new UnicodeEncoding ();
postScript = encoding.GetStr ing(ba);

But I know the encoding.GetStr ing() 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.End oding.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
2754
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 in unexpected places and only when a non-ASCII or unicode character first found its way into the system. Below is an example. The program may runs fine at the beginning. But as soon as an unicode character u'b' is introduced, the program boom...
2
1546
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 it like this: s.encode( 'latin1') and it printed out a sigma (totally wrong)
4
2458
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 to the start of the file,which stopes the other app from reading it!! any idea how to stope it frome doing that,do I have to use another class #####writer that supports unicode? help me Please! Thanks
6
2490
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?"; //it should be other language, not English UnicodeEncoding Unicode = new UnicodeEncoding() byte encodedBytes = Unicode.GetBytes(c)
3
3607
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 = encoding.GetString(encryptedData); And I print constructedString on to the console, I see "??????" When I
2
2625
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 tell me what's the different and which to choose ? Unicode (UTF-8 without signature) - Codepage 65001 Unicode (UTF-8 with signature) - Codepage 65001 Unicode - Codepage 1200
6
3447
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, using setappdefaultencoding and the right encoding for open with styled text control with the right encoding the files. (I see the german Umlauts äöü and the "strong 's'" "ß") The case:
0
1618
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 finalfile.txt with ANSI Encoding ...which is a default. Either tell me how to change the default or how to create a new file with ecoding already set to unicode. I am already using the work around by which i create a finalfile.txt myself and then 'save...
1
32925
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); ###Question: Now this creates finalfile.txt with ANSI Encoding ...which is a default. Either tell me how to change the default or how to create a
0
8673
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
8601
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
9156
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
9021
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...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7716
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...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.