# Hex To ASCII

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
###### Who is Participating?

Commented:
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 Commented:
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

Commented:
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 Commented:
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

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

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

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

Commented:
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 Commented:
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

Author Commented:
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 Commented:
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

Commented:
>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 Commented:
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

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

Author Commented:
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

Commented:
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

Commented:
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 Commented:
Paul - Both for interest sake and being able to encode, JS, and HTML... Thats the only reason...
0

Commented:
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 Commented:
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

Commented:
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 Commented:
Sorry Paul I forgot I even said that... :S Will make antoher question...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.