How to convert Uint8Array to Uint16Array or string

Discussion in 'JavaScript' started by ociebiedocie, Sep 8, 2014.

  1. #1
    How to revert from step 3 to step 1?
    I read a lot and used google before asking.
    Here is my listing with comments:

    
    // --- STEP 1 ---
    x1=getFile('filename');
    // I get raw 16 bit binary string (it is not just a text)
    
    // --- STEP 2 ---
    x2=x1.split('').map(function(x){return x.charCodeAt(0);});
    // I convert string to array with ASCII codes (some of them are above 255)
    // first four elements of this array: 63367 63457 95 22
    
    // --- STEP 3 ---
    x3=new Uint8Array(x2);
    // I convert ASCII codes to 8 bit data array
    // first four elements of this array: 135 225 95 22
    
    // --- STEP 4 (PROBLEM) ---
    // - OPTION A (WORKING) -
    // if I save x4 to binary file, and then read it (like in STEP 1)
    // then data is the same as in original
    // this means that load file 1, do STEP 2, do STEP 3, save as file 2,
    // load file 2 will result in file 1 has exactly the same 
    // bytes as file 2 (compared in Total Commander)
    // - OPTION B (PROBLEM) -
    // problem is how to convert this Uint8Array (from STEP 3)
    // back to 16 bit ASCII codes array (like STEP 2),
    // or just to correct JavaScript string
    // (like it was in STEP 1 before you started to convert it to arrays)
    // to make this question more simple:
    // how to revert from step 3 to step 1
    
    Code (markup):
     
    ociebiedocie, Sep 8, 2014 IP