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
hollstarAsked:
Who is Participating?
 
PaulHewsConnect With a Mentor 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
 
hollstarAuthor 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
 
aikimarkCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
hollstarAuthor 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
 
aikimarkCommented:
pstrTemp = Chr(Asc(Mid(pstrOrgCode, i, 1)))
0
 
aikimarkCommented:
BTW...why are you prefixing with "%"?
0
 
Richie_SimonettiIT OperationsCommented:
BTW...why are you prefixing with "%"?
Yeah, i want to know too.
0
 
PaulHewsCommented:
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
 
hollstarAuthor 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.

http://www.paradigm.nu/ascii2hex/ascii2hex.htm
0
 
hollstarAuthor 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
 
hollstarAuthor 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 -->
<HTML><HEAD><TITLE>Encrypt HTML Source Code Generator</TITLE>
<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>
<!--
document.write(unescape("%3Cscript%3E%0D%0A%3C%21--%0D%0Adocument.write%28unescape%28%22%253CSCRIPT%2520LANGUAGE%253D%2522JavaScript%2522%253E%250D%250A%253C%2521--%250D%250Avar%2520txtCrip%253B%250D%250Avar%2520txtFinal%253B%250D%250Avar%2520crlf%2520%253D%2520unescape%2528%2522%25250D%25250A%2522%2529%250D%250Avar%2520Sl%2520%253D%2520unescape%2528%2522%25252F%2522%2529%250D%250A%250D%250Afunction%2520SetNewWords%2528form%2529%250D%250A%257B%250D%250A%250D%250A%2509txtCrip%2520%253D%2520escape%2528form.txtPlain.value%2529%253B%250D%250A%2509txtFinal%2520%253D%2520%2522%253CHTML%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253CHEAD%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2527%253CSCRIPT%2520LANGUAGE%253D%2522Javascript%2522%253E%255Cn%253C%2521--%2520HTML%2520Encryption%2520Code%2520Provided%2520Free%2520By%2520http%253A//www.DesignerWiz.com%2520--%253E%255Cn%253C%2521--%2520Your%2520Free%2520Complete%2520Web%2520Development%2520Resource%2520%2526%2520JavaScript%2520Public%2520Archive%2520Center%2520--%253E%2527%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253C%2521--%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2527var%2520Words%2520%253D%2522%2527%2520+%2520txtCrip%2520+%2520%2520%2527%2522%2527%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522function%2520SetNewWords%2528%2529%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%257B%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%2520%2520%2520%2520%2520var%2520NewWords%253B%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%2520%2520%2520%2520%2520NewWords%2520%253D%2520unescape%2528Words%2529%253B%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%2520%2520%2520%2520%2520document.write%2528NewWords%2529%253B%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%257D%2520%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522SetNewWords%2528%2529%253B%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522//%2520--%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253C%2522%2520+%2520Sl%2520+%2520%2522SCRIPT%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253C%2522%2520+%2520Sl%2520+%2520%2522HEAD%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253CBODY%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253C%2522%2520+%2520Sl%2520+%2520%2522BODY%253E%2522%250D%250A%2509txtFinal%2520%253D%2520txtFinal%2520+%2520crlf%2520+%2520%2522%253C%2522%2520+%2520Sl%2520+%2520%2522HTML%253E%2522%250D%250A%2520form.txtCripted.value%2520%253D%2520txtFinal%253B%250D%250A%257D%250D%250A//%2520--%253E%250D%250A%253C/SCRIPT%253E%22%29%29%3B%0D%0A//--%3E%0D%0A%3C/script%3E"));
//-->
</SCRIPT>

<META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
<BODY aLink=#ff0000
background="Encrypt HTML Source Code Generator_files/bg_bluetile.gif"
bgColor=#0060a0 link=#ffcc66 text=#ffffff vLink=#ffcc66>
<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
 
PaulHewsCommented:
>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
 
hollstarAuthor 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
 
PaulHewsCommented:
So at this point, what more are you looking for?
0
 
hollstarAuthor 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
 
PaulHewsCommented:
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
 
TigerZhaoCommented:
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
 
hollstarAuthor Commented:
Paul - Both for interest sake and being able to encode, JS, and HTML... Thats the only reason...
0
 
PaulHewsCommented:
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
 
hollstarAuthor 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
 
PaulHewsCommented:
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
 
hollstarAuthor 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.

All Courses

From novice to tech pro — start learning today.