Asc和Chr
C#
VB.NET
可以完全替代Microsoft.VisualBasic.Strings.Asc和Microsoft.VisualBasic.Strings.Chr
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[]{(byte)asciiCode};
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[]{(byte)asciiCode};
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
VB.NET
Public Shared Function Asc(ByVal sCharacter As String) As Integer
If sCharacter.Length = 1 Then
Dim oASCIIEncoding As New System.Text.ASCIIEncoding
Dim iAsciiCode As Integer = CInt(oASCIIEncoding.GetBytes(sCharacter)(0))
Return iAsciiCode
Else
Throw New Exception("Character is not valid.")
End If
End Function
Public Shared Function Chr(ByVal iAsciiCode As Integer) As String
If iAsciiCode >= 0 And iAsciiCode <= 255 Then
Dim oASCIIEncoding As New System.Text.ASCIIEncoding
Dim oByteArray() As Byte = {CByte(iAsciiCode)}
Dim sCharacter As String = oASCIIEncoding.GetString(oByteArray)
Return sCharacter
Else
Throw New Exception("ASCII Code is not valid.")
End If
End Function
If sCharacter.Length = 1 Then
Dim oASCIIEncoding As New System.Text.ASCIIEncoding
Dim iAsciiCode As Integer = CInt(oASCIIEncoding.GetBytes(sCharacter)(0))
Return iAsciiCode
Else
Throw New Exception("Character is not valid.")
End If
End Function
Public Shared Function Chr(ByVal iAsciiCode As Integer) As String
If iAsciiCode >= 0 And iAsciiCode <= 255 Then
Dim oASCIIEncoding As New System.Text.ASCIIEncoding
Dim oByteArray() As Byte = {CByte(iAsciiCode)}
Dim sCharacter As String = oASCIIEncoding.GetString(oByteArray)
Return sCharacter
Else
Throw New Exception("ASCII Code is not valid.")
End If
End Function
可以完全替代Microsoft.VisualBasic.Strings.Asc和Microsoft.VisualBasic.Strings.Chr