Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have done a system to read an image from an SD card on Arduino and send all these bytes to a Node.js server.

This is an example of the first row of the GIF image (I will use a JPEG)

71%73%70%56%57%97%50%0%50%0%247%0%0%150%140%115%102%94%69%198%187%159%123%114%90%71%71%71%138%129%101%202%193%166%201%193%172%238%234%221%200%197%188%140%135%122%211%201%171%67%59%38%235%229%209%250%250%250%229%228%226%166%157%131%134%126%103%193%180%142%228%220%198%130%130%130%225%221%209%227%221%202%228%218%188%245%241%230%38%37%37%156%145%115%248%243%228%195%185%157%211%202%180%74%66%42%209%198%162%108%98%70%234%230%219%187%179%156%219%211%187%92%84%61%241%238%230%220%218%211%231%228%220%206%194%159%248%246%242%118%108%83%252%251%249%233%225%202%23%19%9%103%77%0%248%244%232%220%211%181%243%236%214%128%116%82%253%251%246%33%29%18%176%163%125%252%252%251%220%213%192%238%236%233%187%174%138%162%148%108%109%102%84%131%127%117%181%172%145%211%"

I checked that I got the same buffer each time with the same numbers. The problem is to convert these numbers into a file, in a few words: recreate the file.

I was looking for a decode of bytes to file, and I found this function:

var chars = data.split("%");
function pack(utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {

        c = utftext[i];

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext[(i+1)];
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext[(i+1)];
            c3 = utftext[(i+2)];
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return string;
}
pack(chars);

This function works just half way. The header should start with "GIF89a2" and I can see the same result on the file recreated from Node.js, but the other strange symbols are not the same.

This is the method that I use to write the string into a file:

writeStream.write(pack(chars));

I am sure that there is a system to recreate the image on my Node.js server, but I am not an expert about that.

share|improve this question
    
To be clear, you're sending this over serial from Arduino to Bluetooth and you're looking for a way to write binary data to a file in Node.js? –  ZnArK Oct 9 '12 at 13:35
    
I am using a GPRS modem that has a serial port connection, so I have to write through a serial –  max246 Oct 9 '12 at 17:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.