Convert decimal to hexadecimal code

Source: Internet
Author: User

 

 
5621 the method for converting to a hexadecimal number is the "except for the remainder of 16" method, 5621/16 = Shang 351 more than 5, the last digit of the hexadecimal system is 5 351/16 = Shang 21 more 15, the second digit on the right of the hexadecimal system is f 21/16 = Shang 1 more than 5, and the third digit on the right of the hexadecimal system is 5 1/16 = Merchant 0 more than hexadecimal number the fourth to the right is 1 merchant 0 will not continue
 
The final result is 15f5.
# Include <iostream> # include <string> using namespace STD; // convert an integer to a string int2str (INT num); int main (INT argc, char * argv []) {// convert decimal to hexadecimal int num = 0; cout <"Please input a num" <Endl; CIN> num; string str_num; while (Num/16! = 0) {int A = num/16; int mod_a = num % 16; cout <"mod_a =" <mod_a <Endl; string s; Switch (mod_a) {case 10: S = 'a'; break; Case 11: S = 'B'; break; Case 12: S = 'C'; break; Case 13: S = 'D'; break; Case 14: S = 'E'; break; Case 15: S = 'F'; break; default: S = int2str (mod_a ); break;} str_num = S + str_num; num = A;} If (Num % 16! = 0) {int last_a = num % 16; str_num = int2str (last_a) + str_num;} cout <"the hex num =" <str_num <Endl; return 0;} string int2str (INT num) {If (num = 0) Return "0"; string STR = ""; int num _ = num> 0? Num:-1 * num; while (Num _) {STR = (char) (Num _ % 10 + 48) + STR; num _/= 10 ;} if (Num <0) STR = "-" + STR; return STR ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.