Asc和Chr

C#
        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.");
            }

        }

VB.NET
    Public Shared Function Asc(ByVal sCharacter As StringAs 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 IntegerAs 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

posted on 2004-09-25 13:10  小牛哥  阅读(2673)  评论(7编辑  收藏  举报

导航