Re: "Ascii" codes to Hex (again)
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 07/22/04
- Next message: Nick Palmius: "Re: Osama Found Hanged 5750"
- Previous message: Kai Bøhli: ""Ascii" codes to Hex (again)"
- In reply to: Kai Bøhli: ""Ascii" codes to Hex (again)"
- Next in thread: Kai Bøhli: "Re: "Ascii" codes to Hex (again)"
- Reply: Kai Bøhli: "Re: "Ascii" codes to Hex (again)"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 22 Jul 2004 13:43:15 +0100
Kai Bøhli <kaiboeNO-SPAM@online.no> wrote:
> I've got a lot of feedback from (the always helpful) Jon Skeet on this
> subject before. Dispite this I'm still not there - due to my own lack of
> knowledge of course.
Don't worry, we'll crack it :)
> Anyway, I'm "talking to" different labelprinters. They got one thing in
> common -> A char value above 127 must be written in the "dos way".
*Which* DOS way? Installed in different places, you'll need different
encodings. You may be able to leave that question for the moment though
- let's get it working with one particular encoding first...
> The best way to do this AFAIK is to use hex values.
What exactly do you mean by "use hex values"? What bytes are you
expecting to send to the printer?
> By reading hundreds of
> messages in this group, I've come to the code below. It does not work
> properly though. The result of this code is the hex value 0x0 followed
> by the next two signs for the hex value.
Yes, it would...
> If I just can get this to work, then I'm solved the (by far) largest
> problem I've had porting my projects from Delphi.
>
> Any help are greatly apprecitated
>
> protected internal string StringToAscii(string s)
> {
> Encoding en = UTF8Encoding.GetEncoding(850);
First, change that to Encoding.GetEncoding(850);
You're not doing anything UTF8Encoding specific, or indeed doing
anything related to UTF-8 at all - the above gives the wrong
impression. It's not actually going to be causing any errors - just
confusion.
> byte[] codedBytes = en.GetBytes(s);
> char[] codedChars = en.GetChars(codedBytes);
What are you hoping to achieve with this pair of lines? All it will do
is convert characters which aren't in CodePage 850 to question marks,
if you're lucky. Your string will still contain the Unicode characters
you started off with.
> StringBuilder sb = new StringBuilder();
> foreach (char c in codedChars)
> {
> int si = (int)c;
> if (si > 127)
> {
> sb.AppendFormat("\0x{0:x2}",(int)c);
> }
> else
> {
> sb.AppendFormat("{0}",(char)si);
> }
> }
> string newString = sb.ToString();
> return newString;
> }
The string you return is going to be very odd, to be honest. By the
time you get to a stage dealing with encodings, you shouldn't be
dealing with strings - you should be dealing with bytes. How are you
sending the data to the printer in the end? *Something's* got to be
converting the text data to bytes, and *that's* where you want the
encoding to go.
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Nick Palmius: "Re: Osama Found Hanged 5750"
- Previous message: Kai Bøhli: ""Ascii" codes to Hex (again)"
- In reply to: Kai Bøhli: ""Ascii" codes to Hex (again)"
- Next in thread: Kai Bøhli: "Re: "Ascii" codes to Hex (again)"
- Reply: Kai Bøhli: "Re: "Ascii" codes to Hex (again)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|