Translation of 3 characters into hex with an interval of 5 characters

New to FreeBASIC? Post your questions here.
Post Reply
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

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!
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by D.J.Peters »

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
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

D.J.Peters wrote: Aug 19, 2022 18:43

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
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 spaces
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

D.J.Peters wrote: Aug 19, 2022 18:43

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
36 8635 6c6C o,20 w666 7236 cd21 m79 2036 e
631 me20 is32 0634 6176 id

>

hello, world! my name is david
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by fxm »

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.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

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
Thanks!! :)
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

D.J.Peters wrote: Aug 19, 2022 18:43

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
In any case, thank you for your response, maybe you misunderstood me, fxm apparently understood)))
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

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
One request, redo using these functions so that I understand how it works when decrypting

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
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by fxm »

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
For compatibility with an ASCII value < &h10, modify the line 5 as follows:

Code: Select all

sr &= Lcase(Hex(s[I], 2))
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by fxm »

gerry wrote: Aug 19, 2022 19:35 One request, redo using these functions so that I understand how it works when decrypting

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
I don't understand your request.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

fxm wrote: Aug 19, 2022 19:50
gerry wrote: Aug 19, 2022 19:35 One request, redo using these functions so that I understand how it works when decrypting

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
I don't understand your request.
I understand everything, I'm sorry, no more help is needed thank you :)
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by fxm »

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
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Translation of 3 characters into hex with an interval of 5 characters

Post by gerry »

fxm 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
Thanks :)
Post Reply