1

I have this function in php that would convert hexadecimal to 32 float in php. How do you do it in Javascript

public static function hexTo32Float($val)
{
    $packed=pack("H*",$val);
    $reversedpac=strrev($packed);
    $unpack=unpack( "f",$reversedpac);

    return array_shift($unpack);
}
13
  • well i have tried this which i found somewhere here but id didn't work. return parseInt("0x"+hexNumber); Commented Jan 6, 2014 at 11:06
  • possible duplicate of Converting hexadecimal to float in javascript Commented Jan 6, 2014 at 11:08
  • @DeepakBhattarai this is the same approach as my answer. What exactly was the error? Commented Jan 6, 2014 at 11:09
  • @Callahan if we convert 0x8c42465d the float should be around 49.something Commented Jan 6, 2014 at 11:15
  • @DeepakBhattarai Are you sure that hexNumber did not already contain a 0x prefix? In that case the output would have been 0 for your input. Commented Jan 6, 2014 at 11:16

2 Answers 2

2

What about playing with TypedArray (that will only work with recent browsers) ?

var intData = Uint32Array(1);
intData[0] = parseInt("42458c14", 16);
var dataAsFloat = new Float32Array(intData.buffer);
var result = dataAsFloat[0];
// result == 49.38679504394531

EDIT (one year later ...) : It does seem that the result could depend on whether or not your CPU is big indian or little indian. Be careful when using this.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks this works too. but which one do i choose @callahan's one or your?
Well this one is only a bit longer. You should just take the version that you like more to see in your code.
@callahan's answer uses the Buffer object which doesn't seems to be a part of html5. If you're doing it in a browser, you should prefer my version. If you're using Node.js, his solution is better.
I will keep both."Looks like i have to call two of the functions randomly"
The node.js thing is a good point. Never coded javascript, did not think of that.
0

After a complete BS answer try the following I found in the answer to this question:
var b = new Buffer(hexNumber, 'hex').readFloatBE(0).
edit: The fault was the usage of readFloatLE here.
For further explanation: The point here is that readFloatXX() expects buffer contents as two's complement signed values. I guess readFloatLE is the version for unsigned floats (could not find that in the docs, though).

4 Comments

@DeepakBhattarai Please try this one. I undeleted my answer and dont know if you get notified for this.
I did this too, but it doesn't convert '42458c14' to 49.something which is what i need
@DeepakBhattarai There seems to be a readFloatBE version too. Maybe try that.
well that works fine. edit your answer so that i can mark you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.