0

I have been using the base64 encoding function from this answer (code is below)

https://stackoverflow.com/a/506992/510296

I noticed that it is wrapping lines of output after the 72nd character (which causes problems when I try to pass that encoded string to the eBay API).

I can remove the line breaks easily enough with replace(base64string, vblf, "") but wanted to ask if there is a proper way to prevent line breaks in the output.

Function Base64Encode(sText)
    Dim oXML, oNode
    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.nodeTypedValue =Stream_StringToBinary(sText)
    Base64Encode = oNode.text
    Set oNode = Nothing
    Set oXML = Nothing
End Function

Function Stream_StringToBinary(Text)
    Const adTypeText = 2
    Const adTypeBinary = 1

    'Create Stream object
    Dim BinaryStream 'As New Stream
    Set BinaryStream = CreateObject("ADODB.Stream")

    'Specify stream type - we want To save text/string data.
    BinaryStream.Type = adTypeText

    'Specify charset For the source text (unicode) data.
    BinaryStream.CharSet = "us-ascii"

    'Open the stream And write text/string data To the object
    BinaryStream.Open
    BinaryStream.WriteText Text

    'Change stream type To binary
    BinaryStream.Position = 0
    BinaryStream.Type = adTypeBinary

    'Ignore first two bytes - sign of
    BinaryStream.Position = 0

    'Open the stream And get binary data from the object
    Stream_StringToBinary = BinaryStream.Read

    Set BinaryStream = Nothing
End Function
user692942
  • 16,398
  • 7
  • 76
  • 175
GWR
  • 1,878
  • 4
  • 26
  • 44
  • There's a `LineSeparator` property but it seems irrelevant to a binary data https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/lineseparator-property-ado?view=sql-server-2017 – montonero Jan 28 '19 at 07:20
  • @montonero it is. – user692942 Jan 28 '19 at 13:08
  • 2
    See this: https://stackoverflow.com/questions/41837881/base64-encode-with-stream-stringtobinary-inserts-a-newline-breaking-the-string – Plippie Jan 30 '19 at 12:52
  • @Plippie - Not sure how I missed that. Didn't find it via search. This question can be marked as duplicate of 41837881. – GWR Jan 30 '19 at 14:40

0 Answers0