The max call stack exceeded error is being generated by converting the hex string into an array and supplying it to String.fromCharCode() as an arguments array using the apply method from Function.prototype.

There are practical limits on how many arguments can be passed in JavaScript function calls so the method can work for small files and fail for larger ones.

Refactoring the code will be necessary. As an untested basic solution

function hexToBase64(str) {
    var bString = "";
    for( var i = 0; i < str.length; i +=2) {
         bString += String.fromCharCode( parseInt( str.substr( i, 2), 16));
    }
    return btoa(bString);
}

may provide some ideas.

Answer from traktor on Stack Overflow
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
Hex to Base64 | Base64 Encode | Base64 Converter | Base64
The “Hex to Base64” converter is a smart tool which is able to convert online Hex values to Base64 strings. I call him “smart” because it accepts several written representations of hexadecimal values. The conversion process is quite simple: the converter decodes the Hex value into the original data, then encodes it to Base64 and gives you the final result almost instantly.
🌐
Teleport
goteleport.com › home › resources › tools › hex to base64 converter | instant hex to base64 conversion
Hex to Base64 Converter | Instant Hex to Base64 Conversion | Teleport
Instantly transform your hexadecimal strings into Base64 encoding using this free online tool. Loading tool configuration... ... Have you ever needed to send binary data, like an image or a file, through systems designed for text?
People also ask

Is this HEX to Base64 converter free?
Yes, it's completely free with no registration, ads, or usage limits.
🌐
onlinewebdevtools.com
onlinewebdevtools.com › home › encoding tools › hex to base64
HEX to Base64 Converter | Free & Fast Online Tool – DevTools
Can I convert Base64 back to HEX?
Yes — use the reverse tool: Base64 to HEX converter at /base64-to-hex.
🌐
onlinewebdevtools.com
onlinewebdevtools.com › home › encoding tools › hex to base64
HEX to Base64 Converter | Free & Fast Online Tool – DevTools
What happens if my HEX is invalid?
The tool validates the input against [0-9A-Fa-f] after stripping separators. If non-hex characters are found, an error toast appears and the output shows "Error: Invalid hexadecimal input".
🌐
onlinewebdevtools.com
onlinewebdevtools.com › home › encoding tools › hex to base64
HEX to Base64 Converter | Free & Fast Online Tool – DevTools
🌐
Tomeko
tomeko.net › online_tools › hex_to_base64.php
Hex to base64 converter
ONLINE TOOLS · Base64 -> HEX · Base32 -> HEX · Base32hex -> HEX · ASCII -> HEX · ASCII -> u16/u32/u64 HEX · HEX -> Base64 · Text -> Base64 · HEX -> Base32 · HEX -> Base32hex · HEX -> ASCII · HEX -> DEC · HEX -> bitstream · DEC -> ASCII · BIN <-> 32-bit hex · MD5 calculator · SHA1 calculator · Regular exp. Text -> Cpp · Cpp -> text · Text -> Pascal · HEX -> file · File -> HEX · File -> Base64 · Text -> HTML ul · Images ...
Top answer
1 of 2
1

I was able to get this working. Here is how.

In sendHex, I removed the &H portion, and wrapped my string in hex():

Private Sub sendHex(valHex)
    Dim iCntHex
    For iCntHex = 1 To len(valHex) Step 2
    If len( mid(valHex, iCntHex, 2)) = 1 Then 
        response.write "0"
    end if 
    response.write mid(valHex, iCntHex, 2)
    Next
End Sub

This results in a string output like this (in byte strings of 2 hexidecimal chars):

424d1e050000000000003e00000028000000340000001800000001000

