Conversion between hexadecimal notation

Source: Internet
Author: User
// Convert a hexadecimal character into a decimal number unsigned char cconvnumsdlg: btoh (char ch) {// 0-9if (CH> = '0' & Ch <= '9') Return (CH-'0 '); // 9-15if (CH> = 'A' & Ch <= 'F') Return (CH-'A' + 0xa ); // 9-15if (CH> = 'A' & Ch <= 'F') Return (CH-'A' + 0xa); Return (255 );} // convert the hexadecimal format to binary cstring cconvnumsdlg: hextobinary (cstring strhex) {int nlenth = strhex. getlength (); char * hex = new char [nlenth]; hex = strhex. getbuffer (0); cstring strbinary = ""; for (INT I = 0; I <nlenth; I ++) {// convert a hexadecimal number to a decimal char H = hex [nLenth-1-i]; Int J = btoh (h); cstring STR; Str. format ("% d", J); // convert the decimal format to 4 as the binary STR = decimaltobinary (STR); strbinary ++ = STR;} return strbinary ;} // convert binary to hexadecimal cstring cconvnumsdlg: binarytohex (cstring strbinary) {int nlength = strbinary. getlength (); cstring STR = strbinary; // when the number of digits is not a multiple of four, the switch (nlength % 4) {Case 0: break; Case 1: strbinary. format ("% d % s", 0, 0, STR); break; Case 2: strbinary. format ("% d % s", 0, STR); break; Case 3: strbinary. format ("% d % s", 0, STR); break; default: Return ""; break;} cstring strhex, str1; str1 = ""; nlength = strbinary. getlength (); For (INT I = 1; I <= (nlength/4); I ++) {// convert every four bits to a hexadecimal number STR = strbinary. left (4); cstring strdecimal = binarytodecimal (STR); int ndecimal = atoi (strdecimal. getbuffer (0); If (ndecimal <10) str1.format ("% d", ndecimal); else {char c = 'A' + (nDecimal-10 ); str1.format ("% C", c);} strhex + = str1; strbinary = strbinary. right (strbinary. getlength ()-Str. getlength () ;} return strhex;} // convert the decimal value to the binary cstring cconvnumsdlg: decimaltobinary (cstring strdecimal) {int ndecimal = atoi (strdecimal. getbuffer (0); int nyushu; // remainder int nshang; // operator cstring strbinary = ""; char buff [2]; cstring STR = ""; bool bcontinue = true; while (bcontinue) {nyushu = ndecimal % 2; nshang = ndecimal/2; sprintf (buff, "% d", nyushu); STR = strbinary; strbinary. format ("% S % s", buff, STR); ndecimal = nshang; If (nshang = 0) bcontinue = false;} return strbinary ;} // convert the binary value to the decimal cstring cconvnumsdlg: binarytodecimal (cstring strbinary) {int nlenth = strbinary. getlength (); char * binary = new char [nlenth]; binary = strbinary. getbuffer (0); int ndecimal = 0; For (INT I = 0; I <nlenth; I ++) {char H = binary [nLenth-1-i]; char STR [1]; STR [0] = H; Int J = atoi (STR); For (int K = 0; k <I; k ++) {J = J * 2 ;} ndecimal + = J;} cstring strdecimal; strdecimal. format ("% d", ndecimal); Return strdecimal ;}

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.