Convert ASCII Character To Its Hexadecimal Value
11,665
Please, avoid the usage of past decade VB6
methods while you are programming in VB.NET
, methods such as HEX
and ASC
can be replaced with the methods provided by the Convert
Class.
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
Author by
Jigar patel
Updated on June 04, 2022Comments
-
Jigar patel almost 2 years
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.
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 = "�" & 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 = "�" & 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 = "�" & 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.