将ASCII字符转换为十六进制值

时间:2014-02-28 06:19:40

标签: vb.net visual-studio-2010 visual-studio hex

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

我试过了,

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")

任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:4)

请在VB6编程时避免使用过去十年VB.NET方法,HEXASC等方法可以替换为Convert班。

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

将代码放在这里:

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

一个例子:

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

答案 1 :(得分:-1)

我猜你从未对charlist中找到的char做任何事情,因为:

If char1.Contains(letter) Then
Else