JDB
JDB

Reputation: 19

reverse engineer excel formula

I have an excel vba formula that I am trying to reverse the formula , the Sub letterToHex works great. But I cant work out how to get the final stages of working it in reverse , to get the Hex code converted back to CPJ. I have managed to get the first few stages working in Sub HextoLetter

Sub LetterToHex()

R1 = "CPJ"

N1 = Asc(Right(R1, 1)) - 64 ' Result is ASCII value of letter J is  (74) - 64  = 10

N1 = N1 + (Asc(Mid(R1, 2, 1)) - 64) * 64 ' Result is ASCII value of letter P is (80) - 64 * 64 = 1234 + N1 value 10 = 1034

n11 = (Asc(Left(R1, 1)) - 64) * 64 ' Result is ASCII value of letter C (67) - 64 =3 * 64 = 192

N1 = N1 + n11 * 64 ' result is 13322

N1 = N1 + 14680064 ' Result is 14693386

N2 = Hex(N1) 'Result is N2 = E0340A
dec = CLng("&H" & N2)

notfound:

End Sub

Sub HextoLetter()
hexcode = "E0340A"
dec = CLng("&H" & hexcode) '14693386
shortdec = dec - 14680064 '13322

End Sub

I need to get the HextoLetter code completed to get the hexcode E0340A converted back to CPJ

additional code showing for

Sub LetterToHex_CPK()

R1 = "CPK"

N1 = Asc(Right(R1, 1)) - 64 ' Result is ASCII value of letter K is  (75) - 64  = 11
MsgBox N1
N1 = N1 + (Asc(Mid(R1, 2, 1)) - 64) * 64 ' Result is ASCII value of letter P is (80) - 64 * 64 = 1234 + N1 value 11 = 1035
MsgBox N1
n11 = (Asc(Left(R1, 1)) - 64) * 64 ' Result is ASCII value of letter C (67) - 64 =3 * 64 = 192
MsgBox n11
N1 = N1 + n11 * 64 ' result is 13323
MsgBox N1
N1 = N1 + 14680064 ' Result is 14693387
MsgBox N1
N2 = Hex(N1) 'Result is N2 = E0340B
MsgBox N2
dec = CLng("&H" & N2)

notfound:

End Sub

Upvotes: 2

Views: 414

Answers (2)

Siddharth Rout
Siddharth Rout

Reputation: 149325

N1 + n11 * 64

The above can be written as

 N1 = N1 + (n11 * 64)

which is the same as

 N1 + ((Asc(Left(R1, 1)) - 64) * 64)

which is the same as

 (N1 + (Asc(Mid(R1, 2, 1)) - 64) * 64) + ((Asc(Left(R1, 1)) - 64) * 64)

which is the same as

 (Asc(Right(R1, 1)) - 64 + (Asc(Mid(R1, 2, 1)) - 64) * 64) + ((Asc(Left(R1, 1)) - 64) * 64)

Building on this logic it is possible to get what you want

Here is a function for it

Sub Sample()
    Dim shortdec As Long, dec As Long
    Dim hexcode As String

    hexcode = "E0340A" 
    'hexcode = "E0340B" '<~~ This will give you CPK

    dec = CLng("&H" & hexcode) '14693386
    shortdec = dec - 14680064 '1332

    Debug.Print GetString(shortdec)
End Sub

 Function GetString(n As Long) As String
    Dim i As Long, j As Long, k As Long
    Dim a As Long, b As Long, c As Long
    Dim x As Long

    For i = 65 To 90
        For j = 65 To 90
            For k = 65 To 90
                a = Asc(Chr(i)) - 64
                b = (Asc(Chr(j)) - 64) * 64
                c = (Asc(Chr(k)) - 64) * 64

                x = (a + b) + c * 64
                If x = n Then
                    GetString = Chr(k) & Chr(j) & Chr(i)
                    Exit Function
                End If
            Next k
        Next j
    Next i
 End Function

My assumptions: You will use Capital letters. Else you will have to loop through ASC 97 to 122 as well.

Upvotes: 2

Pᴇʜ
Pᴇʜ

Reputation: 57743

You have a sum in your first procedure

N1 = N1 + n11 * 64

therefore the formula cannot be reverted!

Explanation:

Imagine you have the sum 5 + 4 = 10. Then you cannot revert it if you only know the sum 10 and you want to know a + b = 10 then there is more than one solution for a and b (actually there are infinite solutions for that).

Therefore the answer is: Your Hex code cannot be reverted into the 3 letters.

Upvotes: 2

Related Questions