Retrieve the digits from a hexadecimal DataFrame using parseInt() and ParseFloat() functions

When dealing with sensor data in hexadecimal format like '450934644ad7a38441', I need to extract the last 8 characters which represent the temperature value and convert it into a readable number. I attempted using an online hex converter tool from .

The conversion on this website suggests that the desired number falls under the Float - Little Endian (DCBA) system, while another UINT32 - Big Endian (ABCD) system is used for the process.

I am struggling to convert this number into float as parseFloat() results in NaN. Any guidance would be greatly appreciated.

var data = '450934644ad7a38441';

function main(data) {

  var result = [
    {      
        "key": "temperature",
        "value": parseInt('0x'+data.substring(10))
    }
    ]
  
  
  return result;
}

console.log(main(data));
// expected output: Array [Object { key: "temperature", value: 16.58 }]

Answer №1

If you are looking for a solution, I believe this could be helpful. The function yourStringToFloat takes the string input and converts it to float, using the data variable passed from the main function in your example. Feel free to adjust the precision as needed.

function flipHexString(hexValue, hexDigits) {
  var h = hexValue.substr(0, 2);
  for (var i = 0; i < hexDigits; ++i) {
    h += hexValue.substr(2 + (hexDigits - 1 - i) * 2, 2);
  }
  return h;
}


function hexToFloat(hex) {
  var s = hex >> 31 ? -1 : 1;
  var e = (hex >> 23) & 0xFF;
  return s * (hex & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, (e - 127))
}

function yourStringToFloat(str){

    return hexToFloat(flipHexString("0x"+str.substr(10), 8))

}

console.log(yourStringToFloat('450934644ad7a38441'));

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

My JSON unexpectedly returned a null value where there should have been valid data

Presented below is the PHP code I am working with: $query = "SELECT * FROM pro_artukel"; $result = mysql_query($query) or die('Errorquery: '.$query); $rows = array(); while ($r = mysql_fetch_assoc($result)) { $rows[] = $r; } $data = "{p ...

What is the best method for displaying the total in datatable?

I am encountering a slight issue while working on creating a table using Datatable and Bootstrap 4. Everything seems to be functioning well, but I have hit a snag. I am attempting to add a totalizer so that the sum of one or more columns is displayed at th ...

Continue reading from a file until a particular set of characters is encountered

I have a JSON file structured as follows: { "id": 25, "type": 0, "date": "Aug 28, 2017 12:14:28 PM", "isOpen": true, "message": "test" } /* Some lines of comments here, not part of the JSON */ My goal is to read from the file until I re ...

A guide on utilizing Retrofit and GSON to effectively parse data from the WordPress REST API

I'm facing an issue with parsing a list of data from this JSON structure! Here is the JSON data I am working with: [ { "id": 17502, "link": "https://www.angrybirds.com/blog/get-ready-angry-birds-movie-2-premiere-new-game-events/" ...

Creating an interactive dropdown menu with simple_form

I am new to JS/Ajax and unsure how to render dynamic options based on previously inserted data in the same form. Background: The bike shop chain application allows the renting of bikes with names like "bike 1" or "yellow bike" ('bikes'), by sele ...

Embed PHP script within the PrettyPhoto.js file

Looking to integrate a piece of PHP code into the prettyPhoto.js file. This is the snippet I'm currently working with. The section where I need to insert the PHP code is marked as ((CODE ME HERE)). (function($) { $.prettyPhoto = {version: ' ...

In SwiftUI, the List component is displaying only the initial item from the JSON file

Looking for help understanding SwiftUI and JSON! I am able to read and print the json file, but when I try to create a List, it only displays the first element. What could be causing this issue? struct ContentView: View { var fetch = Fetch() p ...

Issue with Textarea not updating when props change in a React component

I am facing an issue with updating the default value of a textarea based on props passed from a parent component. Strangely, the update works when using 'value' but not when using 'defaultValue'. However, I need the textarea to be edita ...

Incorporating Redis within an Express route

My goal is to store a value in a specific key in one route: /api/foo?redisKey="1" (setting value for the key id=1) Then, in another route, I want to retrieve the value: /api/bar?redisKey="1" (getting value for key id=1) Since redis operates asynchronou ...

Decompress data using JSON

I received a JSON string from the OMDb API and now I am trying to deserialize it into a list of objects. However, I encountered the following error: Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON object (e.g. {"name":"va ...

What could be causing jQuery's done() function to display the success message repeatedly instead of just once?

Using jQuery and AJAX, I am processing my HTML form. The function is triggered by the 'change()' event in jQuery. When the request is successful, it displays the message: Successfully Updated However, if another request is made, it displays: S ...

Is there a way for a userscript to receive notifications about dynamic page changes triggered by ajax?

Looking at a webpage that dynamically updates items via AJAX calls, how can a userscript detect these changes as they occur? Let's take the example of a social media newsfeed like Facebook. Initially, there are 12 items displayed within <li> ta ...

Struggling to access authorization headers on an express backend?

As I work on developing my MERN app, I encountered an issue with fetching data from an API within the useEffect hook. Despite setting the authorization token in the request, I found that I couldn't retrieve it from the headers in the backend. useEffec ...

Implementing Blob image rendering in Vue.js from a database

In my front-end development, I am utilizing Vue.js. On the backend, I have set up Node.js, Express, and PostgreSQL with Sequelize. One of the challenges I encountered involved storing an item in the database that includes a thumbnail image. Database Model ...

I'm having issues integrating AngularJS with Django. Can anyone provide guidance on how to effectively use both technologies together

I am facing an issue where I have an HTML file containing AngularJS code, along with a backend file. Despite the URL working correctly, I am encountering a problem where I am unable to use {{ }} in the HTML. What could be the reason for this? Additionall ...

Find and locate items in a checklistbox by typing text into a textbox

I need a search function inside a checklist box. The idea is that when a user types text into a text field, if that particular item exists in the checkbox list, it should automatically be selected. HTML: <asp:TextBox ID="TextBox1" runat="server" OnTex ...

Add or delete elements in a hyperlink

Currently, I'm utilizing to facilitate the filtering of various items on a website. The menu needs to function as a "build up" system where selecting one category filters to that specific category, clicking on another category adds it to the existing ...

Leverage the power of React by utilizing SVGR to easily integrate SVG files

Wondering if there's a way to bring in an SVG file from my public folder and use it as a React component like this: import { ReactComponent as MySvg } from '/assets/svg/mysvg.svg'; const MyComponent = () => { return ( <div> ...

Discovering various lists of objects in Android using Retrofit

My aim is to retrieve various types of List data using Retrofit. I have created a List and added data to it. Can someone guide me on how to fetch Different Types of Model Data? When I attempted to add Object List to my Custom model list, I encountered thi ...

What could be causing my shape to not appear when I alter the variable name in three.js?

Recently, I encountered an interesting issue with some code I found on a tutorial website. The code worked perfectly when I used the variable 'canvas', but when I changed it to something else like 'canvas2', it caused unexpected behavio ...