JSFiddle - React, Tailwind, and code Playground

by Brian Houlihan

JavaScript

var data1 =  [226, 64];
  convertToInt(data1);
  //var data2 =  [162,22,131,191];
  var data2 =  [191,131,22,162];
  convertToFloat(data2);
  function convertToInt(data){
    //var data =  [64, 226];

    // Create a buffer
    var buf = new ArrayBuffer(2);
    // Create a data view of it
    var view = new DataView(buf);
    
    // set bytes
    data.forEach(function (b, i) {
        view.setUint8(i, b);
    });
    
    // Read the bits as a float; note that by doing this, we're implicitly
    // converting it from a 32-bit float into JavaScript's native 64-bit double
    var num = view.getInt16(0);
    // Done
    console.log(num);
    return num;
  }

  function convertToFloat(data){
    //var data =  [64, 226, 157, 10];

    // Create a buffer
    var buf = new ArrayBuffer(4);
    // Create a data view of it
    var view = new DataView(buf);
    
    // set bytes
    data.forEach(function (b, i) {
        view.setUint8(i, b);
    });
    
    // Read the bits as a float; note that by doing this, we're implicitly
    // converting it from a 32-bit float into JavaScript's native 64-bit double
    var num = view.getFloat32(0);
    // Done
    console.log(num);
    return num;
  }