如何将char转换为ASCII码? [英] How to convert char to ASCII code?

查看:143
本文介绍了如何将char转换为ASCII码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将每个字符转换为ASCII码?

解决方案

如果你的意思是字符串中的每个字符,那么试试这个:

< pre lang =vb> string s = ...
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(一个或多个);





大声笑我只想知道加密工作的方式!


好​​的,所以首先看一下加密是如何工作的:除了最基本的凯撒(或替代)Cyphers之外,它们都没有与字符一起工作 - 它的所有字节都是方式。

替换很简单:设置一组有效输入和一组有效输出,并进行转换。 (我打算用C#做这些,而不是VB,因为它是我认为更好的一种语言 - 如果你需要的话,还有在线转换器)

  char  [] CaesarIP =  new   char  [] {< span class =code-string>' ''  A''  B''  C''  D''  E' '  F''  G''  H'' 我''  J''  K''  L''  M''  N''  O' '  P''  Q''  R''  S''  T''  U''  V''  W''  X''  Y''  Z'}; 
char [] CaesarOP = new char [] {' N'' M'' D'' U'' < span class =code-string> L',' W'' C'' P'' H'' Q'' X'' R'' K'' T'' '' ' F'' G'' Y'' O'' A'' V'' Z',< span class =code-string>' J'' S'' E'' B'};
private string CaesarEncrypt( string 输入)
{
char [] data = input.ToCharArray();
for int index = 0 ; index < data.Length; index ++)
{
data [index] = CaesarOP [Array.IndexOf(CaesarIP,data [指数])];
}
return new string (数据);
}
私有 void button1_Click( object sender,EventArgs e)
{
Console.WriteLine(CaesarEncrypt( 你好世界));
...



如果你改用字典,循环就会变得简单得多。

但是解密起来非常非常简单:每个月都会发布数十本书籍,从未接触过全国各地计算机的老太太们正在解决这些问题,而不会挣扎出来: http://www.puzzlemagazines.co.uk/code-breakers [ ^ ]



为了更安全,您需要忽略字符并开始申请数学运算可以逆转。这意味着将数据作为字节而不是字符处理,因为并非您生成的所有值都是有效字符!所以现在,你需要两种方法:

  private   byte  [] BasicEncrypt( string  input)
{
byte [ ] bytes = System.Text.Encoding.Unicode.GetBytes(input);
return 个字节;
}
私有 字符串 BasicDecrypt( byte [] bytes)
{
string output = System.Text.Encoding.Unicode.GetString(bytes);
return 输出;
}
私有 void button1_Click( object sender,EventArgs e)
{
Console.WriteLine(BasicDecrypt(BasicEncrypt( Hello World!\ n这是文本?)));



其中告诉我们基本转换有两种方式。



最简单的加密方式是ADD - 如果你为每个字节添加一个固定值,你可以减去它并回到你开始的地方:

  private   byte  [] BasicEncrypt( string  input)
{
byte [] bytes = System.Text.Encoding.Unicode.GetBytes(input);
for int index = 0 ; index < bytes.Length; index ++)
{
bytes [index] + = 42 ;
}
返回个字节;
}
私有 字符串 BasicDecrypt( byte [] bytes)
{
for int index = 0 ; index < bytes.Length; index ++)
{
bytes [index] - = 42 ;
}
string output = System.Text.Encoding.Unicode.GetString(bytes);
return 输出;
}

但数据仍然不完全安全 - 主要是因为它仍然是有效的替代密码。

稍微好一点的方法是使用更复杂的数学,并在数学中使用索引 - 这样HELLO中的两个'L'字符不会产生相同的输出:

  private   byte  [] BasicEncrypt( string 输入)
{
byte [] bytes = System.Text.Encoding.Unicode.GetBytes(input);
for int index = 0 ; index < bytes.Length; index ++)
{
bytes [index] + =( byte )( 42 + index);
bytes [index] ^ = 123 ;
}
返回个字节;
}
私有 字符串 BasicDecrypt( byte [] bytes)
{
for int index = 0 ; index < bytes.Length; index ++)
{
bytes [index] ^ = 123 ;
bytes [index] - =( byte )( 42 + index);
}
string output = System.Text.Encoding.Unicode.GetString(bytes);
return 输出;
}
私有 void button1_Click( object sender,EventArgs e)
{
Console.WriteLine(BasicDecrypt(BasicEncrypt( Hello World!\ n这是文本?)));



请注意,我们必须按相反的顺序进行数学计算,否则我们无法获得正确的输出。

如果你看一下中间的字节流,你会发现它看起来像是总的gobbledegook!从现在开始,您根本无法将加密数据显示为字符串 - 它只是不起作用,如果您尝试将其转换为字符串,那么您将无法获得非常非常好的机会使用字符串将其解密回原始输入,除非您使用类似每个字节的十六进制字符对表示,或者可能使用Base64来获得可以转换回字节数组的可传输字符串。



从这里开始,下一步是开始使用Key:基本上是一个密码,它控制着如何转换数据。最简单的方法是将键值添加到输入值,但我们也可以合并基本内容:

  private   byte  [] KeyedEncrypt( string  input, string  key)
{
byte [] bytes = System.Text.Encoding.Unicode.GetBytes(input);
byte [] keys = System.Text.Encoding.Unicode.GetBytes(key);
int keyIndex = 0 ;
for int index = 0 ; index < bytes.Length; index ++)
{
if (keyIndex > = keys.Length)
{
keyIndex = 0 ;
}
bytes [index] + =( byte )(keys [keyIndex ++] + index);
bytes [index] ^ = 123 ;
}
返回个字节;
}
private string KeyedDecrypt( byte [] bytes, string key)
{
byte [] keys = System.Text.Encoding.Unicode.GetBytes(key);
int keyIndex = 0 ;
for int index = 0 ; index < bytes.Length; index ++)
{
if (keyIndex > = keys.Length)
{
keyIndex = 0 ;
}
bytes [index] ^ = 123 ;
bytes [index] - =( byte )(keys [keyIndex ++] + index);
}
string output = System.Text.Encoding.Unicode.GetString(bytes);
return 输出;
}
私有 void button1_Click( object sender,EventArgs e)
{
Console.WriteLine(KeyedDecrypt(KeyedEncrypt( Hello World!\ n这是文本? 我的密码), 我的密码));

这还不错 - 如果国家安全局对此感兴趣,可能需要普通PC才能解密这个问题:甚至可能需要几微秒! :笑:

但这是基本的想法:使用可以反转的数学运算,以及控制数学运算方式的秘密信息。这就是所有加密所做的,它只是使用了更多,更复杂的数学:看看Wiki并且它将开始解释(在可怕的数学中我宁愿避免自己!)



有意义吗?


您可以通过以下链接找到解决方案 https://www.google.co.uk/?gfe_rd=cr& ei = 7f1XU-WoH6bR8gfb-ICYDA #q =如何+转换+每个+ char +到+ ASCII +代码+ vb.net [ ^ ]



请至少尝试在此处发布问题之前尝试进行自己的研究


这是不可能的.NET和许多现代平台。 .NET中的字符是Unicode字符。这些字符中只有少数符合某些ASCII码,只有代码点的字符低于128.所有字符都有不同的显示,具体取决于UTF,包括与ASCII范围匹配的那些。 Unicode只定义了被理解为从字体和其他细节中抽象出来的文化实体的字符之间的对应关系,以及在数学意义上理解的整数值(称为代码点),从它们的二进制表示中抽象出来(这是由UTFs定义)。 ASCII是过时的,面对它。

有些背景,请参阅:

http://en.wikipedia.org/wiki/Unicode [ ^ ],

http://www.unicode.org [ ^ ],

http://en.wikipedia.org/wiki/Unicode_Transformation_Format#Unicode_Transformation_Format_and_Universal_Character_Set [ ^ ],

http: //www.unicode.org/faq/utf_bom.html [ ^ ]。



-SA


How Convert each char to ASCII code?

解决方案

If you mean every character in a string, then try this:

string s = ...
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);



