[英]How to convert byte to Hex char
嗨,大家好,我需要將Byte轉換為Hex char,然后打印出來。 我有waspmote v11,因此無法使用api 010(我無法使用USB.printHex
函數)。
使用v010 API(僅v12 waspmote可用)執行此操作:
USB.printHex(xbeeZB.getExtendedPAN[0]);
USB.printHex(xbeeZB.getExtendedPAN[1]);
USB.printHex(xbeeZB.getExtendedPAN[2]);
USB.printHex(xbeeZB.getExtendedPAN[3]);
USB.printHex(xbeeZB.getExtendedPAN[4]);
USB.printHex(xbeeZB.getExtendedPAN[5]);
USB.printHex(xbeeZB.getExtendedPAN[6]);
USB.printHex(xbeeZB.getExtendedPAN[7]);
但是printHex在api v0.033中不存在(我無法更改它)。 有人可以幫我嗎?
首先,一個char
大小為一個字節 。 在內存中,char 是一個字節, 並且是可以以十六進制形式表示的整數值。 如果我了解您的請求,請先將Byte轉換為Hex char然后打印出來 ,這是一個簡單的示例,說明了如何執行此操作:
int main(void)
{
char byte[10]={2,23,76,125,43,65,78,37,19,84};
char string[160];
int i;
for(i=0;i<sizeof(byte)/sizeof(*byte);i++)
{
printf("0x%02x, ", byte[i]);
}
printf("\n");
//Placing elements into a string:
sprintf(string, "Null Terminated String:\n0x%02x, 0x%02x, 0x%02x,0x%02x, 0x%02x,"
"0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
byte[0], byte[1], byte[2], byte[3], byte[4],
byte[5], byte[6], byte[7], byte[8], byte[9]);
printf("%s", string);//null terminated string
getchar();
return 0;
}
這段代碼在打印時將存儲在char數組中的整數值簡單地格式化為十六進制,然后使用sprintf()
將這些值放入以NULL終止的char
數組(C字符串)中,並將其打印出來。 輸出為:
有關如何將字節轉換為十六進制,以null終止的字符串的示例1:
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int uValue;
unsigned int uNibble;
char sHexByte[3];
sHexByte[2] = '\0';
const char csHexChars[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
for (uValue = 0; uValue < 256U; uValue++)
{
uNibble = (uValue & 0xFFU) >> 4U;
sHexByte[0] = csHexChars[uNibble];
uNibble = uValue & 0x0FU;
sHexByte[1] = csHexChars[uNibble];
if (uValue > 0) putchar(':');
fputs(sHexByte,stdout);
}
putchar('\n');
/* Dummy code to have no warnings on build. */
if(argv[0][1] == ' ') return argc;
return 0;
}
此方法通常比下面的第二個示例快。
有關如何將字節轉換為十六進制,以null終止的字符串的示例2:
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int uValue;
unsigned int uNibble;
char sHexByte[3];
sHexByte[2] = '\0';
for (uValue = 0; uValue < 256U; uValue++)
{
uNibble = (uValue & 0xFFU) >> 4U;
sHexByte[0] = (uNibble < 10) ? uNibble + '0' : uNibble + ('A' - 10U);
uNibble = (uValue & 0x0FU);
sHexByte[1] = (uNibble < 10) ? uNibble + '0' : uNibble + ('A' - 10U);
if (uValue > 0) putchar(':');
fputs(sHexByte,stdout);
}
putchar('\n');
/* Dummy code to have no warnings on build. */
if(argv[0][1] == ' ') return argc;
return 0;
}
unsigned char firstNibble=0U; // a Nibble is 4 bits, half a byte, one hexadecimal character
char firstHexChar=0;
unsigned char initialByte; //initialize this to the byte you want to print
unsigned char secondNibble=0U;
char secondHexChar=0;
firstNibble=(initialByte>>4); // isolate first 4 bits
if(firstNibble<10U)
{
firstHexChar=(char)('0'+firstNibble);
}
else
{
firstNibble-=10U;
firstHexChar=(char)('A'+firstNibble);
}
secondNibble=(initialByte&0x0F); // isolate last 4 bits
if(secondNibble<10U)
{
secondHexChar=(char)('0'+secondNibble);
}
else
{
secondNibble-=10U;
secondHexChar=(char)('A'+secondNibble);
}
printf("%c%c\n", firstHexChar, secondHexChar);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.