Convert decimal to hexadecimal, hexadecimal to binary

Source: Internet
Author: User

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <locale. h> int main () {int I, v; char bs [33]; char B [33]; char hs [9]; char h [9]; char s [4]; char * e; // convert a decimal integer to a binary string; I = 1024; ltoa (I, B, 2); sprintf (bs, "% 032 s", B); printf ("I = % d, bs = % s \ n", I, bs); // convert a decimal integer to a hexadecimal string; I = 1024; ltoa (I, h, 16); sprintf (hs, "% 08 s", h); printf ("I = % d, hs = % s \ n ", I, hs); // convert the hexadecimal string to the decimal number strcpy (hs," 00000400 "); sscanf (hs," % x ", & I); printf ("hs = % s, I = % d \ n", hs, I); // convert a binary string to a hexadecimal string; strcpy (bs, "00000000000000000000010000000000"); I = strtol (bs, & e, 2); ltoa (I, h, 16); sprintf (hs, "% 08 s", h ); printf ("bs = % s, hs = % s \ n", bs, hs); // convert a binary string to a decimal number; strcpy (bs, "00000000000000000000010000000000 "); I = strtol (bs, & e, 2); printf ("bs = % s, I = % d \ n", bs, I ); // convert the hexadecimal string to the binary string strcpy (hs, "00000400"); sscanf (hs, "% x", & I); ltoa (I, B, 2 ); sprintf (bs, "% 032 s", B); printf ("hs = % s, bs = % s \ n", hs, bs ); // ASC \ GBK string to hexadecimal string strcpy (s, "a Han"); I = 0; while (1) {if (0 = s [I]) break; sprintf (hs + I * 2, "% 02X", (unsigned char) s [I]); I ++;} setlocale (LC_ALL, "chs "); printf ("s = % s, hs = % s \ n", s, hs );
// Convert the hexadecimal string into Chinese characters (GBK) and characters (ASC) strcpy (hs, "61 BABA"); I = 0; while (1) {if (1! = Sscanf (hs + I * 2, "% 2x", & v) break; s [I] = (char) v; I ++ ;} s [I] = 0; printf ("hs = % s, s = % s \ n", hs, s); return 0 ;}// I = 1024, bs = 00000000000000000000010000000000 // I = 1024, hs = 00000400 // hs = 00000400, I = 1024 // bs = 00000000000000000000010000000000, hs = 00000400 // bs = 00000000000000000000010000000000, I = 1024 // hs = 00000400, bs = 00000000000000000000010000000000 // s = a Han, hs = 61 BABA // hs = 61 BABA, s = a Han ------ solution ------------------ # include <stdio. h> union decompose {int integer; struct {unsigned int hex0: 4; unsigned int hex1: 4; unsigned int hex2: 4; unsigned int hex3: 4; unsigned int hex4: 4; unsigned int hex5: 4; unsigned int hex6: 4; unsigned int hex7: 4;} hex4 ;};/* This macro definition cannot be used for loops, that is, n cannot be a variable, it can only be a number *. Therefore, it is applicable to obtaining bit or byte status from scattered locations */# define GETHEX (x, n) (union decompose ){. integer = x }). hex4.hex # n) void test_getx (int tmp) {printf ("% X", GETHEX (tmp, 0); printf ("% X", GETHEX (tmp, 1); printf ("% X", GETHEX (tmp, 2); printf ("% X", GETHEX (tmp, 3 )); printf ("% X", GETHEX (tmp, 4); printf ("% X", GETHEX (tmp, 5); printf ("% X ", GETHEX (tmp, 6); printf ("% X \ n", GETHEX (tmp, 7);} int main () {test_getx (100); return 0 ;}

MFC hexadecimal to binary


16 to 10

// Convert 16 to 10: int CCIecClientSimulator12Dlg: HexToDec (char * s) {char * p = s; // Null String returns 0. If (* p = '\ 0') return 0; // ignore the '0' character starting with while (* p = '0') p ++; int dec = 0; char c; // loop until the end of the string. While (c = * p ++) {// dec multiplied by 16dec <= 4; // number character. If (c> = '0' & c <= '9') {dec + = c-'0'; continue;} // lower case abcdef. If (c> = 'A' & c <= 'F') {dec + = c-'A' + 10; continue;} // upper case ABCDEF. If (c> = 'A' & c <= 'F') {dec + = c-'A' + 10; continue ;} // if the statement does not end with any if statement, it indicates that an invalid character is encountered. Return-1;} // returns a 10-digit integer when the loop ends normally. Return dec ;}

10 to 2

UINT CCIecClientSimulator12Dlg::to2( int n ){int r=0;//double r=0;double b =10;int shang,yushu;shang=n;int i=0;while(shang!=0){yushu=shang%2;r=r+pow(b,i)*yushu;i++;shang=shang/2;}return r;}



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.