Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Hex To ASCII

Posted on 2002-06-29
Medium Priority
1,044 Views
Hi All : I am just making up a small app to convert ASCII (or more to the point, JavaScript) to Hex. Now this I have done but need to go back the other way and have run into a few problems. Hope someone can help :

pstrOrgCode = Text1.Text
pintStrLen = Len(pstrOrgCode)

For i = 1 To pintStrLen

pstrTemp = "%" & Hex(Asc(Mid(pstrOrgCode, i, 1)))

If pstrTemp = "%D" Then
pstrTemp = "%0D"
ElseIf pstrTemp = "%A" Then
pstrTemp = "%0A"
End If

pstrEncoded = pstrEncoded & pstrTemp

Next
Text2.Text = pstrEncoded
0
Question by:hollstar
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 10
• 7
• 3
• +2

Author Comment

ID: 7118320
The other problem I have had is I am unable to use comment tags and things of this nature - Any ideas how I could fix that ?
0

LVL 46

Expert Comment

ID: 7118428
What is a comment tag?

====================================
ASCII-to-HEX example:

For i = 1 To pintStrLen

pstrTemp = Right("0" & Hex(Asc(Mid(pstrOrgCode, i, 2))),2)

pstrEncoded = pstrEncoded & pstrTemp

Next
0

Author Comment

ID: 7118441
Nope that did not work... The hex number when converted and the % is added, is 41. The ascii value is of course 65 because this is infact the letter A...

What I need to do is return the letter. And of course, letters as it would be many lines.

A comment tag looks like this :

//Comment goes here

Inside some JavaScript and as I understand, does not work when you make this... No big deal but... :) Just need to get the decode working. :)
0

LVL 46

Expert Comment

ID: 7118473
pstrTemp = Chr(Asc(Mid(pstrOrgCode, i, 1)))
0

LVL 46

Expert Comment

ID: 7118476
BTW...why are you prefixing with "%"?
0

LVL 16

Expert Comment

ID: 7118931
BTW...why are you prefixing with "%"?
Yeah, i want to know too.
0

LVL 38

Expert Comment

ID: 7118975
This looks like you are URL encoding the javascript.  Note that in URL encoding, you do not have to encode every character, but it looks like that is what you want to do.

You can decode your URL encoding like this:

Private Function fDecode(ByVal strEnc As String) As String

Dim lngPos  As Long

lngPos = InStr(1, strEnc, "%")
Do While lngPos > 0
strEnc = Replace(strEnc, Mid\$(strEnc, lngPos, 3), Chr(Val("&H" & Mid\$(strEnc, lngPos + 1, 2))))
lngPos = InStr(1, strEnc, "%")
Loop
fDecode = strEnc

End Function
0

Author Comment

ID: 7119424
I am not so much dealing with the URL here Paul but dealing with JavaScript code. My work is based on this Online example so maybe you want to rework both your decode, and my encode if you think I have done something wrong.

0

LVL 38

Accepted Solution

PaulHews earned 200 total points
ID: 7119443
Here you go.

Private Function fDecode(ByVal strEnc As String) As String
Dim i As Long
Dim lngPos  As Long
Dim strHex As String
For i = 1 To Len(strEnc) - 2 Step 3
strHex = Mid\$(strEnc, i + 1, 2)
fDecode = fDecode & Chr(Val("&H" & strHex))
Next i

End Function

Public Function fEncode(ByVal strText As String) As String
Dim byt() As Byte
Dim i As Integer

'Drop into byte array
byt = StrConv(strText, vbFromUnicode)

For i = 0 To UBound(byt)
If byt(i) < 16 Then
fEncode = fEncode & "%0" & Hex(byt(i))
Else
fEncode = fEncode & "%" & Hex(byt(i))
End If
Next
End Function
0

Author Comment

ID: 7119465
Just out of interest, and if you did know how to do it I would be more than happy to juice up the points, but could you do this for HTML as well ?

I have seen it done somewhere and I think I can hunt down the web code for it...

Will keep you posted...
0

Author Comment

ID: 7119657
Hey Paul - I am a little interested to hear what you mean by why am I encoding everything ? Do you know of another way ?

I found the web version for HTML on one of my CD's... I noticed this has some encoded JavaScript, which uses the method I think you speak of... Maybe we should also look into this method as well... I think mine has to be all on one line... With the one seen here, I think it can go over more than one...

How about I adjust the points to 100 or so ? Sound Fair ?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0048)http://designerwiz.com/generator/encryptHTML.htm -->
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<META
content="HTML encrypt source code file protection to hide all your source codes from viewers visiting your web page FREE!"
name=DESCRIPTION>
<META
content="encrypt HTML generator protection hide source code command document files code protection"
name=KEYWORDS>
<SCRIPT>
<!--
//-->
</SCRIPT>

background="Encrypt HTML Source Code Generator_files/bg_bluetile.gif"
<DIV align=center>
<CENTER>

<FORM method=post>

<P align=center><FONT color=#ffff00 face="ms sans serif, arial" size=2>Insert
Current HTML Document Codes Here</FONT><B> <FONT color=#ffffff
face="ms sans serif, arial" size=-1>
<CENTER><TEXTAREA class=hexfield cols=70 name=txtPlain rows=9 VALUE=""></TEXTAREA></CENTER></FONT></B>
<P>
<CENTER><INPUT class=hexfield1 onclick=SetNewWords(this.form) type=button value="Click One Time To Encrypt HTML Source Code"></CENTER>
<P>
<CENTER><TEXTAREA class=hexfield cols=70 name=txtCripted rows=9 VALUE=""></TEXTAREA></CENTER></FORM></BODY></HTML>
0

