Re: "Ascii" codes to Hex (again)

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 07/22/04


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


Relevant Pages

  • Hex editor display - can this be more pythonic?
    ... I'm building a hex line editor as a first real Python programming exercise. ... I had considered using the .translatemethod of strings, however this would require a larger translation table than my printable string. ... Where printing chars are shown in parenthesis, characters with Python escape sequences will be shown as their escapes in parens., while non-printing chars with no escapes will be shown with nothing in parens. ...
    (comp.lang.python)
  • Re: compilation problem
    ... want this utility to print decimal for hex numbers entered. ... IF the third argument passed is a string specifying a decimal number ... TERMINATE with an EXIT_SUCCESS returncode ...
    (comp.lang.c)
  • Re: Need a sysRPL CRC16 routine.
    ... Thanks for your thoughts on the string> hex and vice versa. ... Sysrpl is not particularly intuitive on this point. ... effort he has put into the Debug4x programming environment. ...
    (comp.sys.hp48)
  • Entering strings as user input but interpreting as Python input (sort of)
    ... I would want the quoted part to be interpreted as if I entered it into Python itself as: ... In other words, if a quoted string occurs in the user input, I want only that part to be treated as a Python string. ... The point of this in the context of the hex editor is that the user should be able to enter hex bytes without qualifications like "0xXX" but rather as simply: "0A 1B 2C" etc. but also be able to input a string without having to type in hex ASCII codes. ...
    (comp.lang.python)
  • Re: Converting ascii to hex
    ... Why such meaningless and unreadable ... hex constants? ... Did you perhaps mean that "ciptmp is a CString ... For that matter, since you don't show how the string is read in, how do you know that the ...
    (microsoft.public.vc.mfc)