Jump to content

Decoding Base64 And Displaying Image In Browser


Recommended Posts

This was previously posted in the ASP forum, but is probably better off here.I am looking for a way to take a base64 string that I get from an XML document and covert it to an image. I am trying to integrate UPS Online Tools to generate a Return label from our website. I was able to find a script that looks like it could work. All search inquires for decoding a base64 image is pointing me to this vb function:

Function Base64Decode(ByVal base64String)  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"  Dim dataLength, sOut, groupBegin    'remove white spaces, If any  base64String = Replace(base64String, vbCrLf, "")  base64String = Replace(base64String, vbTab, "")  base64String = Replace(base64String, " ", "")    'The source must consists from groups with Len of 4 chars  dataLength = Len(base64String)  If dataLength Mod 4 <> 0 Then	Err.Raise 1, "Base64Decode", "Bad Base64 string."	Exit Function  End If    ' Now decode each group:  For groupBegin = 1 To dataLength Step 4	Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut	' Each data group encodes up To 3 actual bytes.	numDataBytes = 3	nGroup = 0	For CharCounter = 0 To 3	  ' Convert each character into 6 bits of data, And add it To	  ' an integer For temporary storage.  If a character is a '=', there	  ' is one fewer data byte.  (There can only be a maximum of 2 '=' In	  ' the whole string.)	  thisChar = Mid(base64String, groupBegin + CharCounter, 1)	  If thisChar = "=" Then		numDataBytes = numDataBytes - 1		thisData = 0	  Else		thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1	  End If	  If thisData = -1 Then		Err.Raise 2, "Base64Decode", "Bad character In Base64 string."		Exit Function	  End If	  nGroup = 64 * nGroup + thisData	Next		'Hex splits the long To 6 groups with 4 bits	nGroup = Hex(nGroup)		'Add leading zeros	nGroup = String(6 - Len(nGroup), "0") & nGroup		'Convert the 3 byte hex integer (6 chars) To 3 characters	pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _	  Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _	  Chr(CByte("&H" & Mid(nGroup, 5, 2)))		'add numDataBytes characters To out string	sOut = sOut & Left(pOut, numDataBytes)  Next  Base64Decode = sOutEnd Function

I am trying to run this function like this:


But all I get is:

GIF87a@ ç

I must be doing something wrong. Can anyone help?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...