I'm getting unexpected results using vba to encode a string in base64. For strings shorter than 55 characters - things work fine. But for strings longer than 55 characters (and for each 55 characters next) a newline character gets inserted in the encoded string.
Function EncodeBase64(text As String) As String
Dim arrData() As Byte
arrData = StrConv(text, vbFromUnicode)
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.text
Set objNode = Nothing
Set objXML = Nothing
End Function
Sub testEncode64()
Dim ClientSecret As String, encodedStr As String, tens As String
tens = " 1 2 3 4 5 6 7 8 9 10 11 12"
Debug.Print "Begin"
ClientSecret = "a23456789012345678901234567890123456789012345678901234"
encodedStr = EncodeBase64(ClientSecret)
Debug.Print tens
Debug.Print ClientSecret
Debug.Print encodedStr
Debug.Print
ClientSecret = "b234567890123456789012345678901234567890123456789012345"
encodedStr = EncodeBase64(ClientSecret)
Debug.Print tens
Debug.Print ClientSecret
Debug.Print encodedStr
Debug.Print
ClientSecret = "c23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"
encodedStr = EncodeBase64(ClientSecret)
Debug.Print tens
Debug.Print ClientSecret
Debug.Print encodedStr
Debug.Print
ClientSecret = "c234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
encodedStr = EncodeBase64(ClientSecret)
Debug.Print tens
Debug.Print ClientSecret
Debug.Print encodedStr
Debug.Print
End Sub
Here's the output
Begin
1 2 3 4 5 6 7 8 9 10 11 12
a23456789012345678901234567890123456789012345678901234
YTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
1 2 3 4 5 6 7 8 9 10 11 12
b234567890123456789012345678901234567890123456789012345
YjIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
NQ==
1 2 3 4 5 6 7 8 9 10 11 12
c23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
YzIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4
1 2 3 4 5 6 7 8 9 10 11 12
c234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
YzIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4
OQ==
As can be seen, the 54 char string converts fine. For the 55 character string a newline char gets added. The 108 char string gets 1 newline and the 110 char string gest 2. Any ideas why this extra character is getting added each 55 characters?