convert (text+numbers) to Hexcode then to base64 (1 Viewer)

mhakim

Member
Local time
Today, 22:01
Joined
Jan 25, 2021
Messages
72
hi dears i have a value in text-1 like this

Text -1
0113Alarabi Group021120003548231032212/27/2020 10:47:08 AM0446900053900

question is i need to convert text-1 value to text-2 then text-3

text-2
convert text-1 to Hex in text-2

text-3
convert text-2 to Base64 in text-3
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:01
Joined
May 7, 2009
Messages
19,245
these are the function (put in Module):
Code:
' arnelgp
Function StringToHex(inputString As String) As String
    Dim bytes() As Byte
    Dim i As Long
    Dim hexString As String

    ' Convert string to bytes
    bytes = StrConv(inputString, vbFromUnicode)

    ' Convert bytes to hexadecimal representation
    For i = LBound(bytes) To UBound(bytes)
        hexString = hexString & Right("0" & Hex(bytes(i)), 2)
    Next i

    StringToHex = hexString
End Function

Function HexToString(hexString As String) As String
    Dim i As Long
    Dim outputString As String

    ' Convert each pair of hexadecimal characters to bytes
    For i = 1 To Len(hexString) Step 2
        outputString = outputString & ChrW("&H" & Mid(hexString, i, 2))
    Next i

    HexToString = outputString
End Function


Public Function EncodeToBase64(ByVal inputString As String) As String
    Dim arrData() As Byte
    arrData = StrConv(inputString, vbFromUnicode)
   
    Dim objXML As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
   
    Dim objNode As Object
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
   
    EncodeToBase64 = Replace(objNode.text, vbLf, "")
   
    Set objNode = Nothing
    Set objXML = Nothing
End Function


Public Function DecodeFromBase64(ByVal base64String As String) As String
    Dim objXML As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
   
    Dim objNode As Object
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.text = base64String
   
    Dim byteArray() As Byte
    byteArray = objNode.nodeTypedValue
   
    DecodeFromBase64 = StrConv(byteArray, vbUnicode)
   
    Set objNode = Nothing
    Set objXML = Nothing
End Function

for testing:
Code:
Sub TestConversion()
    Dim myString As String
    Dim hexString As String
    Dim convertedString As String
    Dim base64 As String
   
    myString = "Hello, World!"
    hexString = StringToHex(myString)
    Debug.Print "Hexadecimal representation: " & hexString

    convertedString = HexToString(hexString)
    Debug.Print "Converted back to string: " & convertedString
   
    base64 = EncodeToBase64(hexString)
    Debug.Print "Converted Hex to base64: " & base64
   
    Debug.Print "Convert back from base64 to Hex: " & DecodeFromBase64(base64)
End Sub


result:


Code:
Hexadecimal representation: 48656C6C6F2C20576F726C6421
Converted back to string: Hello, World!
Converted Hex to base64: NDg2NTZDNkM2RjJDMjA1NzZGNzI2QzY0MjE=
Convert back from base64 to Hex: 48656C6C6F2C20576F726C6421
 

mhakim

Member
Local time
Today, 22:01
Joined
Jan 25, 2021
Messages
72
these are the function (put in Module):
Code:
' arnelgp
Function StringToHex(inputString As String) As String
    Dim bytes() As Byte
    Dim i As Long
    Dim hexString As String

    ' Convert string to bytes
    bytes = StrConv(inputString, vbFromUnicode)

    ' Convert bytes to hexadecimal representation
    For i = LBound(bytes) To UBound(bytes)
        hexString = hexString & Right("0" & Hex(bytes(i)), 2)
    Next i

    StringToHex = hexString
End Function

Function HexToString(hexString As String) As String
    Dim i As Long
    Dim outputString As String

    ' Convert each pair of hexadecimal characters to bytes
    For i = 1 To Len(hexString) Step 2
        outputString = outputString & ChrW("&H" & Mid(hexString, i, 2))
    Next i

    HexToString = outputString
End Function


Public Function EncodeToBase64(ByVal inputString As String) As String
    Dim arrData() As Byte
    arrData = StrConv(inputString, vbFromUnicode)
  
    Dim objXML As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
  
    Dim objNode As Object
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
  
    EncodeToBase64 = Replace(objNode.text, vbLf, "")
  
    Set objNode = Nothing
    Set objXML = Nothing
End Function


Public Function DecodeFromBase64(ByVal base64String As String) As String
    Dim objXML As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
  
    Dim objNode As Object
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.text = base64String
  
    Dim byteArray() As Byte
    byteArray = objNode.nodeTypedValue
  
    DecodeFromBase64 = StrConv(byteArray, vbUnicode)
  
    Set objNode = Nothing
    Set objXML = Nothing
