将字符串转换为Ascii代码,然后再转换回字符串 [英] Convert a string to Ascii codes and then back to string again

查看:101
本文介绍了将字符串转换为Ascii代码,然后再转换回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我需要将字符串翻译成Ascii并再次返回一个字符串。下面的代码不适用于

Ascii(Superset)代码高于127.非常感谢任何帮助。

protected internal string StringToAscii(string S)

{

byte [] strArray = Encoding.UTF7.GetBytes(S);

string NewString = Encoding.UTF7.GetString(strArray);

返回NewString;

}


TIA

祝你好运

启Bohli
ka***********@online.no

挪威

Hi all !

I need to translate a string to Ascii and return a string again. The code below dosen''t work for
Ascii (Superset) codes above 127. Any help are greatly appreciated.
protected internal string StringToAscii(string S)
{
byte[] strArray = Encoding.UTF7.GetBytes(S);
string NewString = Encoding.UTF7.GetString(strArray);
return NewString;
}

TIA
Best wishes
Kai Bohli
ka***********@online.no
Norway

推荐答案

Kai Bohli< ka **** @ online.nospam>写道:
Kai Bohli <ka****@online.nospam> wrote:
我需要将字符串转换为Ascii并再次返回一个字符串。以下
代码不适用于127以上的Ascii(Superset)代码。
I need to translate a string to Ascii and return a string again. The
code below dosen''t work for Ascii (Superset) codes above 127.




127以上没有ASCII代码。你不能用

字符翻译字符串>将Unicode 127转换为ASCII而不会丢失数据。


现在,你真的想要你的方法做什么?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



There are no ASCII codes above 127. You can''t translate a string with
characters > Unicode 127 into ASCII without losing data.

Now, what do you *really* want your method to do?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

嗨John!
Hi John !
127以上没有ASCII码。你不能翻译带有
字符的字符串> Unicode 127转换为ASCII而不会丢失数据。


我知道 - 我已经阅读了你的常见问题解答,但我不知道该怎么说:)

现在,你是什么*真的*想要你的方法吗?
There are no ASCII codes above 127. You can''t translate a string with
characters > Unicode 127 into ASCII without losing data.
I know - I''ve read your faq, but didn''t quite know how to put it :)
Now, what do you *really* want your method to do?




我的用户在应用程序中输入一些数据,如包裹的接收者,地址等。

假设这个包裹要去瑞典,那里的Ascii代码132(参见Ascii字符代码图表2
Visual Studio中的
)很常见。


现在,我不知道字符中的字符串(必须翻译的)将是,并且我不会知道它们将出现在字符串中的哪个位置。


labelprinters仅支持标准文本盒子外面。必须发送任何特殊字符

作为Ascii代码。


为了保持简单,我想将原始字符串(用户输入的字符串)发送到

函数并返回 ;&的Ascii QUOT;代码,所以我可以直接写到打印机端口。


TIA


祝你好运

Kai Bohli
ka***********@online.no
挪威



My users enter some data into the app, like receiver of a package, adresses etc.
Say that this package are going to Sweden, where "Ascii" code 132 (See Ascii Character Codes Chart 2
in Visual Studio) is common.

Now, I don''t know in which strings the characters (that must be translated) will be, and and I don''t
know where in the strings they will appear.

The labelprinters support just standard text out of the box. Any special characters has to be sent
as "Ascii" codes.

In order to keep it simple, I would like to send the orginal string (that the user entered) into a
function and return the "Ascii" codes so I can write it directly to the printerport.

TIA

Best wishes
Kai Bohli
ka***********@online.no
Norway


Kai Bohli< ka **** @ online.nospam>写道:
Kai Bohli <ka****@online.nospam> wrote:
127以上没有ASCII码。你不能翻译带有
字符的字符串>将Unicode 127转换为ASCII而不会丢失数据。
我知道 - 我已经阅读了你的常见问题解答,但我不知道该怎么说:)
There are no ASCII codes above 127. You can''t translate a string with
characters > Unicode 127 into ASCII without losing data.
I know - I''ve read your faq, but didn''t quite know how to put it :)
现在,你真的*想要你的方法做什么?
Now, what do you *really* want your method to do?



我的用户在应用程序中输入一些数据,比如包的接收者,地址等。
说这个包将前往瑞典,在那里Ascii代码132
(参见Visual Studio中的Ascii字符代码图表2)很常见。



My users enter some data into the app, like receiver of a package, adresses etc.
Say that this package are going to Sweden, where "Ascii" code 132
(See Ascii Character Codes Chart 2 in Visual Studio) is common.




很遗憾MSDN仍然有调用它的页面ASCII :(

现在,我不知道哪些字符串(必须翻译)将是,并且我不知道字符串在哪里
将会出现。

labelprinters支持开箱即用的标准文本。任何
特殊字符必须以Ascii代码发送。


你需要知道*确切*要使用的编码.ASCII本身并不是
包含你感兴趣的字符,所以你需要知道哪个

编码你* do *想 - 以及打印机将支持的内容。希望

您的打印机手册会告诉您。

为了保持简单,我想将原始字符串
(用户输入的)发送到函数中并返回Ascii代码
所以我可以写它直接发送到打印机端口。



It''s unfortunate that the MSDN still has pages which call it ASCII :(
Now, I don''t know in which strings the characters (that must be
translated) will be, and and I don''t know where in the strings they
will appear.

The labelprinters support just standard text out of the box. Any
special characters has to be sent as "Ascii" codes.
You need to know *exactly* what encoding to use. ASCII itself doesn''t
contain the character you''re interested in, so you need to know which
encoding you *do* want - and what the printer will support. Hopefully
your printer manual will tell you.
In order to keep it simple, I would like to send the orginal string
(that the user entered) into a function and return the "Ascii" codes
so I can write it directly to the printerport.




嗯,首先要意识到的是,如果你想要一个

编码形式的文本,你不应该返回一个字符串开始 - 你

应该返回一个字节数组。在那之后,您只需要知道打印机所期望的编码,然后使用它。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Well, the first thing to realise is that if you want the text in an
encoded form, you shouldn''t be returning a string to start with - you
should be returning an array of bytes. After that, you just need to
know which encoding your printer is expecting, and use that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于将字符串转换为Ascii代码,然后再转换回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