vb.net 代码返回 Unicode 字符如何转换为 ASCII
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25481694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code Returns Unicode characters How to Convert to ASCII
提问by Alfredo Secusana
Imports Microsoft.VisualBasic
导入 Microsoft.VisualBasic
Public Class VigenereCipher
Public Shared Function Encrypt(ByVal cipherTxt As String, ByVal key As String)
Dim encryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = Asc(GetChar(cipherTxt, i)) _
+ Asc(GetChar(key, i Mod key.Length + 1))
encryptedText += Chr(temp)
Next
Return encryptedText
End Function
Public Shared Function Decrypt(ByVal cipherTxt As String, ByVal key As String)
Dim decryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = Asc(GetChar(cipherTxt, i)) _
- Asc(GetChar(key, i Mod key.Length + 1))
decryptedText += Chr(temp)
Next
Return decryptedText
End Function
End Class
I would want the program to return regular characters because it outputs unicode characters.
我希望程序返回常规字符,因为它输出 unicode 字符。
回答by User999999
Straight from MSDN: You should use the Encoding.ConvertMethod
直接来自MSDN:您应该使用Encoding.Convert方法
Example code (from MSDN):
示例代码(来自 MSDN):
Public Function UnicodeToAscii( Byval unicodeString as String) As String
Dim ascii As Encoding = Encoding.ASCII
Dim unicode As Encoding = Encoding.Unicode
' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)
' Convert the new byte array into a char array and then into a string.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
Return asciiString
End Function
回答by User999999
The code works fine, since i got the same plain text after decryption : I execute the encryption code with input Encrypt="abcdef"and key=""hxc"it produces the output for tempas follows
该代码工作正常,因为我得到解密后的相同的纯文本:我输入执行加密代码Encrypt="abcdef"和key=""hxc"它产生的输出temp如下
when
什么时候
i=1 temp= 217 'ù
i=2 temp= 197 '?
i=3 temp= 203 '?
i=4 temp= 220 'ò
i=5 temp= 200 'ò
i=6 temp= 206 '?
from further checking i got the conclusion that your algorithm will produce a value higher than 150 for most of the inputs. The referencesays that, The extended ASCII codes (character code 128-255) There are several different variations of the 8-bit ASCII table. The table below is according to ISO 8859-1, also called ISO Latin-1. Codes 129-159 contain the Microsoft? Windows Latin-1 extended characters.
通过进一步检查,我得出结论,您的算法将为大多数输入生成高于 150 的值。该参考说,扩展ASCII码(字符代码128-255)有8位ASCII表的几种不同的变化。下表符合 ISO 8859-1,也称为 ISO Latin-1。代码 129-159 包含 Microsoft? Windows Latin-1 扩展字符。
Comclusion:The problem for producing Unicode characters is that the algorithm you ware choosing will produce a value greater than 127for temp.
结论:生成 Unicode 字符的问题是您选择的算法将生成大于127for的值temp。