LVL 38

Expert Comment

ID: 7120302
>Just out of interest, and if you did know how to do it I would be more than happy to juice up the points, but could you do this for HTML as well ?

What do you mean "for HTML?"  If you mean in script, is it server side or client side?

Here's a proper URL encode/decode function pair

Public Function fURLEncode(strtoencode As String) As String
Dim strTemp As String
Dim lngLen As Long
Dim intASC As Integer
Dim i As Long

lngLen = Len(strtoencode)

For i = 1 To lngLen
intASC = Asc(Mid\$(strtoencode, i, 1))
'Is the character in the list of valid chars?  Use alphanumerics only to be very very safe.
If InStr(1, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", Chr(intASC)) Then  '\$-_.!*'(),
strTemp = strTemp & Chr(intASC)
Else
If intASC < 16 Then
strTemp = strTemp & "%0" & Hex(intASC)
Else
strTemp = strTemp & "%" & Hex(intASC)
End If
End If
Next i
fURLEncode = strTemp

End Function

Private Function fDecode(ByVal strEnc As String) As String

Dim lngPos  As Long

lngPos = InStr(1, strEnc, "%")
Do While lngPos > 0
strEnc = Replace(strEnc, Mid\$(strEnc, lngPos, 3), Chr(Val("&H" & Mid\$(strEnc, lngPos + 1, 2))))
lngPos = InStr(1, strEnc, "%")
Loop
fDecode = strEnc

End Function
0

Author Comment

ID: 7120537
I think the HTML version of this uses the two new functions you have made. The idea is you drop your HTML source code in, it converts it. The new code is then just placed into the HTML file and loaded as per normal!

:)
0

LVL 38

Expert Comment

ID: 7120711
So at this point, what more are you looking for?
0

Author Comment

ID: 7121387
Well I am just interested on how the HTML version could be converted to VB... Just by looking at it quickly, the code you did basically supports that now... Correct ?
0

LVL 38

Expert Comment

ID: 7122106
They aren't quite compatible... their version has some extra encoding, although it just seems to prepend %25 to the hex instead of %.  Why do you need this?
0

LVL 4

Expert Comment

ID: 7124213
so simple:

Dim vArray          As Variant
Dim strDecoded      As String
Dim I               As Long

Text1 = vbNullString
If Text2 = vbNullString Then Exit Sub

vArray = Split(Text2, "%")
For I = 1 To UBound(vArray) 'vArray(0) was skiped
strDecoded = strDecoded & Chr("&H" & vArray(I))
Next

Text1 = strDecoded
0

Author Comment

ID: 7128616
Paul - Both for interest sake and being able to encode, JS, and HTML... Thats the only reason...
0

LVL 38

Expert Comment

ID: 7128951
Your post of 06/30/2002 12:30AM PST is double encoded.  To decode that text, just decode it twice using the decode function I provided.  I copied and pasted the entire encoded string into a file and did this:

Dim strTextEnc As String
Dim strText As String

strTextEnc = fGetFileContents("C:\Temp\t.txt")
strText = fDecode(strTextEnc)
strText = fDecode(strText)
Call sPutStringToFile(strText, "C:\Temp\t2.txt")

The text in t2.txt is as follows:

<script>
<!--
document.write(unescape("<SCRIPT LANGUAGE="JavaScript">
<!--
var txtCrip;
var txtFinal;
var crlf = unescape("
")
var Sl = unescape("/")

function SetNewWords(form)
{

txtCrip = escape(form.txtPlain.value);
txtFinal = "<HTML>"
txtFinal = txtFinal + crlf + "<HEAD>"
txtFinal = txtFinal + crlf + '<SCRIPT LANGUAGE="Javascript">\n<!-- HTML Encryption Code Provided Free By http://www.DesignerWiz.com -->\n<!-- Your Free Complete Web Development Resource & JavaScript Public Archive Center -->'
txtFinal = txtFinal + crlf + "<!--"
txtFinal = txtFinal + crlf + 'var Words ="' + txtCrip +  '"'
txtFinal = txtFinal + crlf + "function SetNewWords()"
txtFinal = txtFinal + crlf + "{"
txtFinal = txtFinal + crlf + "     var NewWords;"
txtFinal = txtFinal + crlf + "     NewWords = unescape(Words);"
txtFinal = txtFinal + crlf + "     document.write(NewWords);"
txtFinal = txtFinal + crlf + "} "
txtFinal = txtFinal + crlf + "SetNewWords();"
txtFinal = txtFinal + crlf + "// -->"
txtFinal = txtFinal + crlf + "<" + Sl + "SCRIPT>"
txtFinal = txtFinal + crlf + "<" + Sl + "HEAD>"
txtFinal = txtFinal + crlf + "<BODY>"
txtFinal = txtFinal + crlf + "<" + Sl + "BODY>"
txtFinal = txtFinal + crlf + "<" + Sl + "HTML>"
form.txtCripted.value = txtFinal;
}
// -->
</SCRIPT>"));
//-->
</script>
0

Author Comment

ID: 7154164
Sorry Buddy - Been super busy and I know how you guys like to close these things ASAP before us clowns forget about them... ;-)

Anyway, yeah this is top stuff... Works really well... Perfect basically !
0

LVL 38

Expert Comment

ID: 7154197
What happened to this?

>>>How about I adjust the points to 100 or so ? Sound Fair ?<<<

I did take the time to figure out the double encoding on that.
0

Author Comment

ID: 7155622
Sorry Paul I forgot I even said that... :S Will make antoher question...
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate rowâ€¦
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we aâ€¦
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process fromâ€¦
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll