C + + encoding conversion

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<locale.h>intMain () {inti,v; Charbs[ -]; Charb[ -]; CharGst9]; Charh[9]; Chars[4]; Char*e;//decimal integer to binary string;I=1024x768; Ltoa (I,b,2); sprintf (BS,"%032s", B); printf ("i=%d,bs=%s\n", i,bs);//decimal integer to hexadecimal string;I=1024x768; Ltoa (I,h, -); sprintf (HS,"%08s", h); printf ("i=%d,hs=%s\n", I,HS);//hexadecimal string converted to decimal numberstrcpy (HS,"00000400"); SSCANF (HS,"%x",&i); printf ("hs=%s,i=%d\n", hs,i);//the binary string is converted to a hexadecimal string;strcpy (BS,"00000000000000000000010000000000"); I=strtol (Bs,&e,2); Ltoa (I,h, -); 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);//hex string into binary stringstrcpy (HS,"00000400"); SSCANF (HS,"%x",&i); Ltoa (I,b,2); sprintf (BS,"%032s", B); printf ("hs=%s,bs=%s\n", hs,bs);//ASC\GBK strings to hex stringsstrcpy (s),"a Han"); I=0;  while(1) {        if(0==s[i]) Break; sprintf (HS+i*2,"%02x", (unsignedChar) s[i]); I++; } setlocale (Lc_all,"CHS"); printf ("s=%s,hs=%s\n", S,HS);//hexadecimal string converted to kanji (GBK) and character (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 Han

Reprint: http://bbs.csdn.net/topics/390787012 idzhao4zhong1

Oh!

C + + encoding conversion

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.