I can then dump that proper hex string into a HEX to base64 function as follows (not written by me, but rather, by Richard Mueller - http://www.rlmueller.net/Base64.htm)

Function HexToBase64(strHex)
    ' Function to convert a hex string into a base64 encoded string.
    ' Constant B64 has global scope.
    Dim lngValue, lngTemp, lngChar, intLen, k, j, strWord, str64, intTerm

    intLen = Len(strHex)

    ' Pad with zeros to multiple of 3 bytes.
    intTerm = intLen Mod 6
    If (intTerm = 4) Then
        strHex = strHex & "00"
        intLen = intLen + 2
    End If
    If (intTerm = 2) Then
        strHex = strHex & "0000"
        intLen = intLen + 4
    End If

    ' Parse into groups of 3 hex bytes.
    j = 0
    strWord = ""
    HexToBase64 = ""
    For k = 1 To intLen Step 2
        j = j + 1
        strWord = strWord & Mid(strHex, k, 2)
        If (j = 3) Then
            ' Convert 3 8-bit bytes into 4 6-bit characters.
            lngValue = CCur("&H" & strWord)

            lngTemp = Fix(lngValue / 64)
            lngChar = lngValue - (64 * lngTemp)
            str64 = Mid(B64, lngChar + 1, 1)
            lngValue = lngTemp

            lngTemp = Fix(lngValue / 64)
            lngChar = lngValue - (64 * lngTemp)
            str64 = Mid(B64, lngChar + 1, 1) & str64
            lngValue = lngTemp

            lngTemp = Fix(lngValue / 64)
            lngChar = lngValue - (64 * lngTemp)
            str64 = Mid(B64, lngChar + 1, 1) & str64

            str64 = Mid(B64, lngTemp + 1, 1) & str64

            HexToBase64 = HexToBase64 & str64
            j = 0
            strWord = ""
        End If
    Next
    ' Account for padding.
    If (intTerm = 4) Then
        HexToBase64 = Left(HexToBase64, Len(HexToBase64) - 1) & "="
    End If
    If (intTerm = 2) Then
        HexToBase64 = Left(HexToBase64, Len(HexToBase64) - 2) & "=="
    End If

End Function

This converts the above to base64, and I can use the output like this (e.g. in a browser url bar) to view it as an image:

data:image/bmp;base64,Qk0eBQAAAAAAAD4AAAAo...

2 of 2
1

The Microsoft.XMLDOM has built in converters for bin.base64 and bin.hex. I wrote functions that demonstrate how to use this:

Function TextToBinary(text, dataType)
  Dim dom
  Set dom = CreateObject("Microsoft.XMLDOM")
  dom.loadXML("<HELLO/>")
  dom.documentElement.nodeTypedValue = text
  dom.documentElement.dataType = dataType
  TextToBinary = dom.documentElement.nodeTypedValue
End Function

Function BinaryToText(binary, dataType)
  Dim dom
  Set dom = CreateObject("Microsoft.XMLDOM")
  dom.loadXML("<HELLO/>")
  dom.documentElement.dataType = dataType
  dom.documentElement.nodeTypedValue = binary
  dom.documentElement.removeAttribute("dt:dt")
  BinaryToText = dom.documentElement.nodeTypedValue
End Function

Function HexToBase64(strHex)
  HexToBase64 = BinaryToText(TextToBinary(strHex, "bin.hex"), "bin.base64")
End Function

Function Base64ToHex(strBase64)
  Base64ToHex = BinaryToText(TextToBinary(strBase64, "bin.base64"), "bin.hex")
End Function

Here's an example of their usage:

MsgBox HexToBase64("41")
MsgBox Base64ToHex("QQ==")

Also look at the ADODB.Stream as a means of working with binary files. It'll work with these routines.

🌐
Base64.Guru
base64.guru › home › base64 converter › base64 decode
Base64 to Hex | Base64 Decode | Base64 Converter | Base64
Base64 to Hex · Base64 to Image · Base64 to PDF · Base64 to Text · Base64 to Video · Audio to Base64 · CSS to Base64 · File to Base64 · Hex to Base64 · HTML to Base64 · Image to Base64 · PDF to Base64 · Text to Base64 · URL to Base64 · Video to Base64 ·
🌐
DevTools
onlinewebdevtools.com › home › encoding tools › hex to base64
HEX to Base64 Converter | Free & Fast Online Tool – DevTools
The tool auto-detects separators (spaces, dashes, colons, 0x) and normalizes the hex to contiguous bytes. Pick the desired Base64 flavor in Format: Standard, URL Safe, or With Line Breaks (76 chars).
Find elsewhere
🌐
Retool
community.retool.com › 💬 app building
Hex to Base64 to Images - 💬 App Building - Retool Forum
January 13, 2023 - I have a Microsoft sql database that contains staff pictures. They seem to be stored as hex code. When I remove the 0x from the start of the hex code and paste in the hex code online into some hex to base64 conversion tools it converts successfully. I can then view the image in a base64 image viewer online successfully.
🌐
Hexcalculator
hexcalculator.org › hex-to-base64
Hex to Base64 Converter
May 29, 2026 - If you work with multiple encoding systems, the Hex to UTF8 Converter, Hex to EBCDIC Converter, or Hex to Base32 Converter may also be helpful. Enter your hex string (e.g., 48656C6C6F). Click Convert. Get the Base64 output (e.g., SGVsbG8=).
🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
This information is used to convert the decoded data to our website's character set so that all letters and symbols can be displayed properly. Note that this is irrelevant for files since no web-safe conversions need to be applied to them. Decode each line separately: The encoded data usually consists of continuous text, so even newline characters are converted into their Base64-encoded forms.
🌐
Base64.Guru
base64.guru › converter › decode › image
Base64 to Image | Base64 Decode | Base64 Converter | Base64
The “Base64 to Image” converter will force the decoding result to be displayed as an image, even if it is a different file type. Therefore, if you are not sure that your Base64 string is an image, use the Base64 to file converter since it is capable of displaying various file types.
🌐
Cryptii
cryptii.com › pipes › hex-to-base64
Hex to Base64: Encode and decode bytes online - cryptii
Base64 encoding schemes are used when binary data needs to be stored or transferred as textual data. Therefore 64 characters are chosen that are both members of a subset common to most encodings (ASCII), and also printable.
🌐
RapidTables
rapidtables.com › web › tools › base64-encode.html
Base64 Encode & Decode
Encode Base64 from Text/image/hex/binary encode to base64 online.
🌐
RapidTables
rapidtables.com › web › tools › base64-decode.html
Base64 Decode & Encode
Base64 decode to text string/image/hex/binary: Base64 encoder ► · URL decoder ► · Text input: The text input is first encoded as binary bit stream of ASCII codes of each character. Each 6 bits of the bit stream are encoded to base64 digit. Encoded base64 text string output: Image input: Image data URI scheme with base64 encoded data output: data:image/jpeg;base64, is the data URI scheme header.
🌐
Browserling
browserling.com › tools › image-to-base64
Image to Base64 Converter - Encode Images to Base64 - Online - Browserling Web Developer Tools
Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64.
Top answer
1 of 7
37

Edit 26 Aug 2020: As suggested by Ali in the comments, using codecs.encode(b, "base64") would result in extra line breaks for MIME syntax. Only use this method if you do want those line breaks formatting.

For a plain Base64 encoding/decoding, use base64.b64encode and base64.b64decode. See the answer from Ali for details.


In Python 3, arbitrary encodings including Hex and Base64 has been moved to codecs module. To get a Base64 str from a hex str:

import codecs

hex = "10000000000002ae"
b64 = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode()
2 of 7
36
from base64 import b64encode, b64decode

# hex -> base64
s = 'cafebabe'
b64 = b64encode(bytes.fromhex(s)).decode()
print('cafebabe in base64:', b64)

# base64 -> hex
s2 = b64decode(b64.encode()).hex()
print('yv66vg== in hex is:', s2)
assert s == s2

This prints:

cafebabe in base64: yv66vg==
yv66vg== in hex is: cafebabe

The relevant functions in the documentation, hex to base64:

  • b64encode
  • bytes.fromhex
  • bytes.decode

Base64 to hex:

  • b64decode
  • str.encode
  • bytes.hex

I don't understand why many of the other answers are making it so complicated. For example the most upvoted answer as of Aug 26, 2020:

  • There is no need for the codecs module here.
  • The codecs module uses base64.encodebytes(s) under the hood (see reference here), so it converts to multiline MIME base64, so you get a new line after every 76 bytes of output. Unless you are sending it in e-mail, it is most likely not what you want.

As for specifying 'utf-8' when encoding a string, or decoding bytes: It adds unnecessary noise. Python 3 uses utf-8 encoding for strings by default. It is not a coincidence that the writers of the standard library made the default encoding of the encode/decode methods also utf-8, so that you don't have to needlessly specify the utf-8 encoding over and over again.

🌐
8gWiFi
8gwifi.org › base64Hex.jsp
Base64 to Hex Converter (Hex to Base64) – Online, Fast, Free
Convert Base64 to Hex and Hex to Base64 entirely in your browser (secure).
🌐
1024tools
1024tools.com › index.php › en › hexbase64
Hex to Base64, Base64 to Hex, Hexadecimal Base64 Converter Tool - 1024Tools
Hex/Base64 converter tool supports bidirectional conversion between hexadecimal and Base64 encoding.