# B4J QuestionHow convert from vba to B4j code?

#### amorosik

##### Expert
I need help to convert a function from vba to B4j code
How would you convert the following function?

B4X:
``````Function decrypt(ByVal key As String, ByVal src As String) As String
Dim KeyPos As Integer, KeyLen As Integer, SrcAsc As Integer, dest As String, offset As Integer, TmpSrcAsc, SrcPos
KeyLen = Len(key)
offset = Val("&H" + Left\$(src, 2))
For SrcPos = 3 To Len(src) Step 2
SrcAsc = Val("&H" + Trim(Mid\$(src, SrcPos, 2)))
If KeyPos < KeyLen Then KeyPos = KeyPos + 1 Else KeyPos = 1
TmpSrcAsc = SrcAsc Xor Asc(Mid\$(key, KeyPos, 1))
If TmpSrcAsc <= offset Then
TmpSrcAsc = 255 + TmpSrcAsc - offset
Else
TmpSrcAsc = TmpSrcAsc - offset
End If
dest = dest + Chr(TmpSrcAsc)
offset = SrcAsc
Next

decrypt = dest
End Function``````

#### amorosik

##### Expert
decrypt("chiave123","0555EF4A1F223D454FBB88A18AAC86AEC5") => 1234567890ABCDEF
decrypt("chiave123","9CAE88D266ED4149B3DF73DC76D86BD52D") => 1234567890ABCDEF
decrypt("chiave123","91A1BB87DA66F9 0 A70C36DC66BD97AF1") => 1234567890ABCDEF

#### Erel

##### B4X founder
Staff member
Longtime User
B4X:
``````Sub decrypt(key As String, src As String) As String
Dim KeyPos As Int = -1, KeyLen As Int, SrcAsc As Int, dest As String, offset As Int, TmpSrcAsc As Int, SrcPos As Int
KeyLen = key.Length
offset = Bit.ParseInt(src.SubString2(0, 2), 16)
For SrcPos = 2 To src.Length - 1 Step 2
SrcAsc = Bit.ParseInt(src.SubString2(SrcPos, SrcPos + 2).Trim, 16)
If KeyPos < KeyLen - 1 Then KeyPos = KeyPos + 1 Else KeyPos = 0
TmpSrcAsc = Bit.And(0xff, Bit.Xor(SrcAsc, Asc(key.CharAt(KeyPos))))
If TmpSrcAsc <= offset Then
TmpSrcAsc = 255 + TmpSrcAsc - offset
Else
TmpSrcAsc = TmpSrcAsc - offset
End If
dest = dest & Chr(TmpSrcAsc)
offset = SrcAsc
Next
Return dest
End Sub``````

#### amykonio

##### Active Member
Longtime User
@Erel: Faster than GPT, and of course more effective... As always...

#### amykonio

##### Active Member
Longtime User
I was going to use chatgpt but as always we have our own
I wrote my version which look similar to Erel's, but I was missing a trim so the third text was throwing an error...
I found my mistake when I saw Erel's version.
Andreas.

Replies
0
Views
578
Replies
9
Views
917
Replies
3
Views
776
Replies
6
Views
2K
B4J Question convert pid4j into b4j
Replies
6
Views
1K