h = short int 的意思(2字節)
%hu :以 unsigned short格式輸出整數
%hx :以16進制的 輸出short類型的整數
比如
printf("%hu\n",-30); 會輸出 65506
printf("%hx\n",-30); 會輸出FFE2
short int = [h] + [d, i, o, x, or X]
EX:
scanf_s("%ls", &x, 2); // Read a wide-character string
wscanf_s(L"%hC", &x, 2); // Read a single-byte character
sscanf, scanf
The sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format
ex:
int i, result;
float fp;
char c, s[81];
wchar_t wc, ws[81];
result = scanf( "%d %f %c %C %s %S", &i, &fp, &c, &wc, s, ws );
printf( "\nThe number of fields input is %d\n", result );
printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws);
result = wscanf( L"%d %f %hc %lc %S %ls", &i, &fp, &c, &wc, s, ws );
wprintf( L"\nThe number of fields input is %d\n", result );
wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws);
ex:
BYTE m_bCmd[MAX_CMD_LENGTH_12+1]; // sscanf need length + 1
::sscanf(Buffer, "%2hx %2hx %2hx %2hx %2hx %2hx %2hx %2hx %2hx %2hx %2hx %2hx",
&m_bCmd[0],
&m_bCmd[1],
&m_bCmd[2],
&m_bCmd[3],
&m_bCmd[4],
&m_bCmd[5],
&m_bCmd[6],
&m_bCmd[7],
&m_bCmd[8],
&m_bCmd[9],
&m_bCmd[10],
&m_bCmd[11]
);
REF
https://msdn.microsoft.com/en-us/library/xdb9w69d.aspx
https://msdn.microsoft.com/en-us/library/aa272895(v=vs.60).aspx
沒有留言:
張貼留言