https://www.codeproject.com/Answers/246137/How-to-convert-double-to-BSTR#answer1
https://www.codeproject.com/Answers/484217/Convertplusfloatplustoplusbinary#answer2
浮点数,在内存中,本来就是二进制的,直接把二进制打印出来就可以了。
Float is already binary, period.
Everything is binary, but your result is "much less binary", because what you right into console is a string or a chain of characters, a human-readable representation of the binary. Let's assume this is what you really need, by some reason. It's still binary in that sense that your '1' or '0' characters are represented in memory as Unicode code points represented as 16-bit UTF-16LE words: 0x30 and 0x31.
The bits of any data type of the size no more then the size of of System.Int32
can be found in this simplified way. Something like this:
Hide Copy Code
static string ToBinaryString(float value) {
int bitCount = sizeof(float) * 8; // never rely on your knowledge of the size
char[] result = new char[bitCount]; // better not use string, to avoid ineffective string concatenation repeated in a loop
// now, most important thing: (int)value would be "semantic" cast of the same
// mathematical value (with possible rounding), something we don't want; so:
int intValue = System.BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
for (int bit = 0; bit < bitCount; ++bit) {
int maskedValue = intValue & (1 << bit); // this is how shift and mask is done.
if (maskedValue > 0)
maskedValue = 1;
// at this point, masked value is either int 0 or 1
result[bitCount - bit - 1] = maskedValue.ToString()[0]; // bits go right-to-left in usual Western Arabic-based notation
}
return new string(result); // string from character array
}
If the case of arbitrary-size data, you need to serialize to bytes using System.BitConverter
and work with individual bytes, in two nested loops.
You forgot to include the integer part (e.g. 18
) inside the mantissa.
That is, of the Wikipedia recipe:
Here we can show how to convert a base 10 real number into an IEEE 754 binary32 format using the following outline :
consider a real number with an integer and a fraction part such as 12.375
you missed step 1.
I changed the mantisse
method to include such part (just as proof: you may find a way more 'in tune' with your programming style).
//Methode zur Berechnung der Mantisse
public void mantisse(float werte)
{
int ganzzahlteil = (int)werte;
Single kommawert = werte - ganzzahlteil;
int norm = 1;
int mask = 1;
int ganzzahlteil_copy = ganzzahlteil;
while ((ganzzahlteil_copy >>= 1 ) > 1)
{
norm++;
mask <<= 1;
}
for(int i=0; i
C++
#include
#include
#include
using namespace std;
int main()
{
wostringstream wos;
double d = 0.57;
wos << d;
BSTR bstr = SysAllocString(wos.str().c_str());
// enjoy with bstr
// ...
SysFreeString(bstr);
}
#include "windows.h"
#include "comutil.h"
void your_func(BSTR bstr);
int main()
{
_bstr_t str(_variant_t(double(123.456)));
// str is castable to BSTR
// use it like this
your_func(str);
// or simply
your_func(_bstr_t(_variant_t(double(123.456))));
return 0;
}
Why not use VarBstrFromR8[^] Windows API?
https://blog.csdn.net/nxnxfly/article/details/82795241
#include "stdafx.h"
int Float2Binary(const double src, char* dest, int* len)
{
int intNum = 0;
double floatNum = 0.0f;
int i= 0,j=0;
int tmp = 0;
bool record = false;
intNum = (int)src;//取整部分
floatNum = intNum == 0 ? src : src-intNum; //取小数部分
for(i=63;i>=0;i--) { //转整数部分
tmp = (int)(intNum>>i&1);
if (tmp || record){ //tmp第一次出现1时record = true
dest[j] = tmp +'0';
j++;
record = true;
}
}
if (!record){ //整数部分等于0
dest[j] = 0 +'0';
j++;
}
dest[j] = '.';
j++;
if (!floatNum){ //小数部分等于0
dest[j]=0;
return 1;
}
intNum = 0;
while(floatNum){ //转小数部分
floatNum = floatNum*2;
intNum = (int)(floatNum);
if (intNum<0)
dest[j]=intNum+2+'0'; //负整数-1转为正整数1
else
dest[j]=intNum+'0';
floatNum = floatNum - intNum;
j++;
};
*len = j;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
double src = 8.124986653231321313131443523523;
int a = (int)src;
printf("%d\n",a);
printf("%f\n\n",src-a);
int len = 0;
char dest[256]={0};
Float2Binary(src,dest,&len);
for (i=0; i
}
getchar();
return 0;
}