Conversion of strings and 16 binary strings

Source: Internet
Author: User

In our work, we sometimes need to convert a string into a 16 string to the user, because there are some characters in ASCII, when we use printf ("%s", p_ch), the output will be messy, and if you use 16, it will be much better.

So write the program with the following code:

#include <stdio.h>#include<string.h>intStrtohex (Char*ch,Char*hex);intHEXTOSTR (Char*hex,Char*ch);intHexchartovalue (Const Charch);CharValuetohexch (Const intvalue);intMainintargcChar*argv[]) {    Charch[1024x768]; Charhex[1024x768]; Charresult[1024x768]; Char*p_ch =ch; Char*p_hex =Hex; Char*p_result =result; printf ("Please input the string:"); scanf ("%s", p_ch);    Strtohex (P_ch,p_hex); printf ("The hex is:%s\n", P_hex);    Hextostr (P_hex, P_result); printf ("The string is:%s\n", P_result); return 0;}intStrtohex (Char*ch,Char*hex) {  intHigh,low; intTMP =0; if(ch = = NULL | | hex = =NULL) {    return-1; }  if(strlen (ch) = =0){    return-2; }   while(*ch) {tmp= (int)*ch; High= tmp >>4; Low= tmp & the; *hex++ = Valuetohexch (high);//write high byte first*hex++ = Valuetohexch (low);//Next Write Low bytech++; }  *hex =' /'; return 0;}intHEXTOSTR (Char*hex,Char*ch) {  intHigh,low; intTMP =0; if(hex = = NULL | | ch = =NULL) {    return-1; }  if(strlen (hex)%2==1){    return-2; }   while(*hex) { High= Hexchartovalue (*hex); if(High <0){      *ch =' /'; return-3; } Hex++;//The pointer moves to the next characterLow = Hexchartovalue (*hex); if(Low <0){      *ch =' /'; return-3; } tmp= (High <<4) +Low ; *ch++ = (Char) tmp; Hex++; }  *ch =' /'; return 0;}intHexchartovalue (Const Charch) {  intresult =0; //get 16-binary high-byte bits of data  if(Ch >='0'&& CH <='9') {result= (int) (CH-'0'); }  Else if(Ch >='a'&& CH <='Z') {result= (int) (CH-'a') +Ten; }  Else if(Ch >='A'&& CH <='Z') {result= (int) (CH-'A') +Ten; }  Else{result= -1; }  returnresult;}CharValuetohexch (Const intvalue) {  Charresult =' /'; if(Value >=0&& value <=9) {result= (Char) (Value + -);//48 ASCII encoded ' 0 ' character encoded value  }  Else if(Value >=Ten&& value <= the) {result= (Char) (Value-Ten+ $);//subtract 10 to find its offset in 16, and 65 is the ASCII character encoding value of ' A '  }  Else{    ; }  returnresult;}

Conversion of strings and 16 binary strings

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.