将任何字符串转换为其acii格式 [英] convert any string to its acii format

查看:65
本文介绍了将任何字符串转换为其acii格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将任何字符串转换为ASCII格式,以便在C#中实现rsa算法,并将转换后的值存储在单个Integer中;因此,请帮助我.

I want to convert any string to its ASCII format for rsa algorithm implementation in C# and the converted value is store in a single Integer; So please help me.

推荐答案

按字符串,我相信您的意思是char.
如果我是正确的话,您可以使用int c = Convert.ToInt32(''a'');
By string, I believe you really mean char.
If I am correct you can achieve this quickly with int c = Convert.ToInt32(''a'');


快速实现此目标,请尝试在
上找到解决方案 http://www.dotnetperls.com/ascii-string-representation [ http://www.dreamincode.net/forums/topic/63794-converting -from-ascii-to-string/ [
Try the solutions at
http://www.dotnetperls.com/ascii-string-representation[^]
http://www.dreamincode.net/forums/topic/63794-converting-from-ascii-to-string/[^]


使用加密(RSA或其他方法)绝对不需要这样做.您确实需要保留编码.所有的加密算法都可以处理字节数组,而别无其他.而且此字节数组与ASCII无关.此外,将Unicode(以一个或另一个UTF; Windows使用UTF-16LE进行文本的内存表示)转换为ASCII意味着信息丢失.您所有的代码点都适合ASCII吗?可能,但不太确定.使用UTF-8:所有代码点<都占用与ASCII相同的空间. 128位仍支持所有代码点,包括32位.

通常,我建议您忘记ASCII:它已经消失了;它的鬼影仅作为Unicode子集存在.如果使用了良好的排版,即使是美国英语文本也包含超过ASCII的代码点.你知道吗?

那么,最后,如何获得该字节数组?简单的.例如,假设我说服您使用UTF-8.然后代码是:
You absolutely don''t need to do it to use encryption (RSA or whatever). You really need to preserve encoding. All encryption algorithms work with arrays of bytes and nothing else. And this array of bytes has nothing to do with ASCII. Moreover, converting Unicode (in one or another UTF; Windows uses UTF-16LE for memory presentation of text) to ASCII means the loss of information. All your code points fit in ASCII? Possibly, but don''t be so sure. Use UTF-8: it takes the same space as ASCII for all code points < 128 bit still supports all code points including 32-bit ones.

Generally, I would advise you to forget ASCII: it''s gone; the ghost of it only exists as a Unicode subset. Even US English text contains code points beyond ASCII, if good typography is used. Did you know that?

So, finally, how to get that array of bytes? Simple. For example, let''s assume I convinced you to use UTF-8. Then the code is:
string sourceText = //... this is your input
byte[] encryptionInput = System.Text.Encoding.UTF8.GetBytes(sourceText);

//now encrypt encryptionInput...

//later decrypt it, obtain some
byte[] decryptionOutput = //decrypt

string decryptedText = new string(System.Text.Encoding.UTF8.GetChars(decryptionOutput));
// if everything is correct, it should give decryptedText == sourceText



这就是它的完成方式.除了System.Text.Encoding.UTF8编码之外,您还可以使用其他任何编码,甚至可以使用ASCII,但是出于多种原因,我强烈建议使用UTF-8.

—SA



This is how it''s done. Instead of System.Text.Encoding.UTF8 encoding, you can use any other, even ASCII, but I strongly recommend UTF-8, by many reasons.

—SA


这篇关于将任何字符串转换为其acii格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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