vb.net 将 ASCII 字符转换为其十六进制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22087710/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:56:07  来源:igfitidea点击:

Convert ASCII Character To Its Hexadecimal Value

vb.netvisual-studio-2010visual-studiohex

提问by Jigar patel

I am trying to replace the ascii character in a word file to its respected hexadecimal value but the problem is only uppercase characters that exist are replacing with proper values and lowercase characters are getting replaced with the uppercase entities.

我试图将 word 文件中的 ascii 字符替换为其受尊重的十六进制值,但问题是只有存在的大写字符被替换为正确的值,小写字符被大写实体替换。

I have tried this,

我试过这个,

Dim var As String
            Dim char1 As String = "!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ " & vbCrLf
            Dim values As Char() = objDoc.Range.Text
            For Each letter As Char In values
                If char1.Contains(letter) Then
                Else
                    var = Convert.ToString(Convert.ToInt32(letter), 16)
                    If var.Length = 1 Then
                        Dim FindObject2 As Word.Find = objDoc.Content.Find
                        With FindObject2
                            .ClearFormatting()
                            .Text = letter
                            .Replacement.ClearFormatting()
                            .Replacement.Text = "&#x000" & StrConv(var, VbStrConv.None) & ";"
                            .Execute(Replace:=Word.WdReplace.wdReplaceAll)
                        End With
                    ElseIf var.Length = 2 Then
                        Dim FindObject2 As Word.Find = objDoc.Content.Find
                        With FindObject2
                            .ClearFormatting()
                            .Text = letter
                            .Replacement.ClearFormatting()
                            .Replacement.Text = "&#x00" & StrConv(var, VbStrConv.None) & ";"
                            .Execute(Replace:=Word.WdReplace.wdReplaceAll)
                        End With
                    ElseIf var.Length = 3 Then
                        Dim FindObject2 As Word.Find = objDoc.Content.Find
                        With FindObject2
                            .ClearFormatting()
                            .Text = letter
                            .Replacement.ClearFormatting()
                            .Replacement.Text = "&#x0" & StrConv(var, VbStrConv.None) & ";"
                            .Execute(Replace:=Word.WdReplace.wdReplaceAll)
                        End With
                    End If
                End If
            Next
            Exit For
        Next
    Catch ex As Exception

    End Try

    objDoc.Save()
    objDoc.Close()
    objapp.Quit()

    MsgBox("Process Completed")

Any help will be really appreciated.

任何帮助将不胜感激。

回答by ElektroStudios

Please, avoid the usage of past decade VB6methods while you are programming in VB.NET, methods such as HEXand ASCcan be replaced with the methods provided by the ConvertClass.

请避免VB6在您编程时使用过去十年的方法VB.NET,例如HEXASC的方法可以替换为Convert类提供的方法。

var = Convert.ToString(Convert.ToInt32(letter), 16)

And place your code here:

并将您的代码放在这里:

If char1.Contains(letter) Then
   ' Here the instructions to do when a character is found...
Else

An Example:

一个例子:

Dim AscChars As Char() =
    "!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ " _
    & Environment.NewLine

Dim HexValue As String = String.Empty

Dim sb As New System.Text.StringBuilder

For Each c As Char In AscChars

    HexValue = Convert.ToString(Convert.ToInt32(c), 16)

    sb.Clear()
    sb.AppendLine(String.Format("ASC: {0}", CStr(c)))
    sb.AppendLine(String.Format("HEX: {0}", HexValue))

    MessageBox.Show(sb.ToString, "Character conversion")

Next c

回答by GinSonic

I guess you never do anything with a found char in charlist because:

我猜你永远不会对在 charlist 中找到的字符做任何事情,因为:

If char1.Contains(letter) Then
Else