Float in Int Speichern
-
Hi,
wie schaffe ich es die binäre Repräsentation einer Float (IEEE ...) Variable in einer Int Variablen zu speichern, so dass der gespeicherte Float Wert aus dem Int Wert wieder hergestellt werden kann?greets -Caster-
-
float foo = /*blubb*/; int bar = reinterpret_cast<int>(foo);
-
Unter der Annahme, dass float und int gleichgroß sind.
-
Code:
float f = 3424.44f; int tmp = reinterpret_cast<int>(f);
Visual C++ 2005 Express Beta 1 spuckt dabei aber folgendes aus:
error C2440: 'reinterpret_cast' : cannot convert from 'float' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
-
1. Willst du das Bitmuster speichern oder den Wert?
2. Bei Bitmuster:float f; unsigned int i = *(unsigned int *)&f;
Bei Wert:
float f; int i = f;