MACRO to convert to hex

  • Thread starter UnicodeConfusion
  • Start date
U

UnicodeConfusion

I need to convert ascii characters (including unicode) to 4 digit hexadecimal
representation. The alt+x (Selection.ToggleCharacterCode) functionality
works for a single character, but I am trying to write a macro to convert
every character in the file. I can't figure out how to parse through the
document...

any suggestions?

Stephanie
 
H

Helmut Weber

Hi Stephanie,

how about this one:

Sub Test2()
Dim rngChr As Range
Dim strTmp As String
For Each rngChr In ActiveDocument.Characters
strTmp = CStr(Hex(AscW(rngChr)))
' cause I could't get to work it with format
While Len(strTmp) < 4
strTmp = "0" & strTmp
Wend
MsgBox strTmp ' or your code
Next
End Sub



--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Nurick

Hi Helmut

This might be faster:

...
Dim strTmp As String * 4
...
strTemp = Right("000" & Hex(AscW(rngChr)),4)
MsgBox ...
 
H

Helmut Weber

Hi John,
This might be faster:

...
Dim strTmp As String * 4
...
strTemp = Right("000" & Hex(AscW(rngChr)),4)
MsgBox ...

At least it looks better, and faster, too. :)

But in fact, it isn't. :-(

For 70,000 characters, I get differences of some hundredths seconds.
Sometimes in favor of solution, sometimes in favor of mine.

Have a nice day.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Nurick

At least it looks better, and faster, too. :)

But in fact, it isn't. :-(

For 70,000 characters, I get differences of some hundredths seconds.
Sometimes in favor of solution, sometimes in favor of mine.

Thanks for the feedback, Helmut.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top