getch and arrow codes

By pressing one arrow key getch will push three values into the buffer: ‘\033’ ‘[‘ ‘A’, ‘B’, ‘C’ or ‘D’ So the code will be something like this: if (getch() == ‘\033’) { // if the first value is esc getch(); // skip the [ switch(getch()) { // the real value case ‘A’: // code … Read more

Moving decimal places over in a double

If you use double or float, you should use rounding or expect to see some rounding errors. If you can’t do this, use BigDecimal. The problem you have is that 0.1 is not an exact representation, and by performing the calculation twice, you are compounding that error. However, 100 can be represented accurately, so try: … Read more

Integer to hex string in C++

Use <iomanip>‘s std::hex. If you print, just send it to std::cout, if not, then use std::stringstream std::stringstream stream; stream << std::hex << your_int; std::string result( stream.str() ); You can prepend the first << with << “0x” or whatever you like if you wish. Other manips of interest are std::oct (octal) and std::dec (back to decimal). … Read more

Decimal precision and scale in EF Code First

The answer from Dave Van den Eynde is now out of date. There are 2 important changes, from EF 4.1 onwards the ModelBuilder class is now DbModelBuilder and there is now a DecimalPropertyConfiguration.HasPrecision Method which has a signature of: public DecimalPropertyConfiguration HasPrecision( byte precision, byte scale ) where precision is the total number of digits … Read more

How to deal with big numbers in javascript [duplicate]

While looking for an big integer library for an ElGamal crypto implementation I tested several libraries with the following results: I recommend this one: Tom Wu’s jsbn.js (http://www-cs-students.stanford.edu/~tjw/jsbn/) Comprehensive set of functions and fast Leemon Baird’s big integer library (http://www.leemon.com/crypto/BigInt.js) Comprehensive set of functions and pretty fast BUT: Negative number representation is buggy! bignumber.js (https://github.com/MikeMcl/bignumber.js) … Read more

Remove trailing zeros

I ran into the same problem but in a case where I do not have control of the output to string, which was taken care of by a library. After looking into details in the implementation of the Decimal type (see http://msdn.microsoft.com/en-us/library/system.decimal.getbits.aspx), I came up with a neat trick (here as an extension method): public … Read more