Saturday, September 10, 2016

How to fix the uncontrollable mouse when playing Minecraft on VMware Player

When playing Minecraft on VMware Player's guest OS, the game and the host OS both try to grab the mouse. Hence when you move the mouse, the scene of the game moves wildly.

One trick to fix that is to disconnect the mouse from the host OS so that only the game will control the mouse.

Step 1: before starting the VM image, change its settings: Hareware -> USB Controller -> Check the checkbox of Show all USB input devices.

Step 2: Starts the VM image. After it is booted, find in the top right corner the icon that represents the mouse. Right click it and choose to Connect (Disconnect from host).

Step 3: Enjoy the game.

Step 4: When finish playing, shut down the guest OS and the host OS will gain back the control of the mouse.

Tuesday, April 26, 2016

C++: Convert a string to its hex form

#include <sstream>
#include <iomanip>


std::string toHex(const std::string &input)
    std::stringstream ss;
    ss << hex;

    for (size_t i = 0; i < input.length(); i++)
        ss << setw(2) << setfill('0') 
           << (unsigned short)(input[i] & 0xff);

    return ss.str();

Wednesday, March 30, 2016

C++ get array size with template

#include <iostream>

using namespace std;

template <typename T, unsigned int S>
unsigned int array_size(const T (&)[S])
    return S;

int main()
    char a[] = "Hello, world!";
    unsigned int s = array_size(a);
    cout << s << endl;
    return 0;

The output of the program is 14.

T(&)[S] is the type of the passed in parameter, i.e. T (&in)[S], where "in" is the reference of the array "a[14]" in our case. When doing the type deduction, the compiler will match T with char and S with 14. 
Get This <