Solved

Hex To ASCII

Posted on 2002-06-29
22
1,029 Views
Last Modified: 2012-08-13
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
Comment
Question by:hollstar
  • 10
  • 7
  • 3
  • +2
22 Comments
 

Author Comment

by:hollstar
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 45

Expert Comment

by:aikimark
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

by:hollstar
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 45

Expert Comment

by:aikimark
ID: 7118473
pstrTemp = Chr(Asc(Mid(pstrOrgCode, i, 1)))
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7118476
BTW...why are you prefixing with "%"?
0
 
LVL 16

Expert Comment

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

Expert Comment

by:PaulHews
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

by:hollstar
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.

http://www.paradigm.nu/ascii2hex/ascii2hex.htm
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 50 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

by:hollstar
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

by:hollstar
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 -->
<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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 38

Expert Comment

by:PaulHews
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

by:hollstar
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

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

Author Comment

by:hollstar
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

by:PaulHews
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

by:TigerZhao
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

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

Expert Comment

by:PaulHews
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

by:hollstar
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

by:PaulHews
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

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now