function _arrayBufferToBase64( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return window.btoa( binary );
}

but, non-native implementations are faster e.g. https://gist.github.com/958841 see http://jsperf.com/encoding-xhr-image-data/6

jsPerf.com is jsPerf.app now: https://jsperf.app/encoding-xhr-image-data/51

Updated benchmarks: https://jsben.ch/wnaZC

Answer from mobz on Stack Overflow
🌐
EyeHunts
tutorial.eyehunts.com › home › byte array to base64 javascript | examples code
Byte Array to Base64 JavaScript | Simple Examples code
December 9, 2021 - Convert received arrayBuffer value into Base64 encoded string and then we write a file using toBinary() function of CFML bypassing Base64 encoded string in it. Below is custom JavaScript function arrayBufferToBase64() to accomplish the requirement.
Discussions

javascript - How to convert byte array to base64 - Stack Overflow
In this post (Creating a Blob from a base64 string in JavaScript) they take base64 and convert to a byte array. I haven't been able to figure out how to convert byte array back in to base64 code. I More on stackoverflow.com
🌐 stackoverflow.com
javascript - Convert Byte array to Base64 string in Angularjs - Stack Overflow
I am getting byte array in service response and that image would be shown in an image field of my html page. Any idea how can i implement this. I tried to find out solution for this over stack over... More on stackoverflow.com
🌐 stackoverflow.com
May 30, 2016
javascript - how to convert byte array to base64 encoded format? - Stack Overflow
I have a page to download a file from the Postgres database. Now I can hit the following URL to view the file content present in the database(stored as bytes)-HTTP://sandbox4.wootz.io:8080/api/blob/1/ More on stackoverflow.com
🌐 stackoverflow.com
July 6, 2020
Avoid byte array to base64 conversion
FYI I got it working. More on reddit.com
🌐 r/Blazor
3
1
February 5, 2021
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromBase64
Uint8Array.fromBase64() - JavaScript - MDN Web Docs
If you already have an array buffer allocated and you want to populate it, use the instance method Uint8Array.prototype.setFromBase64() instead. ... A base64 string encoding bytes to convert to a Uint8Array.
🌐
npm
npmjs.com › package › byte-base64
byte-base64 - npm
August 18, 2020 - In particular, atob and btoa only support ASCII strings. ... bytesToBase64(bytes: number[] | Uint8Array): string - Encodes a Uint8Array or JS array of bytes to a base64-encoded string.
      » npm install byte-base64
    
Published: Aug 18, 2020
Version: 1.1.0
🌐
Isummation
isummation.com › home › development › convert arraybuffer to base64 string and vice-versa
JavaScript - Convert arrayBuffer to Base64 string and vice-versa iSummation - USA
August 30, 2019 - Here first we need to convert received arrayBuffer value into Base64 encoded string and then we write a file using toBinary() function of CFML by passing Base64 encoded string in it.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 60730932 › how-to-convert-byte-array-to-base64
javascript - How to convert byte array to base64 - Stack Overflow
In this post (Creating a Blob from a base64 string in JavaScript) they take base64 and convert to a byte array. I haven't been able to figure out how to convert byte array back in to base64 code. I
🌐
IQCode
iqcode.com › code › javascript › convert-base64-string-to-byte-array-javascript
convert base64 string to byte array javascript Code Example
November 4, 2021 - javascript base64 encode byte array bytearray to base64 js byte array to base64 in javascript byte to base64 javascript javascript bytes to base64 nodejs base64 to byte array base64 to byte array js js string to byte array and base64 encode convert byte to base64 string javascript js base64 encode byte array convert byte array to base64 string js from byte base64 to base64 javascript array byte to base64 js javascript array of bytes to base64 node js base64 to byte array javascript byte array to base64 convert 2048 to byte array using base64 decoder javascript convert string to byte array usin
🌐
GitHub
gist.github.com › llzes › 24fe0a9b3d786d48f2b3932a633d5b2b
Byte to Base64 implementation in JavaScript · GitHub
Byte to Base64 implementation in JavaScript. GitHub Gist: instantly share code, notes, and snippets.
Top answer
1 of 2
8

Use this filter to convert byte array to base64

app.filter('bytetobase', function () {
    return function (buffer) {
        var binary = '';
        var bytes = new Uint8Array(buffer);
        var len = bytes.byteLength;
        for (var i = 0; i < len; i++) {
            binary += String.fromCharCode(bytes[i]);
        }
        return window.btoa(binary);
    };
});

to bind it as image use

<img ng-src="data:image/JPEG;base64,{{picture | bytetobase}}" alt="..." width="100" height="100">

Or if you need to assign it a variable use

var image = $filter('bytetobase')($scope.picture );
2 of 2
3

If you need to display and image from byte array you can create an object using Blob and get it's URL to pass into the image tag source. The last parameter in Blob constructor contains information about blob type, so you should set correct type during blob creation.

$http.get(url, {responseType: 'arraybuffer'})
  .then(function(response) {
    return $window.URL.createObjectURL(new Blob([response.data], {type: 'image/png'}));
  });

And when you don't plan to work with your object any longer (e.g. after image has been loaded in appropriate img tag)

Update

Alternative solution with base64

