简体   繁体   中英

How to convert byte to Hex char

Hi guys I need to convert Byte to Hex char and then print it. I have waspmote v11 so I cannot use api 010(I'm not able to use USB.printHex function).
Using the v010 API(available only with v12 waspmote) I do this:

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]);

But printHex dosen't exist in api v0.033(and I cannot change it). Someone could help me?

First, a char is one byte in size. In memory, a char is a byte, and is an integer value that can be depicted in hexadecimal form. If I understand your request convert Byte to Hex char and then print it , here is a simple example showing how to do that:

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

This code simply formats the integer values stored in the char array to a hexadecimal as they are printed, then uses sprintf() to place the values into a NULL terminated char array (C string) and prints that out also. The output is:

在此处输入图片说明

Example 1 on how to convert a byte to a hexadecimal, null terminated string:

#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;
}

This method is usually faster than the second example below.

Example 2 on how to convert a byte to a hexadecimal, null terminated string:

#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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM