接收字符的函数,检查是否是字符并更改ti ascii代码

时间:2015-11-17 11:53:22

标签: vba char

我想编写一个接收字符(作为字符串)的函数,并返回该字符的ascii代码(作为整数)。这个函数假设它收到的字符串实际上是一个字符,而不是ascii代码,所以它需要检查一下。

如果函数检测到字符串作为字符的ascii代码,则该函数应返回与整数相同的字符串。 (除非字符串是空格字符的ascii代码或ascii代码无效。如果是,则函数应该抛出错误。)

如果函数检测到字符串为字符,则该函数应返回ascii代码。 (除非字符串中有几个字符。如果是这样,函数应该抛出错误。)

我意识到这可能是不可能的,但如果您有任何想法,请尽量回答这个问题。我想要一个用vba代码或伪代码的答案。

2 个答案:

答案 0 :(得分:0)

您可以使用“if isnumeric”来检查它是否为数字。如果要检查是否有任何部件是编号,可以将字符串拆分为任意数量的部件,并检查每个部件是否为数字。如果检查没有解决(如果有很多检查你可能想要使用标志)那么你可以使用asc(* string)来获取字符串的ascii值。 如果您想检查字符串中的每个单个字母,您可以

for i = 1 to len(string)
if isnumeric(left(right(string,i),1)) then
numericflag = true
endif
next i
if numericflag = true then
'do something to resolve the error
else
output = asc(string)
endif

答案 1 :(得分:0)

  • 此函数假定不允许除SPACE之外的空格(退格,制表符等)。

收到一个字符串。该字符串可以是用户希望知道ascii代码的字符,或者是用户希望变成整数的ascii代码。

Public Function CharToAscii(ByVal char As String) As Integer
    Dim ascii As Integer    'the variable that will be returned
    If Len(char) = 0 Then   'if empty string was sent...
        MsgBox "You sent an empty string" '...raise error
        GoTo errFunction
    ElseIf Len(char) > 1 Then   'if char is longer than one character...
        If IsNumeric(char) Then '...if the string is a number, assumed to be ascii code and...
            Select Case CInt(char)
                Case 0 To 31, 127 '...if the number is whitespace in ascii, raise error
                    MsgBox "You have entered a string of numbers." & Chr(10) & _
                    "We assumed you meant to get this string as a ascii code, but this string is code for a whitespace."
                    GoTo errFunction
                Case Is < 0, Is > 127 '...if the number does not exist in ascii, raise error
                    MsgBox "You have entered a string of numbers." & Chr(10) & _
                    "We assumed you meant to get this string as a ascii code, But there is no character for this number."
                    GoTo errFunction
            End Select
            ascii = CInt(char) '...if no error was raise,the number is OK, so return it
        Else                '...if the string is characters...
            MsgBox "you have sent more than one character. We assume you meant the first."
            Select Case Asc(char) '...check the ascii code for whitespace and...
                Case 0 To 31, 127 '...if whitespace, raise error
                    MsgBox "But it was a whitespace."
                    GoTo errFunction
            End Select      'if no error was raised, the number is okay
            ascii = Asc(char) 'asc() returns the ascii for the first character in a string.
        End If
    Else                    ' if only one character was received...
        Select Case Asc(char)
            Case 0 To 31, 127 '...if it is a whitespace, raise error
                MsgBox "You have entered a whitespace."
                GoTo errFunction
        End Select
        ascii = Asc(char)   '...if not return it
    End If
    CharToAscii = ascii
    GoTo finish
errFunction:
    MsgBox "please try again" 'or anything else you want to do in case of error
finish:
End Function

注意:只要有消息框,就表示存在错误。您应该将msgbox更改为以想要的方式处理错误的代码。