$scope.getPrescription = function(id) {
  $ionicLoading.show({
    template: '<ion-spinner icon="spiral"></ion-spinner>',
    noBackdrop: false
  });
  serverRepo.prescriptionGet(id).then(function(objS) {
    console.log("orderByCustomer:\n" + JSON.stringify(objS));
    // Creating file reader
    var reader = new window.FileReader();
    // Creating blob from server's data
    var data = new Blob([objS.data], {type: 'image/jpeg'});
    // Starting reading data
    reader.readAsDataURL(data); 
    // When all data was read
    reader.onloadend = function() {
      // Setting source of the image
      $scope.picdata = reader.result;
      // Forcing digest loop
      $scope.$apply();
    }

    $ionicLoading.hide();
    console.log("getOrderByNew_success_loadMore:\n" +$scope.picdata);
    }, function(objE) {
    $ionicLoading.hide();
  });
}
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-byte-array-to-string-in-javascript
JavaScript - Convert Byte Array to String - GeeksforGeeks
July 23, 2025 - String.fromCharCode converts each byte to its corresponding character. This approach involves encoding the byte array into Base64 and decoding it to a string.
🌐
Wikitechy
wikitechy.com › tutorials › javascript › encode-a-string-to-base-64-in-javascript
[Solved-100% Working Code] - Encode a string to Base64 in javascript - javascript tutorial - Wikitechy
We can use btoa() and atob() to convert to and from base64 encoding. btoa() accepts a “string” where each character represents an 8-bit byte - If we pass a string containing characters that can’t be represented in 8 bits, it will probably break. This isn’t a problem if you’re actually ...
🌐
Medium
medium.com › @koteswar.meesala › convert-array-buffer-to-base64-string-to-display-images-in-angular-7-4c443db242cd
Convert Array buffer to base64 string to display images in Angular 7 | by Kotesh Meesala | Medium
June 9, 2019 - Thanks to Tin.K for pointing out this. const STRING_CHAR = TYPED_ARRAY.reduce((data, byte)=> {return data + String.fromCharCode(byte);}, '')); ... Now, to obtain the base64 string, pass the string of characters to window.btoa() method which ...
🌐
GitHub
gist.github.com › mantonaci › 10731724
Convert byte array to base64 string · GitHub
Convert byte array to base64 string · Raw · gistfile1.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Technocat Games
technocatgames.com › blog › fast-arraybuffer-to-base64-in-javascript
Fast ArrayBuffer to Base64 in JavaScript - Technocat Games
December 19, 2024 - // https://stackoverflow.com/a/9458996 function arrayBufferToBase64(buffer) { var binaryString = ''; var array = new Uint8Array(buffer); var len = array.byteLength; for (var i = 0; i < len; i++) { binaryString += String.fromCharCode(array[i]); ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-byte-array-to-string-in-javascript
JavaScript – Convert Byte Array to String | GeeksforGeeks
November 26, 2024 - String.fromCharCode converts each byte to its corresponding character. This approach involves encoding the byte array into Base64 and decoding it to a string.
Top answer
1 of 1
1

Your file is a correct ASCII text file with content [\x58595a5052415445454b3123473b4c534e44204e474f49574853474849444748445348474d70253335] literally.
That you get this content when you download it is perfectly fine, since it's what this file's content is.

What you want is to parse that content from the Hex representation they used to an actual ArrayBuffer to finally read that again as UTF-8 text (or any encoding respecting ASCII).

This has to be done after you do download the file and read it as text.
You first extract the actual bytes sequence as Hex from that [\x - ] wrapper, then you split the resulting string at every two chars to get hex values of every bytes, and finally you parse it into an Uint8Array to get back the original data:

// for StackSnippet we need to hardcode the response
// OP would have to make its request return that string
const response = { data: String.raw`[\x58595a5052415445454b3123473b4c534e44204e474f49574853474849444748445348474d70253335]` };
// .then( (response) => {
  const encoded_text = response.data;
  // remove leading "[\x" and final "]"
  const encoded_data = encoded_text.slice( 3, -1 );
  // split at every two chars, so we can get 0xNN, 0xNN
  const hex_bytes = encoded_data.match( /.{2}/g );
  // as numbers (0 - 255)
  const num_bytes = hex_bytes.map( (hex) => parseInt( hex, 16 ) );
  // wrap in an Uint8Array
  const view = new Uint8Array( num_bytes );
  
  // from there you can generate the Blob to save on disk
  download( new Blob( [ view ] ), "file.txt" );

  // but if you want to read it as UTF-8 text, you can:
  const as_text = new TextDecoder().decode( view );
  console.log( as_text );
// } );

function download( blob, filename ) {
  const anchor = document.createElement( "a" );
  anchor.href = URL.createObjectURL( blob );
  anchor.download = filename;
  anchor.textContent = "click to download";
  document.body.append( anchor );
}

🌐
Base64 Encode
base64encode.org
Base64 Encode and Decode - Online
These three bytes are joined together in a 24 bit buffer producing the binary sequence "010011010110000101101110". Packs of 6 bits (6 bits have a maximum of 64 different binary values) are converted into 4 numbers (24 = 4 * 6 bits) which are then converted to their corresponding values in Base64.