js将4个字节型字符串转为Float
function convertFloat(byteStr) { var buffer = str2ArrayBuffer(byteStr, 4); var dataView = new DataView(buffer, 0, 4); return dataView.getFloat32(0); } // 字符串转为ArrayBuffer对象,参数为字符串 function str2ArrayBuffer(str, len) { var buf = new ArrayBuffer(len); var bufView = new Uint8Array(buf); for (var i = 0, strLen = str.length; i < strLen; i++) { bufView[i] = parseInt(str.substr(i * 2, 2), 16); } return buf; }
首先将字符串转换为ArrayBuffer对象数组,再根据DataView方法进行转换