?
=?ISO-8859-15?Q?Kai_B=F8hli?=
Hi all !
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.
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". The
best way to do this AFAIK is to use hex values. 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.
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);
byte[] codedBytes = en.GetBytes(s);
char[] codedChars = en.GetChars(codedBytes);
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;
}
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.
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". The
best way to do this AFAIK is to use hex values. 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.
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);
byte[] codedBytes = en.GetBytes(s);
char[] codedChars = en.GetChars(codedBytes);
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;
}