That may sound like a weird struggle and actually easy to do, but I cannot find a working way to convert an hexidecimal in a string format into a float.
My exemple is for instance: 406ea716
If I convert it using one of the following website, I get 3.728948.
http://www.h-schmidt.net/FloatConverter/IEEE754.html
http://gregstoll.dyndns.org/~gregstoll/floattohex/
I tried every single piece of code I found on the internet, but it won't return the same result.
Does it exist a module in NodeJS to perform the same conversion? If not, what can I do?
Thank you for your help.
I had the same issue. try this.
Buffer('406ea716','hex').readFloatBE(0)
3.7289481163024902
No need for a module:
var hex = '406ea716';
// transform the hexadecimal representation in a proper js hexadecimal representation by prepending `0x` to the string
// parseInt() - because your example was an integer.
var num = parseInt( '0x' + '406ea716');
console.log( num );
Have you tried parseInt?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
$ node
> parseInt('406ea716', 16)
1080993558
Related
what is the simplest way to print the result as follows using pyhton3
I have a Hex string s="FFFC"
In python if using this command line: print(int(s,16))
The result I'm expecting is -4 (which is in signed format). But this is not the case, It displays the Unsigned format which the result is 65,532.
How can I convert this the easiest way?
Thank you in advance.
There are several ways, but you could just do the math explicitly (assuming s has no more than 4 characters, otherwise use s[-4:]):
i = int(s, 16)
if i >= 0x8000:
i -= 0x10000
You can use the bytes.fromhex and int.from_bytes class methods.
s = bytes.fromhex('FFFC')
i = int.from_bytes(s, 'big', signed=True)
print(i)
Pretty self-explanatory, the only thing that might need clarification is the 'big' argument, but that just means that the byte array s has the most significant byte first.
How do i convert hex values to their decimal value using nodejs ?
Suppose i have hex value as bellow
9c63e8e2f6574c197c0626bad843eb47104adf3f01f2901aad1258936feb007e
Any one have any idea the please let me know
JS parseInt function takes base as a second argument.
So you can simply use parseInt(hexString, 16).
For example: parseInt('ff', 16) will return 255.
I'm bothering you because I'm looking to convert a string into a float with node.JS.
I have a string that comes back from a request and looks like:
x,xxx.xxxx (ex. 4,530.1200) and I'd like to convert it to a float: xxxx.xxxx (or 4530.1200).
I've tried to use:
parseFloat(str) but it returns 4. I also found Float.valueOf(str) on the web but I get a ReferenceError: Float is not defined error.
You can use string replace to solve this kind of problem.
str = '4,530.1200';
parseFloat(str.replace(/,/g, ''));
This will remove all the , from your string so that you can convert it to the float using parseFloat.
Notice the g flag in regex, it represents global, by using this you are specifying to remove all the matched regex.
I have this hexadecimal in a nodejs Tcp Client
82380000000000000400000000000000
I have used parseint function to convert it to
1.73090408076117e+38
But i need to get its binary representation that is
10000010001110000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000
Ho can i get this binary representation from the above hexadecimal format?
Using the toString specifying the base as the parameter should make it. For example, if you want to convert it to a binary string, after parsing it to an integer:
var number = '82380000000000000400000000000000';
console.log(parseInt(number).toString(2));
In case you want an hexadecimal string, just use .toString(16);
If you don't want to parse the string with the number:
var number = 82380000000000000400000000000000;
console.log(number.toString(2));
Hope this helps.
I have a string in which the data is getting loaded in this format. "float;#123456.0300000" from which i need to extract only 123456.03 and trim all other starting and ending characters. May be it is very basic that i am missing, please let me know how can i do that. Thanks.
If the format is always float;# followed by the bit you want, then it's fairly simple:
// TODO: Validate that it actually starts with "float;#". What do you want
// to do if it doesn't?
string userText = originalText.Substring("float;#".Length);
// We wouldn't want to trim "300" to "3"
if (userText.Contains("."))
{
userText = userText.TrimEnd('0');
// Trim "123.000" to "123" instead of "123."
if (userText.EndsWith("."))
{
userText = userText.Substring(0, userText.Length - 1);
}
}
But you really need to be confident in the format - that there won't be anything else you need to remove, etc.
In particular:
What's the maximum number of decimal places you want to support?
Might there be thousands separators?
Will the decimal separator always be a period?
Can the value be negative?
Is an explicit leading + valid?
Do you need to handle scientific format (1.234e5 etc)?
Might there be trailing characters?
Might the trailing characters include digits?
Do you need to handle non-ASCII digits?
You may well want to use a regular expression if you need anything more complicated than the code above, but you'll really want to know as much as you can about your possible inputs before going much further. (I'd strongly recommend writing unit tests for every form of input you can think of.)
Why not just do:
String s = "float;#123456.0300000";
int i = int.Parse(s.Substring(s.IndexOf('#') + 1));
I haven't tested this, but it should be close.
Assuming the format doesn't change, just find the character after the '#' and turn it into an int, so you lose everything after the '.'.
If you need it as a string, then just call the ToString method.