"lol i'm just want know how encryption work!"

OK, so start by looking at how encryption works: and with the exception of the most basic Caesar (or Substitution) Cyphers, none of them work with characters - it's bytes all the way.
Substitution is easy: Set up an array of valid inputs, and a array of valid outputs, and convert them. (I'm going to do these in C#, not VB as it's a language I think better in - there are online converters if you need them)

char[] CaesarIP = new char[] { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] CaesarOP = new char[] { 'N', 'M', 'D', 'U', 'L', 'W', 'C', 'P', 'H', 'Q', 'X', 'R', 'K', 'T', ' ', 'I', 'F', 'G', 'Y', 'O', 'A', 'V', 'Z', 'J', 'S', 'E', 'B' };
private string CaesarEncrypt(string input)
    {
    char[] data = input.ToCharArray();
    for (int index = 0; index < data.Length; index++)
        {
        data[index] = CaesarOP[Array.IndexOf(CaesarIP, data[index])];
        }
    return new string(data);
    }
private void button1_Click(object sender, EventArgs e)
    {
    Console.WriteLine(CaesarEncrypt("HELLO WORLD"));
    ...


The loop becomes a lot simpler to read if you use a Dictionary instead.
But these are very, very simple to decrypt: every month dozens of "books" are published and old ladies who have never touched a computer all over the country are solving them without breaking into a sweat: http://www.puzzlemagazines.co.uk/code-breakers[^]

To get more secure, you need to ignore characters and start applying math operations which can be reversed. This means handling data as bytes instead of characters, because not all the values you generate will be valid characters! So now, you need two methods:

private byte[] BasicEncrypt(string input)
    {
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
    return bytes;
    }
private string BasicDecrypt(byte[] bytes)
    {
    string output = System.Text.Encoding.Unicode.GetString(bytes);
    return output;
    }
private void button1_Click(object sender, EventArgs e)
    {
    Console.WriteLine(BasicDecrypt(BasicEncrypt("Hello World!\nThis is text?")));


Which show us that the basic conversion works both ways.

The simplest encrypt is ADD - if you add a fixed value to each byte, you can subtract it and get back to where you started:

private byte[] BasicEncrypt(string input)
    {
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
    for (int index = 0; index < bytes.Length; index++)
        {
        bytes[index] += 42;
        }
    return bytes;
    }
private string BasicDecrypt(byte[] bytes)
    {
    for (int index = 0; index < bytes.Length; index++)
        {
        bytes[index] -= 42;
        }
    string output = System.Text.Encoding.Unicode.GetString(bytes);
    return output;
    }

But the data still isn't exactly secure - mostly because it's still effectively a substitution cypher.
A slightly better way is to use more complex math, and to use the index in the math as well - that way the two 'L' characters in "HELLO" don't generate the same output:

private byte[] BasicEncrypt(string input)
    {
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
    for (int index = 0; index < bytes.Length; index++)
        {
        bytes[index] += (byte)(42 + index);
        bytes[index] ^= 123;
        }
    return bytes;
    }
private string BasicDecrypt(byte[] bytes)
    {
    for (int index = 0; index < bytes.Length; index++)
        {
        bytes[index] ^= 123;
        bytes[index] -= (byte)(42 + index);
        }
    string output = System.Text.Encoding.Unicode.GetString(bytes);
    return output;
    }
private void button1_Click(object sender, EventArgs e)
    {
    Console.WriteLine(BasicDecrypt(BasicEncrypt("Hello World!\nThis is text?")));


Notice that we have to do the math in the reversed order, or we don't get the right output.
And if you look at the intermediate stream of bytes, you'll see it looks like total gobbledegook! From this point on, you can't display the encrypted data as a string at all - it just doesn't work, and if you try to convert it to a string the chances are very, very good that you won't be able to use the string to decrypt it back to the original input, unless you use something like a "Hex character pair" representation of each byte, or maybe Base64 to get a "transmittable" string that can be converted back to an array of bytes.

From here, the next step is to start using a Key: basically a password which controls how the data to convert is changed. The simplest is to add the key value to the input value, but lets combine the basic stuff as well:

private byte[] KeyedEncrypt(string input, string key)
    {
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
    byte[] keys = System.Text.Encoding.Unicode.GetBytes(key);
    int keyIndex = 0;
    for (int index = 0; index < bytes.Length; index++)
        {
        if (keyIndex >= keys.Length)
            {
            keyIndex = 0;
            }
        bytes[index] += (byte)(keys[keyIndex++] + index);
        bytes[index] ^= 123;
        }
    return bytes;
    }
private string KeyedDecrypt(byte[] bytes, string key)
    {
    byte[] keys = System.Text.Encoding.Unicode.GetBytes(key);
    int keyIndex = 0;
    for (int index = 0; index < bytes.Length; index++)
        {
        if (keyIndex >= keys.Length)
            {
            keyIndex = 0;
            }
        bytes[index] ^= 123;
        bytes[index] -= (byte)(keys[keyIndex++] + index);
        }
    string output = System.Text.Encoding.Unicode.GetString(bytes);
    return output;
    }
private void button1_Click(object sender, EventArgs e)
    {
    Console.WriteLine(KeyedDecrypt(KeyedEncrypt("Hello World!\nThis is text?", "My secret password"), "My secret password"));

This isn't bad - it would probably take the average PC quite a while to decrypt this if the NSA were interested in it: maybe even a couple of microseconds! :laugh:
But that's the basic idea: use a math operation you can reverse, and "secret information" that controls how the math works. That's all any encryption does, it just uses much, much more complex math: have a look at Wiki and it'll start to explain (in horrible math that I'd rather avoid myself!)

Make sense?


You will find your solution on one of the following links https://www.google.co.uk/?gfe_rd=cr&ei=7f1XU-WoH6bR8gfb-ICYDA#q=How+Convert+each+char+to+ASCII+code+vb.net[^]

Please, at least try to make an attempt to do your own research before posting a question here


It is impossible in .NET and many modern platforms. A character in .NET is the Unicode character. Only few of those characters match some ASCII codes, only the characters of the code points below 128. All characters have different presentations, depending on UTF, including those matching the ASCII range. Unicode only defines correspondence between "characters" understood as cultural entities abstracted from fonts and other details, and integers values (called code points) which are understood in their mathematical sense, abstracted from their binary representations (which are defined by UTFs). ASCII is obsolete, face it.
For some background, please see:
http://en.wikipedia.org/wiki/Unicode[^],
http://www.unicode.org[^],
http://en.wikipedia.org/wiki/Unicode_Transformation_Format#Unicode_Transformation_Format_and_Universal_Character_Set[^],
http://www.unicode.org/faq/utf_bom.html[^].

—SA


这篇关于如何将char转换为ASCII码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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