Translation of 3 characters into hex with an interval of 5 characters
Translation of 3 characters into hex with an interval of 5 characters
Hi! I have the line
"hello, world! my name is david"
I need every 3 letters to be converted to hex with a space of 5 letters and returned to their original state
Example:
"68656clo, w6f726cd! my206e61me is206461vid"
But the string can be any length it's just for example
Thanks!
"hello, world! my name is david"
I need every 3 letters to be converted to hex with a space of 5 letters and returned to their original state
Example:
"68656clo, w6f726cd! my206e61me is206461vid"
But the string can be any length it's just for example
Thanks!
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Translation of 3 characters into hex with an interval of 5 characters
Code: Select all
function string2hex5space(sInput as string) as string
var ret=""
for i as integer=0 to len(sInput)-1
if (i mod 3)=0 then
ret &= hex(sInput[i],2) & space(5)
' ret &= space(5) & hex(sInput[i],2)
else
ret &= chr(sInput[i])
endif
next
return ret
end function
var result = string2hex5space("68656clo, w6f726cd! my206e61me is206461vid")
print result
sleep
Re: Translation of 3 characters into hex with an interval of 5 characters
That's right, but we need to return the string to its original state, that is, translate it from hex to the original string and remove spacesD.J.Peters wrote: ↑Aug 19, 2022 18:43Code: Select all
function string2hex5space(sInput as string) as string var ret="" for i as integer=0 to len(sInput)-1 if (i mod 3)=0 then ret &= hex(sInput[i],2) & space(5) ' ret &= space(5) & hex(sInput[i],2) else ret &= chr(sInput[i]) endif next return ret end function var result = string2hex5space("68656clo, w6f726cd! my206e61me is206461vid") print result sleep
Re: Translation of 3 characters into hex with an interval of 5 characters
36 8635 6c6C o,20 w666 7236 cd21 m79 2036 eD.J.Peters wrote: ↑Aug 19, 2022 18:43Code: Select all
function string2hex5space(sInput as string) as string var ret="" for i as integer=0 to len(sInput)-1 if (i mod 3)=0 then ret &= hex(sInput[i],2) & space(5) ' ret &= space(5) & hex(sInput[i],2) else ret &= chr(sInput[i]) endif next return ret end function var result = string2hex5space("68656clo, w6f726cd! my206e61me is206461vid") print result sleep
631 me20 is32 0634 6176 id
>
hello, world! my name is david
Re: Translation of 3 characters into hex with an interval of 5 characters
For example:
Code: Select all
Function encode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String
Dim As String sr
For I As Integer = 0 To Len(s) - 1
If I Mod (n1 + n2) < n1 Then
sr &= Lcase(Hex(s[I], 2))
Else
sr &= Chr(s[I])
End If
Next I
Return sr
End Function
Function decode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String
Dim As String sr
For I As Integer = 0 To Len(s) - 1
If I Mod (2 * n1 + n2) < 2 * n1 Then
sr &= Chr(Val("&h" & Chr(s[I]) & Chr(s[I + 1])))
I += 1
Else
sr &= Chr(s[I])
End If
Next I
Return sr
End Function
Dim As String s0
s0 = "hello, world! my name is david"
Print s0
Dim As String s1
s1 = encode(s0, 3, 5)
Print s1
Dim As String s2
s2 = decode(s1, 3, 5)
Print s2
Sleep
Last edited by fxm on Aug 20, 2022 6:48, edited 1 time in total.
Reason: Updated as per my following post.
Reason: Updated as per my following post.
Re: Translation of 3 characters into hex with an interval of 5 characters
Thanks!!fxm wrote: ↑Aug 19, 2022 19:14 For example:Code: Select all
Function encode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String Dim As String sr For I As Integer = 0 To Len(s) - 1 If I Mod (n1 + n2) < n1 Then sr &= Lcase(Hex(s[I])) Else sr &= Chr(s[I]) End If Next I Return sr End Function Function decode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String Dim As String sr For I As Integer = 0 To Len(s) - 1 If I Mod (2 * n1 + n2) < 2 * n1 Then sr &= Chr(Val("&h" & Chr(s[I]) & Chr(s[I + 1]))) I += 1 Else sr &= Chr(s[I]) End If Next I Return sr End Function Dim As String s0 s0 = "hello, world! my name is david" Print s0 Dim As String s1 s1 = encode(s0, 3, 5) Print s1 Dim As String s2 s2 = decode(s1, 3, 5) Print s2 Sleep
Re: Translation of 3 characters into hex with an interval of 5 characters
In any case, thank you for your response, maybe you misunderstood me, fxm apparently understood)))D.J.Peters wrote: ↑Aug 19, 2022 18:43Code: Select all
function string2hex5space(sInput as string) as string var ret="" for i as integer=0 to len(sInput)-1 if (i mod 3)=0 then ret &= hex(sInput[i],2) & space(5) ' ret &= space(5) & hex(sInput[i],2) else ret &= chr(sInput[i]) endif next return ret end function var result = string2hex5space("68656clo, w6f726cd! my206e61me is206461vid") print result sleep
Re: Translation of 3 characters into hex with an interval of 5 characters
One request, redo using these functions so that I understand how it works when decryptingfxm wrote: ↑Aug 19, 2022 19:14 For example:Code: Select all
Function encode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String Dim As String sr For I As Integer = 0 To Len(s) - 1 If I Mod (n1 + n2) < n1 Then sr &= Lcase(Hex(s[I])) Else sr &= Chr(s[I]) End If Next I Return sr End Function Function decode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String Dim As String sr For I As Integer = 0 To Len(s) - 1 If I Mod (2 * n1 + n2) < 2 * n1 Then sr &= Chr(Val("&h" & Chr(s[I]) & Chr(s[I + 1]))) I += 1 Else sr &= Chr(s[I]) End If Next I Return sr End Function Dim As String s0 s0 = "hello, world! my name is david" Print s0 Dim As String s1 s1 = encode(s0, 3, 5) Print s1 Dim As String s2 s2 = decode(s1, 3, 5) Print s2 Sleep
Code: Select all
Function StrToHex (Byref convstr As String) As String
Dim as string c
c = convstr
Dim As Uinteger i
Dim As String ftext
For i = 1 To Len(c)
ftext += Hex(Asc(Mid$(c,i,1)),2)
Next i
Return ftext
End Function
Function HexToStr (Byref convstr As String) As String
Dim as string c
c = convstr
If Len(c) Mod 2 = 1 Then c += "0"
Dim As Uinteger i
Dim As String f
For i = 1 To Len(c) Step 2
f += Chr$(Val("&H"+Mid$(c,i,2)))
Next i
Return f
End Function
Re: Translation of 3 characters into hex with an interval of 5 characters
For compatibility with an ASCII value < &h10, modify the line 5 as follows:fxm wrote: ↑Aug 19, 2022 19:14 For example:Code: Select all
Function encode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String Dim As String sr For I As Integer = 0 To Len(s) - 1 If I Mod (n1 + n2) < n1 Then sr &= Lcase(Hex(s[I])) Else sr &= Chr(s[I]) End If Next I Return sr End Function Function decode(Byref s As String, Byval n1 As Integer, Byval n2 As Integer) As String Dim As String sr For I As Integer = 0 To Len(s) - 1 If I Mod (2 * n1 + n2) < 2 * n1 Then sr &= Chr(Val("&h" & Chr(s[I]) & Chr(s[I + 1]))) I += 1 Else sr &= Chr(s[I]) End If Next I Return sr End Function Dim As String s0 s0 = "hello, world! my name is david" Print s0 Dim As String s1 s1 = encode(s0, 3, 5) Print s1 Dim As String s2 s2 = decode(s1, 3, 5) Print s2 Sleep
Code: Select all
sr &= Lcase(Hex(s[I], 2))
Re: Translation of 3 characters into hex with an interval of 5 characters
I don't understand your request.gerry wrote: ↑Aug 19, 2022 19:35 One request, redo using these functions so that I understand how it works when decryptingCode: Select all
Function StrToHex (Byref convstr As String) As String Dim as string c c = convstr Dim As Uinteger i Dim As String ftext For i = 1 To Len(c) ftext += Hex(Asc(Mid$(c,i,1)),2) Next i Return ftext End Function Function HexToStr (Byref convstr As String) As String Dim as string c c = convstr If Len(c) Mod 2 = 1 Then c += "0" Dim As Uinteger i Dim As String f For i = 1 To Len(c) Step 2 f += Chr$(Val("&H"+Mid$(c,i,2))) Next i Return f End Function
Re: Translation of 3 characters into hex with an interval of 5 characters
I understand everything, I'm sorry, no more help is needed thank youfxm wrote: ↑Aug 19, 2022 19:50I don't understand your request.gerry wrote: ↑Aug 19, 2022 19:35 One request, redo using these functions so that I understand how it works when decryptingCode: Select all
Function StrToHex (Byref convstr As String) As String Dim as string c c = convstr Dim As Uinteger i Dim As String ftext For i = 1 To Len(c) ftext += Hex(Asc(Mid$(c,i,1)),2) Next i Return ftext End Function Function HexToStr (Byref convstr As String) As String Dim as string c c = convstr If Len(c) Mod 2 = 1 Then c += "0" Dim As Uinteger i Dim As String f For i = 1 To Len(c) Step 2 f += Chr$(Val("&H"+Mid$(c,i,2))) Next i Return f End Function
Re: Translation of 3 characters into hex with an interval of 5 characters
My own version:
Code: Select all
Function StrToHex (Byref convstr As String) As String
Dim As String ftext
For I As Integer = 0 To Len(convstr) - 1
ftext &= Hex(convstr[I], 2)
Next I
Return ftext
End Function
Function HexToStr (Byref convstr As String) As String
Dim As String f
For I As Integer = 0 To Len(convstr) - 1 Step 2
f &= Chr(Val("&H" & Chr(convstr[I]) & Chr(convstr[I + 1])))
Next I
Return f
End Function
Dim As String s0
s0 = "hello, world! my name is david"
Print s0
Dim As String s1
s1 = StrToHex(s0)
Print s1
Dim As String s2
s2 = HexToStr(s1)
Print s2
Sleep
Re: Translation of 3 characters into hex with an interval of 5 characters
Thanksfxm wrote: ↑Aug 19, 2022 20:04 My own version:Code: Select all
Function StrToHex (Byref convstr As String) As String Dim As String ftext For I As Integer = 0 To Len(convstr) - 1 ftext &= Hex(convstr[I], 2) Next I Return ftext End Function Function HexToStr (Byref convstr As String) As String Dim As String f For I As Integer = 0 To Len(convstr) - 1 Step 2 f &= Chr(Val("&H" & Chr(convstr[I]) & Chr(convstr[I + 1]))) Next I Return f End Function Dim As String s0 s0 = "hello, world! my name is david" Print s0 Dim As String s1 s1 = StrToHex(s0) Print s1 Dim As String s2 s2 = HexToStr(s1) Print s2 Sleep