Decimal conversion to 16-in and 16-to-2 implementation of the system

Source: Internet
Author: User
Tags integer printf sprintf
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main () {I
    NT I,v;
    Char bs[33];
    Char b[33];
    Char hs[9];
    Char h[9];
    Char s[4];

Char *e;
    Decimal integer to binary string; i=1024;
    Ltoa (i,b,2);
    sprintf (BS, "%032s", b);
printf ("i=%d,bs=%s\n", I,bs);
    Decimal integer to hexadecimal string; i=1024;
    Ltoa (i,h,16);
    sprintf (HS, "%08s", h);
printf ("i=%d,hs=%s\n", I,HS);
    hexadecimal string to decimal number strcpy (HS, "00000400");
    SSCANF (HS, "%x", &i);
printf ("hs=%s,i=%d\n", hs,i);
    Binary strings are converted to hexadecimal strings; strcpy (BS, "00000000000000000000010000000000");
    I=strtol (bs,&e,2);
    Ltoa (i,h,16);
    sprintf (HS, "%08s", h);
printf ("bs=%s,hs=%s\n", BS,HS);
    Binary strings are converted to decimal numbers; strcpy (BS, "00000000000000000000010000000000");
    I=strtol (bs,&e,2);
printf ("bs=%s,i=%d\n", bs,i);
    hexadecimal strings are converted into binary strings strcpy (HS, "00000400");
    SSCANF (HS, "%x", &i);
    Ltoa (i,b,2);
    sprintf (BS, "%032s", 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);
hexadecimal strings are converted into Chinese characters (GBK) and characters (ASC) strcpy (HS, "61BABA");
    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=61baba//hs=61baba,s=a------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 more applicable to the fragmented fetch bit state or byte state/#define GETHEX ((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;
 }

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.