c++ - Converting binary to hex: error only on 2-7 -
i'm converting 4digit binary single digit hex using basic switch-case statement , code not run digits 0010-0111 reason , have no idea why.
here have:
void binhex() { int binin; cout << "enter binary(####): " << endl; cin >> binin; switch(binin){ case 0000: cout << "hex: 0" << endl; break; case 0001: cout << "hex: 1" << endl; break; case 0010: cout << "hex: 2" << endl; break; ... } } all numbers 0,1,8-15 work fine middle numbers not. ideas on causing error out/not run?
this case:
case 0010: cout<<"hex: 2\n"; break; will not fire binin == 10. fire binin == 8, because 0010 octal literal. drop leading 0s value gets interpreted decimal literal instead.
Comments
Post a Comment