如何将字节转换为Hex char

时间:2014-07-28 11:37:11

标签: c++ c arduino

大家好我需要将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]);

但是api v0.033中不存在printHex(我无法改变它)。有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

首先,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字符串)并打印那也是。输出是:

enter image description here

答案 1 :(得分:1)

示例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;
}

此方法通常比下面的第二个示例更快。

关于如何将字节转换为十六进制,空终止字符串的示例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;
}

答案 2 :(得分:1)

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);
相关问题