Decimal conversion to 16 binary, 16 binary to 2 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;//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 converted to decimal number strcpy (HS, "00000400");    SSCANF (HS, "%x", &i);    printf ("hs=%s,i=%d\n", hs,i);//binary string converted to hexadecimal string; 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 string converted to decimal number; strcpy (BS, "00000000000000000000010000000000");    I=strtol (bs,&e,2);    printf ("bs=%s,i=%d\n", bs,i);//hexadecimal string is converted to binary string 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 hex 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);
The hexadecimal string is converted to kanji (GBK) and the character (ASC) strcpy (HS, "61BABA");    i=0;        while (1) {if (1!=sscanf (hs+i*2, "%2x", &v)) is 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 han------solution--------------------#include < stdio.h>union decompose{int integer;struct{unsigned int hex0:4;unsigned int hex1:4;unsigned int hex2:4;unsigned i NT 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, only can be a number * Therefore, it is more suitable to get bit state or byte state/#define GETHEX (x, N) ((union Decompose) {. Integer = x}). hex4.hex# #n ) void Test_getx (int tmp) {printf ("%x", Gethex (tmp, 0));p rintf ("%x", Gethex (TMP, 1));p rintf ("%x", Gethex (TMP, 2));p rintf ("%x", GETHEX (TMP, 3));p rintf ("%x", Gethex (TMP, 4));p rintf ("%x", Gethex (TMP, 5));p rintf ("%x", Gethex (TMP, 6));p rintf ("%x\n", Gethex (TMP, 7));} int main () {test_getx (+); return 0;}

MFC 16-in-Turn 2-binary


16 Go to 10

16 to 10 binary int cciecclientsimulator12dlg::hextodec (char *s) {char *p = s;//empty string returns 0. if (*p = = ') return 0;//ignores the beginning of the ' 0 ' character while (*p = = ' 0 ') p++;int Dec = 0;char c;//Loop Until the end of the string. while (c = *p++) {//dec by 16dec <<= 4;//numeric characters. if (c >= ' 0 ' && C <= ' 9 ') {Dec + = c-' 0 '; continue;} Lowercase abcdef. if (c >= ' a ' && C <= ' F ') {dec + = c-' a ' + 10;continue;} Uppercase ABCdef. if (c >= ' a ' && C <= ' F ') {dec + = c-' A ' + 10;continue;} Does not end in any of the IF statements, the description encounters an illegal character. return-1;} The normal end loop returns a 10 binary integer value. return Dec;}

10 Go 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;}



Decimal conversion to 16 binary, 16 binary to 2 binary

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.