Home > Software engineering >  Questions about serial communication data processing
Questions about serial communication data processing

Time:10-09

I use the serial port class do a serial port communication, now receives the data, how to put the data on the data of a certain extracted into a binary number show

CodePudding user response:

Cstrings stremp. The Format (" % 02 x ", bt); Bt is byte type

CodePudding user response:

_itot (bVal & amp; StBuff bMask), 2)

CodePudding user response:

Fyi:
 # include & lt; stdio.h> 
# include & lt; Stdlib. H>
# include & lt; String. H>
# include & lt; The 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 binary string;
I=1024;
Ltoa (I, b, 2);
Sprintf (bs, "% 032 s", b);
Printf (" I=% d, bs=% s \ n ", I, bs);
//decimal integer hexadecimal string;
I=1024;
Ltoa (I, h, 16);
Sprintf (hs, "% s" 08, h);
Printf (" I=% d, hs=% s \ n ", I, hs);
//hex string converted to a decimal number
Strcpy (hs, "00000400");
Sscanf (hs, "% x", & amp; i);
Printf (" the hs=% s, I=% d \ n ", the hs, I);
//binary string into hexadecimal string;
Strcpy (bs, "00000000000000000000010000000000");
I=strtol (bs, & amp; E, 2);
Ltoa (I, h, 16);
Sprintf (hs, "% s" 08, h);
Printf (" bs=% s, hs=% s \ n ", bs, hs);
//binary string converted to a decimal number;
Strcpy (bs, "00000000000000000000010000000000");
I=strtol (bs, & amp; E, 2);
Printf (" bs=% s, I=% d \ n ", bs, I);
//hex string into a binary string
Strcpy (hs, "00000400");
Sscanf (hs, "% x", & amp; i);
Ltoa (I, b, 2);
Sprintf (bs, "% 032 s", b);
Printf (" the hs=% s, bs=% s \ n ", hs, bs);
//ASC \ GBK string hexadecimal string
Strcpy (s, "a han");
i=0;
While (1) {
If (0==s [I]) break;
Sprintf (hs + I * 2, 02 x %, (unsigned char) s [I]);
i++;
}
The setlocale (LC_ALL, "CHS");
Printf (" s=% s, hs=% s \ n ", s, hs);
//hex string into (GBK Chinese characters) and characters (ASC)
Strcpy (hs, "61 baba");
i=0;
While (1) {
If (1!=sscanf (hs + I * 2, "% 2 x", & amp; V)) break;
S [I]=(char) v.
i++;
}
S [I]=0;
Printf (" the 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=61 baba
//hs=61 baba, s=a han
  • Related