Is there a way to translate IBM floating point data into JavaScript numbers?

I am interested in extracting data from a legacy file format that utilizes IBM floating point numbers. These numbers were commonly used on the IBM System/360 mainframe computer system. Now, I am hoping to leverage this information within a JavaScript program.

What is the most efficient method for converting IBM floating point numbers into JavaScript-compatible values?

Answer №1

Here is a custom function that can take a buffer of little endian IBM floats and convert them into a numerical value:

function ibmToJavaScript(buffer) {
    var sign = buffer[0] >> 7
    var exponent = buffer[0] & 0x7f
    var fraction = 0
    function getBit(buffer, bit) {
        return buffer[Math.floor(bit / 8)] >> (7 - (bit % 8)) & 1
    }
    for (var i = 0; i < 24; i++) {
        fraction += getBit(buffer, 8 + i) / (2 << i)
    }
    return (1 - 2 * sign) * Math.pow(16.0, exponent - 64) * fraction
}

Please note that this implementation does not account for special cases like NaN or Infinity.

This code snippet was adapted from the source:

Answer №2

A minor adjustment to Hallgrim's solution that now includes support for double-precision floats:

function ibm2js(buffer) {
    var sign = buffer[0] >> 7
    var exponent = buffer[0] & 0x7f
    var fraction = 0
    function bit(buffer, bit) {
        return buffer[Math.floor(bit / 8)] >> (7 - (bit % 8)) & 1
    }
    var denom = 1
    var totalBit = (buffer.length - 1) * 8
    for (var i = 0; i < totalBit; i++) {
        denom = denom * 2
        fraction += bit(buffer, 8 + i) / denom
    }
    return (1 - 2 * sign) * Math.pow(16.0, exponent - 64) * fraction
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sorting and filtering data using AngularJS filter and orderBy inside a controller or factory

I am currently working with a factory that looks like this: app.factory("ModuleFactory", function (api, $http, $q, filterFilter) { var moduleList = []; var categoryList = []; var moduleTypeList = []; var academyModuleTypeList = []; var mostUsed = []; var ...

What level of security is involved in sending an ajax request to a Codeigniter controller?

I am looking to securely pass the response of an ajax request to my codeigniter application. The current approach I have is as follows: Javascript var dataString = { 'id' : id }; var submit = $.ajax({ url:$('#hiddenurl').v ...

Floating above a surface to manipulate a title

Wondering if it's possible to achieve this without JavaScript, but that would be the ideal scenario. Here's a snapshot of how my page is structured: ____________________________ | TITLE | |This is a fixed header. I wa ...

Generating JSON array objects dynamically in JavaScript code

Need help with organizing my code let newArr = []; $.post( "/reports/search", { query:'*'},function(data) { for(let i=0; i<data.length; i++) { newArr[i].value = data[i].name; newArr[i].data = data[i].id; } },'json ...

Utilizing a JSON file for long-term storage of a compact database in JavaScript

As a newcomer to JSON, I wanted my webpage to display a small database of records in a table without using a traditional database like MySQL. Instead, I decided to read data from and write it out to a JSON file for convenient and persistent storage on my w ...

Bringing in States and Functions to a React Component

Is there a way to improve the organization of states and functions within a functional React Component? Here's my current code structure: import React from 'react' //more imports... const Dashboard = () => { const [] = useState() / ...

core.js encountered an issue at line 6210: TypeError - The function this.service.addDepartment does not exist

Whenever I attempt to click the 'Add' button on a web application that I'm constructing, an error pops up. core.js:6210 ERROR TypeError: this.service.addDepartment is not a function at AddEditDepComponent.addDepartment (add-edit-dep.componen ...

Ways to prevent the repetition of keys associated with values

I am currently dealing with an array called serialNumbers, which can have different structures. For example: lot: 1 Serial: 2 lot: 1 Serial: 3 lot: 1 Serial: 4 ...or it could look like this: lot: 1 Serial: 5 lot: 1 Serial: 9 lot: 8 Serial: 2 lot: ...

Incorporating URL parameters into an HTML form using JavaScript or jQuery

I need to pass variables in the URL to populate an HTML and Liquid form and then submit it. Here is an example URL: http://www.example.com/[email protected] &customer_password=123456 I came across this question which is somewhat related to what ...

Demystifying Iron Ajax: Unraveling the process of parsing an array of JSON objects from a successful

When making an AJAX call to the server, I receive a response in the form of an array of objects as JSON. [{"dms":[{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":37.8688,"longitude":-144.2093,"toolType":1},{"serialNo":"EG0022","st ...

What is the significance of the "#" character in the URL of a jQuery mobile

As I encounter a strange issue with my jQuery mobile page, I notice that when accessing page.php everything looks good, but once adding #someDetailsHere to the URL, it only displays a blank white page. What could be causing this and how can I resolve it? ...

AngularJS modules are not loading dependencies such as constants or controllers

While searching for the reason why a properly defined factory is not loading, I came across this question. This led me to contemplate on the concept of services being "defined in an angular module". Consider this example: angular.module('d3', ...

What is the best way to stop a jQuery function from applying titles extracted from the first row th's in thead's to multiple tables in a document?

My circumstances: I am new to jQuery and Stack Overflow, seeking assistance for my website project. The challenge: Building a website using Bootstrap 3.3.6 with intricate data tables that need to be stacked dynamically on mobile devices using CSS. It is c ...

In Vue.js, the minus button will automatically disappear once the value reaches 0, and the plus button will reappear

I am trying to implement a functionality where, when the minus button is clicked and the value reaches 0, the minus button gets hidden and the plus button becomes visible again to add values. I have managed to make the minus and plus buttons work but nee ...

What is the best way to tally the frequency of words in a collection of URLs using JavaScript?

I have a JSON object in WordPress that contains a list of URLs. I would like to determine the frequency of occurrence of the second part of each URL. Currently, the code snippet below is able to extract the remaining part of the URL after the prefix https ...

Utilize JavaScript to dynamically deactivate hyperlinks during runtime

Utilizing Bootstrap <ul class="nav nav-pills nav-stacked col-sm-2 hidden" id="menu"> <li role="presentation" id="LiNewsFeed"><a href="javascript:GetNewsFeed();">News Feed</a></li> <li role ...

Combine two redux actions and access the modified state in the second action

I'm facing a dilemma with a straightforward problem that I believe I have a solution for, but I'm uncertain about the effectiveness of my idea. Essentially, in the current state of things, I have an array property that requires updating just bef ...

JavaScript validation function stopping after the first successful validation

Script: NewsletterValidation.js function formValidation() { var fname = document.getElementById('firstName').value; var lname = document.getElementById('lastName').value; var pnumber = document.getElementById('phoneNumb ...

unable to load variables in google extension due to technical difficulties

I'm encountering an error even though I've already loaded the DOM. I have no idea how to fix this issue and I've been sitting here for hours trying to troubleshoot. The error message I'm getting is: btncheck.js:10 Uncaught TypeError: C ...

Encountering an issue with JQuery when attempting to create a double dropdown SelectList. Upon submitting the POST request, the output received is always

Two dropdownlists have been created, where one acts as a filter for the other. When selecting a customer from the dropdown Customer, only a limited set of ClientUsers is displayed in the dropdown ClientUser. This functionality is achieved using a jQuery fu ...