[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppBuilderDoubleToBitString

(C++ Builder) Converting a double to a String of bits

The example below shows the code for converting a double to a String. It shows that 0.0 is not equal to -0.0 in a TStringGrid.

  1. include <assert>
String doubleToBitString(const double& anyDouble) { static union MyUnion { unsigned int myInts[2]; double myDouble; } myUnion; //assert(sizeof(myUnion.myInts) == sizeof(myUnion.myDouble)); myUnion.myDouble = anyDouble; unsigned int myInt0 = myUnion.myInts[0]; unsigned int myInt1 = myUnion.myInts[1]; String myBitString; for (unsigned int i = 0; i<32; ++i) { if (myInt0%2==0) myBitString = "0" + myBitString; else myBitString = "1" + myBitString; myInt0>>=1; } for (unsigned int i = 0; i<32; ++i) { if (myInt1%2==0) myBitString = "0" + myBitString; else myBitString = "1" + myBitString; myInt1>>=1; } return myBitString; } void __fastcall TForm1::Button1Click(TObject *Sender) { const String bitStringOne = doubleToBitString(1.0); const String bitStringZero = doubleToBitString(0.0); const String bitStringMinusZero = doubleToBitString(-0.0); const String bitStringMinusOne = doubleToBitString(-1.0); for (int y=0; y<64; ++y) { StringGrid1->Cells[0][y+1]=y; StringGrid1->Cells[1][y+1]=bitStringMinusOne[y+1]; //AnsiString starts at index 1 StringGrid1->Cells[2][y+1]=bitStringMinusZero[y+1]; StringGrid1->Cells[3][y+1]=bitStringZero[y+1]; StringGrid1->Cells[4][y+1]=bitStringOne[y+1]; }


Topic links





last edited (November 16, 2005) by bilderbikkel, Number of views: 1389, Current Rev: 2 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.