End Function

for testing:
Code:
Sub TestConversion()
    Dim myString As String
    Dim hexString As String
    Dim convertedString As String
    Dim base64 As String
  
    myString = "Hello, World!"
    hexString = StringToHex(myString)
    Debug.Print "Hexadecimal representation: " & hexString

    convertedString = HexToString(hexString)
    Debug.Print "Converted back to string: " & convertedString
  
    base64 = EncodeToBase64(hexString)
    Debug.Print "Converted Hex to base64: " & base64
  
    Debug.Print "Convert back from base64 to Hex: " & DecodeFromBase64(base64)
End Sub


result:


Code:
Hexadecimal representation: 48656C6C6F2C20576F726C6421
Converted back to string: Hello, World!
Converted Hex to base64: NDg2NTZDNkM2RjJDMjA1NzZGNzI2QzY0MjE=
Convert back from base64 to Hex: 48656C6C6F2C20576F726C6421
you are the best thank you
 

mhakim

Member
Local time
Today, 22:01
Joined
Jan 25, 2021
Messages
72
hi friend
i would like to give you big thanks for the code
1- first step is great from text to hex very good i checked it online from the web
2-second step convert hex to base 64 i think we miss something

i tried to convert online from web i find the converting from hex to base 64 not same result
for example hex value in our example
= 48656C6C6F2C20576F726C6421

will give base 64 = SGVsbG8sIFdvcmxkIQ==

so what is your recomendation to edit the code

but the code is great thank you
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:01
Joined
May 7, 2009
Messages
19,245
oh..
here try another one:
Code:
' arnelgp
Function StringToHex(inputString As String) As String
    Dim bytes() As Byte
    Dim i As Long
    Dim hexString As String

    ' Convert string to bytes
    bytes = StrConv(inputString, vbFromUnicode)

    ' Convert bytes to hexadecimal representation
    For i = LBound(bytes) To UBound(bytes)
        hexString = hexString & Right("0" & Hex(bytes(i)), 2)
    Next i

    StringToHex = hexString
End Function

Function HexToString(hexString As String) As String
    Dim i As Long
    Dim outputString As String

    ' Convert each pair of hexadecimal characters to bytes
    For i = 1 To Len(hexString) Step 2
        outputString = outputString & ChrW("&H" & Mid(hexString, i, 2))
    Next i

    HexToString = outputString
End Function


Function Hex2Base64(ByVal strHex As String) As String
    Dim arrBytes
    If Len(strHex) Mod 2 <> 0 Then
        strHex = Left(strHex, Len(strHex) - 1) & "0" & Right(strHex, 1)
    End If
    With CreateObject("Microsoft.XMLDOM").createElement("objNode")
        .DataType = "bin.hex"
        .text = strHex
        arrBytes = .nodeTypedValue
    End With
    With CreateObject("Microsoft.XMLDOM").createElement("objNode")
        .DataType = "bin.base64"
        .nodeTypedValue = arrBytes
        Hex2Base64 = .text
    End With
End Function

Function Base64ToHex(ByVal sBase64 As String) As String
    Dim arrBytes() As Byte
    With CreateObject("MSXML2.DOMDocument").createElement("objNode")
        .DataType = "bin.base64"
        .text = sBase64
        arrBytes = .nodeTypedValue
    End With
    With CreateObject("MSXML2.DOMDocument").createElement("objNode")
        .DataType = "bin.hex"
        .nodeTypedValue = arrBytes
        Base64ToHex = UCase(.text)
    End With
End Function

Sub TestConversion()
    Dim myString As String
    Dim hexString As String
    Dim convertedString As String
    Dim base64 As String
    
    myString = "Hello, World!"
    hexString = StringToHex(myString)
    Debug.Print "Hexadecimal representation: " & hexString

    convertedString = HexToString(hexString)
    Debug.Print "Converted back to string: " & convertedString
    
    base64 = Hex2Base64(hexString)
    Debug.Print "Converted Hex to base64: " & base64
    
    Debug.Print "Convert back from base64 to Hex: " & Base64ToHex(base64)
End Sub

result:

Code:
Hexadecimal representation: 48656C6C6F2C20576F726C6421
Converted back to string: Hello, World!
Converted Hex to base64: SGVsbG8sIFdvcmxkIQ==
Convert back from base64 to Hex: 48656C6C6F2C20576F726C6421
 

Users who are viewing this thread

Top Bottom