C-language Data Conversion (hex-char-byte Array-acsii)

Source: Internet
Author: User

The online collation, the source is not clear, thank the Author.

Dataconvert.c

#include <stdio.h>#include<string.h>#include"DataConvert.h"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;}intHexchartobyte (Char*s,Char*byte){    intI,n =0;  for(i =0; s[i]; i + =2)    {        if(s[i] >='A'&& s[i] <='F')            byte[n] = s[i]-'A'+Ten; Else byte[n] = s[i]-'0'; if(s[i +1] >='A'&& S[i +1] <='F')            byte[n] = (byte[n] <<4) | (s[i +1] -'A'+Ten); Else byte[n] = (byte[n] <<4) | (s[i +1] -'0'); ++n; }        returnn;} unsignedCharChartoascii (ConstUnsignedCharCha) {unsignedCharascii; if(cha >=0x0A) && (cha <=0x0F) ) {ascii= Cha +'A'-Ten; }    Else{ascii= Cha +'0'; }    returnascii;}

DataConvert.h

#ifndef __data_h#define__data_hintStrtohex (Char*ch,Char*hex);intHEXTOSTR (Char*hex,Char*ch);intHexchartovalue (Const Charch);CharValuetohexch (Const intvalue);intHexchartobyte (Char*s,Char*byte); unsignedCharChartoascii (ConstUnsignedCharcha);#endif

Main.c

#include <stdio.h>#include<string.h>#include"DataConvert.h"#defineMcu_firware_version "V1.0.0"#defineBle_firware_version "V1.0.0"#defineFont_version "V1.0.0"intMainintargcChar*Argv[]) {    inti; Charresult[1024x768]; Char*p_result =result; //converting version Number data    Charmcu_version_hex[ a]; Charmcu_version_byte[6]; Char*p_ch =mcu_firware_version; Char*p_hex =mcu_version_hex;    Strtohex (p_ch,p_hex); intn =Hexchartobyte (mcu_version_hex,mcu_version_byte); Charble_version_hex[ a]; Charble_version_byte[6]; P_ch=ble_firware_version; P_hex=ble_version_hex;    Strtohex (p_ch,p_hex); intm =Hexchartobyte (ble_version_hex,ble_version_byte); Charfont_version_hex[ a]; Charfont_version_byte[6]; P_ch=font_version; P_hex=font_version_hex;    Strtohex (p_ch,p_hex); intK =Hexchartobyte (font_version_hex,font_version_byte); //populating the version Number data     for(inti =0; i<n;i++) printf ("%x",0XFF&mcu_version_byte[i]);  for(inti =0; i<m;i++) printf ("%x",0XFF&ble_version_byte[i]);  for(inti =0; i<k;i++) printf ("%x",0XFF&font_version_byte[i]);        Hextostr (p_hex, p_result); printf ("the string is:%s\n", p_result);}

The demo above converts a string into a UTF8 byte stream, which can be converted to string validation using the UTF8 conversion tool.

------end

C-language Data Conversion (hex-char-byte Array-acsii)

Related Article

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.