skip to Main Content

I’m receiving an image in a stream as shown below that is (imageBuffer.toString('utf8')) converted to utf8 string before receiving, however when I try to create a buffer with the following data the buffer appears to be different and not able to recreate image.

msg = ����ExifII*��DuckyF��,Photoshop 3.08BIM%��Adobed����↵→→→→↵↵ ↵ ↵↵��g����→!1AQa”q�2��B#��R3ђ��$T���bCSD��%EU�r�cs��!1QA����”a2q�B#��� ?�65�]����jF(mR�$Pm�*�a��F�H7҄�Ҁ� 7D�(�

console.log( typeOf msg ) => string

So far I have tried this:

let buffer = Buffer.from(msg, 'utf8');
let writable = require('fs').createWriteStream( '/home/test.jpg);
writable.write(buffer);
writable.end();

Problem is: Not able to reproduce the same image. output buffer size is different from input buffer of the image.

2

Answers


  1. Use

    let buffer = Buffer.from(new Uint8Array(msg));
    ...
    
    Login or Signup to reply.
  2. Do not send binary as utf-8 string. Try base64:

    imageBuffer.toString('base64')
    

    and:

    let buffer = Buffer.from(msg, 'base64');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search