Skip to content

Instantly share code, notes, and snippets.

@Jozo132
Last active May 12, 2024 21:04
Show Gist options
  • Select an option

  • Save Jozo132/2c0fae763f5dc6635a6714bb741d152f to your computer and use it in GitHub Desktop.

Select an option

Save Jozo132/2c0fae763f5dc6635a6714bb741d152f to your computer and use it in GitHub Desktop.

Revisions

  1. Jozo132 revised this gist Feb 12, 2022. 2 changed files with 52 additions and 6 deletions.
    29 changes: 26 additions & 3 deletions float32encoding.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,29 @@
    // Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
    // Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
    // Modifyed by: Jozo132 (https://github.com/Jozo132)
    /* ##### float32encoding.js #####
    MIT License
    - Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
    - Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
    - Modifyed by: Jozo132 (https://github.com/Jozo132)
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

    const Float32ToHex = (float32) => {
    const getHex = i => ('00' + i.toString(16)).slice(-2);
    29 changes: 26 additions & 3 deletions float32encoding.min.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,29 @@
    // Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
    // Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
    // Modifyed by: Jozo132 (https://github.com/Jozo132)
    /* ##### float32encoding.min.js #####
    MIT License
    - Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
    - Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
    - Modifyed by: Jozo132 (https://github.com/Jozo132)
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

    const Float32ToHex = float32 => { const getHex = i => ('00' + i.toString(16)).slice(-2); var view = new DataView(new ArrayBuffer(4)); view.setFloat32(0, float32); return Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join(''); }
    const Float32ToBin = float32 => parseInt(Float32ToHex(float32), 16).toString(2).padStart(32, '0');
  2. Jozo132 revised this gist Dec 22, 2018. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions float32encoding.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    // Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
    // Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
    // Modifyed by: Jozo132 (https://github.com/Jozo132)

    const Float32ToHex = float32 => { const getHex = i => ('00' + i.toString(16)).slice(-2); var view = new DataView(new ArrayBuffer(4)); view.setFloat32(0, float32); return Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join(''); }
    const Float32ToBin = float32 => parseInt(Float32ToHex(float32), 16).toString(2).padStart(32, '0');

    const ToFloat32 = num => { if (num > 0 || num < 0) { var sign = (num >>> 31) ? -1 : 1; var exp = (num >>> 23 & 0xff) - 127; var mantissa = ((num & 0x7fffff) + 0x800000).toString(2); var float32 = 0; for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- } return float32 * sign; } else return 0 }
    const HexToFloat32 = str => ToFloat32(parseInt(str, 16));
    const BinToFloat32 = str => ToFloat32(parseInt(str, 2));


    // ------ FULL EXAMPLE ------
    var value = -0.3; // JS number variable

    // FLOAT32 <===> HEX
    var f32_hex = Float32ToHex(value); // JS number => HEX string of a Float32 standard number
    var f32_hex_inverse = HexToFloat32(f32_hex); // HEX string of a Float32 standard number => JS number

    // FLOAT32 <===> BIN
    var f32_bin = Float32ToBin(value); // JS number => HEX string of a Float32 standard number
    var f32_bin_inverse = BinToFloat32(f32_bin); // HEX string of a Float32 standard number => JS number

    console.log(`Input value (${value}) => hex (${f32_hex}) [${Math.ceil(f32_hex.length / 2)} bytes] => float32 (${f32_bin_inverse})`);
    console.log(`Input value (${value}) => binary (${f32_bin}) [${f32_bin.length} bits] => float32 (${f32_bin_inverse})`);

    /* DEBUG OUTPUT:
    Input value (-0.3) => hex (be99999a) [4 bytes] => float32 (-0.30000001192092896)
    Input value (-0.3) => binary (10111110100110011001100110011010) [32 bits] => float32 (-0.30000001192092896)
    */
  3. Jozo132 revised this gist Dec 19, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions float32encoding.js
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,7 @@ const BinToFloat32 = (str) => {
    } else return 0
    }

    // Full example
    var test_value = -0.3;
    console.log(`Input value (${test_value}) => hex (${Float32ToHex(test_value)}) [${Math.ceil(Float32ToHex(test_value).length / 2)} bytes] => float32 (${HexToFloat32(Float32ToHex(test_value))})`);
    console.log(`Input value (${test_value}) => binary (${Float32ToBin(test_value)}) [${Float32ToBin(test_value).length} bits] => float32 (${BinToFloat32(Float32ToBin(test_value))})`);
  4. Jozo132 revised this gist Dec 19, 2018. 1 changed file with 19 additions and 16 deletions.
    35 changes: 19 additions & 16 deletions float32encoding.js
    Original file line number Diff line number Diff line change
    @@ -19,30 +19,33 @@ const Float32ToBin = (float32) => {

    const HexToFloat32 = (str) => {
    var int = parseInt(str, 16);
    var sign = (int >>> 31) ? -1 : 1;
    var exp = (int >>> 23 & 0xff) - 127;
    var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
    var float32 = 0
    for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- }
    return float32 * sign;
    if (int > 0 || int < 0) {
    var sign = (int >>> 31) ? -1 : 1;
    var exp = (int >>> 23 & 0xff) - 127;
    var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
    var float32 = 0
    for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- }
    return float32 * sign;
    } else return 0
    }

    const BinToFloat32 = (str) => {
    var int = parseInt(str, 2);
    var sign = (int >>> 31) ? -1 : 1;
    var exp = (int >>> 23 & 0xff) - 127;
    var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
    var float32 = 0
    for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- }
    return float32 * sign;
    if (int > 0 || int < 0) {
    var sign = (int >>> 31) ? -1 : 1;
    var exp = (int >>> 23 & 0xff) - 127;
    var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
    var float32 = 0
    for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- }
    return float32 * sign;
    } else return 0
    }

    // Full example
    var test_value = 1.3;
    var test_value = -0.3;
    console.log(`Input value (${test_value}) => hex (${Float32ToHex(test_value)}) [${Math.ceil(Float32ToHex(test_value).length / 2)} bytes] => float32 (${HexToFloat32(Float32ToHex(test_value))})`);
    console.log(`Input value (${test_value}) => binary (${Float32ToBin(test_value)}) [${Float32ToBin(test_value).length} bits] => float32 (${BinToFloat32(Float32ToBin(test_value))})`);

    /* DEBUG OUTPUT:
    Input value (1.3) => hex (3fa66666) [4 bytes] => Float32 (1.2999999523162842)
    Input value (1.3) => binary (00111111101001100110011001100110) [32 bits] => Float32 (1.2999999523162842)
    Input value (-0.3) => hex (be99999a) [4 bytes] => float32 (-0.30000001192092896)
    Input value (-0.3) => binary (10111110100110011001100110011010) [32 bits] => float32 (-0.30000001192092896)
    */
  5. Jozo132 created this gist Dec 19, 2018.
    48 changes: 48 additions & 0 deletions float32encoding.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    // Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
    // Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
    // Modifyed by: Jozo132 (https://github.com/Jozo132)

    const Float32ToHex = (float32) => {
    const getHex = i => ('00' + i.toString(16)).slice(-2);
    var view = new DataView(new ArrayBuffer(4))
    view.setFloat32(0, float32);
    return Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join('');
    }

    const Float32ToBin = (float32) => {
    const HexToBin = hex => (parseInt(hex, 16).toString(2)).padStart(32, '0');
    const getHex = i => ('00' + i.toString(16)).slice(-2);
    var view = new DataView(new ArrayBuffer(4))
    view.setFloat32(0, float32);
    return HexToBin(Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join(''));
    }

    const HexToFloat32 = (str) => {
    var int = parseInt(str, 16);
    var sign = (int >>> 31) ? -1 : 1;
    var exp = (int >>> 23 & 0xff) - 127;
    var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
    var float32 = 0
    for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- }
    return float32 * sign;
    }

    const BinToFloat32 = (str) => {
    var int = parseInt(str, 2);
    var sign = (int >>> 31) ? -1 : 1;
    var exp = (int >>> 23 & 0xff) - 127;
    var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
    var float32 = 0
    for (i = 0; i < mantissa.length; i += 1) { float32 += parseInt(mantissa[i]) ? Math.pow(2, exp) : 0; exp-- }
    return float32 * sign;
    }

    // Full example
    var test_value = 1.3;
    console.log(`Input value (${test_value}) => hex (${Float32ToHex(test_value)}) [${Math.ceil(Float32ToHex(test_value).length / 2)} bytes] => float32 (${HexToFloat32(Float32ToHex(test_value))})`);
    console.log(`Input value (${test_value}) => binary (${Float32ToBin(test_value)}) [${Float32ToBin(test_value).length} bits] => float32 (${BinToFloat32(Float32ToBin(test_value))})`);

    /* DEBUG OUTPUT:
    Input value (1.3) => hex (3fa66666) [4 bytes] => Float32 (1.2999999523162842)
    Input value (1.3) => binary (00111111101001100110011001100110) [32 bits] => Float32 (1.2999999523162842)
    */