在C中从字节(二进制)转换为ASCII

时间:2013-12-07 18:15:58

标签: c byte pic

我正在使用PIC微处理器,我需要将值从AD转换器发送到终端(在本例中为HyperTerminal)。现在,终端只能理解ASCII。 AD-Converter提供单个字节的值(范围从0到255)。如何将这个二进制数转换为ASCII?

完全清楚,字节是二进制的。因此,0100 0000应该会产生64。当然,这意味着需要向电脑发送2个ASCII码。

编辑:显然我在这里并不完全清楚。我需要二进制数的ASCII码也是二进制的,所以我可以通过芯片制造商提供的USB模板发送它。

编辑2 :在深入研究其他主题后,this回答了我,尝试了itoa()utoa()。现在,itoa()有效,但不幸的是,对于未签名的字符。 utoa()将用于未签名的字符,但这不起作用。以下是应该发生的事情的一个例子:

char USB_In_Buffer[64];
char myValue = 0x55;

itoa(myValue, USB_In_Buffer);
putUSBUSART(USB_In_Buffer, 3);

因此每个ASCII字符都应发送到USB_In_Buffer。然后,写入此缓冲区的字符总数应该用作putUSBUSART()中的第二个参数。我只需要将数字转换为ASCII,因此不需要实现整个字符集。

编辑3:由于某些自动功能似乎不受支持,我认为制作自己的功能并不难。

if(ADCONvalue/100 != 0){
    res++;
    USB_In_Buffer[0] = (ADCONvalue / 100) + 0x30;
}
if(ADCONvalue/10 != 0){
    res++;
    if(res == 1){
        USB_In_Buffer[0] = (ADCONvalue / 10) + 0x30;
    }else{
        USB_In_Buffer[1] = (ADCONvalue / 10) + 0x30;
    }   

}
res++;
if(res == 1){
    USB_In_Buffer[0] = ADCONvalue % 10 + 0x30;
}else if(res == 2){
    USB_In_Buffer[1] = ADCONvalue % 10 + 0x30;
}else{
    USB_In_Buffer[2] = ADCONvalue % 10 + 0x30;
}
putUSBUSART(USB_In_Buffer, res);

但似乎某处出现了错误。小数字可以正常工作(0-99),但由于某些原因应该有121,它会显示1<1。对于较大的数字,所有字符都是非数字的。

编辑4:重新检查我的代码后,我发现了错误。例如,ADCONvaluechar,而不是unsigned char。我忘了为十分之一做%10。目前的工作代码:

if(ADCONvalue/100 != 0){
    res++;
    USB_In_Buffer[0] = (ADCONvalue / 100) + 0x30;
}
if(ADCONvalue/10 != 0){
    res++;
    if(res == 1){
        USB_In_Buffer[0] = (ADCONvalue / 10)%10 + 0x30;
    }else{
        USB_In_Buffer[1] = (ADCONvalue / 10)%10 + 0x30;
    }   

}
res++;
if(res == 1){
    USB_In_Buffer[0] = ADCONvalue % 10 + 0x30;
}else if(res == 2){
    USB_In_Buffer[1] = ADCONvalue % 10 + 0x30;
}else{
    USB_In_Buffer[2] = ADCONvalue % 10 + 0x30;
}
putUSBUSART(USB_In_Buffer, res);

4 个答案:

答案 0 :(得分:3)

您可能希望查看printf()系列函数。

char str[32] = "";
unsigned char byte = 42;
snprintf(str, sizeof(str), "%hhu", byte); 
printf("'%s'", str); /* This is a debug statement to check what's be placed in str. */

打印出来

'42'

更新参考OP更新中提到的“发送”功能:

使用上面的bytesnprintf()的整数值转换为其字母数字表示后,可以将其放在发送缓冲区中:

putUSBUSART(str, strlen(str));

答案 1 :(得分:3)

构建IntToHex()辅助函数。

static char IntToHex(unsigned x) {
  x &= 15;
  if (x <= 9) return x + '0';
  return x - 10 + 'A';
  }

unsigned char ADConverter;
char s[3];
s[0] = IntToHex(ADConverter >> 4);
s[1] = IntToHex(ADConverter);
s[2] = '\0';
send(s);

反过来看hexadecimal ASCII chars conversion

答案 2 :(得分:2)

这是一种将unsigned char分解为十进制的方法,除以100,10,然后使用余数:

#include <stdio.h>

int main (void) {
   unsigned char val = 137;
   unsigned char res;

   printf("\n\nHundredths: %u\n", (int)((val / 100)));
   val = val % 100;
   printf("Tenths:     %u\n", (int)val / 10);
   printf("Ones:       %u\n", (int)val % 10);

   return 0;
}

程序输出:

Hundredths: 1
Tenths:     3
Ones:       7

要转换为ASCII,您需要为每个整数值添加48。每个数字的ASCII码分别为49,51和55。

答案 3 :(得分:1)

下面提供了一个通用函数,用于将带符号的短二进制值转换为文本字符串作为十进制数字,以及一个配套函数,用于将无符号短二进制值转换为十六进制数字的文本字符串。

我认为如果您将short更改为long而将unsigned short更改为unsigned long,则相同的逻辑会将带符号的长二进制值转换为十进制数字文本和无符号长二进制值分别为十六进制数字文本。

/*
 *   char *ShortToString (short sVal, char *pOutText, int iLen)
 *
 *   Input:    sVal      - binary value as a signed short to convert to text
 *             pOutText  - pointer to a character array to receive the text
 *             iLen      - length of the character array in characters
 *
 *   Returns:  char * - saved value of pOutText, pointer to beginning of output buffer
 *
 *   Description:  Converts the signed binary value of sVal into a text string of decimal digits.
 *                 If the value of sVal is negative, a minus sign is the first character.
 *                 If the output buffer length is too small, astericks are put into the buffer.
 *
 *                 WARNING: Buffer must be sized to include a terminating zero for the end
 *                          of string character.
**/
char *ShortToString (short sVal, char *pOutText, int iLen)
{
    char *pOutTextSave = pOutText;
    char *pOutTextBegin = pOutText;

    if (sVal < 0) {
        sVal *= -1;
        *pOutText++ = '-';
    }
    pOutTextBegin = pOutText;    // remember where beginning of numeric string is

    // generate text digits in order from least significant to most significant
    do {
        *pOutText++ = (sVal % 10) + '0';  // convert least signicant decimal digit to text character
        sVal /= 10;                       // shift the binary value one decimal digit righ
    } while (sVal > 0 && pOutText + 1 < pOutTextSave + iLen);

    *pOutText = 0;   // end of string terminator
    pOutText--;      // back up one to point to last character.

    if (sVal) {
        // if there is insufficient room in the provided buffer for the string of digits
        // then indicate by filling the buffer with astericks.
        pOutTextBegin = pOutTextSave;
        while (pOutTextBegin + 1 < pOutTextSave + iLen) {*pOutTextBegin++ = '*'; }
        *pOutTextBegin = 0;   // end of string terminator
    } else {
        // reverse the string so that digits are in the right order,
        // most significant to least significant. we do this by swapping digits
        // until we come to the middle which means that everything is in order.
        while (pOutTextBegin < pOutText) {
            char k = *pOutTextBegin;
            *pOutTextBegin = *pOutText;
            *pOutText = k;
            pOutTextBegin++; pOutText--;
        }
    }

    return pOutTextSave;
}

/*
 *   char *HexShortToString (unsigned short usVal, char *pOutText, int iLen)
 *
 *   Input:    usVal     - binary value as an unsigned short to convert to text
 *             pOutText  - pointer to a character array to receive the text
 *             iLen      - length of the character array in characters
 *
 *   Returns:  char * - saved value of pOutText, pointer to beginning of output buffer
 *
 *   Description:  Converts the unsigned binary value of sVal into a text string of hex digits.
 *                 If the output buffer length is too small, astericks are put into the buffer.
 *
 *                 WARNING: Buffer must be sized to include a terminating zero for the end
 *                          of string character.
**/
char *HexShortToString (unsigned short usVal, char *pOutText, int iLen)
{
    char *pOutTextSave = pOutText;

    // generate text digits in order from least significant to most significant
    do {
        unsigned char uChar = usVal & 0x000f;

        *pOutText++ = (uChar < 10) ? uChar + '0' : uChar - 10 + 'A';  // convert least signicant hex digit to text character
        usVal >>= 4;                       // shift the binary value one hex digit righ
    } while (usVal > 0 && pOutText + 1 < pOutTextSave + iLen);

    *pOutText = 0;   // end of string terminator
    pOutText--;      // back up one to point to last character.

    if (usVal) {
        // if there is insufficient room in the provided buffer for the string of digits
        // then indicate by filling the buffer with astericks.
        pOutText = pOutTextSave;
        while (pOutText + 1 < pOutTextSave + iLen) {*pOutText++ = '*'; }
        *pOutText = 0;   // end of string terminator
    } else {
        char *pOutTextBegin = pOutTextSave;

        // reverse the string so that digits are in the right order,
        // most significant to least significant. we do this by swapping digits
        // until we come to the middle which means that everything is in order.
        while (pOutTextBegin < pOutText) {
            char k = *pOutTextBegin;
            *pOutTextBegin = *pOutText;
            *pOutText = k;
            pOutTextBegin++; pOutText--;
        }
    }

    return pOutTextSave;
}

一个简单的测试工具如下。

int main(int argc, char **argv)
{
    char pOutBuff[16], pOutBuffHex[16];
    char *p, *pHex;
    short sVal, sLen;

    p = ShortToString((sVal = 0), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 0), pOutBuff, (sLen = 2));
    printf (" sVal = %d  ShortToString() p = %s len = %d\n", sVal, p, sLen);
    p = ShortToString((sVal = 1), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -1), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 12), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -12), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 12), pOutBuff, (sLen = 2));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -12), pOutBuff, (sLen = 2));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 12), pOutBuff, (sLen = 3));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -12), pOutBuff, (sLen = 3));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 12), pOutBuff, (sLen = 4));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -12), pOutBuff, (sLen = 4));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -12), pOutBuff, (sLen = 5));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf ("     sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 103), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -103), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 100), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -100), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 123), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -123), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 1234), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -1234), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 12345), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -12345), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = 32767), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);
    p = ShortToString((sVal = -32767), pOutBuff, (sLen = 15));
    pHex = HexShortToString(sVal, pOutBuffHex, sLen);
    printf (" sVal = %d  ShortToString() p = %s, 0x%x pHex=%s len = %d\n", sVal, p, sVal, pHex, sLen);

    return 0;
}

产生以下输出。

 sVal = 0  ShortToString() p = 0, 0x0 pHex=0 len = 15
 sVal = 0  ShortToString() p = 0 len = 2
 sVal = 1  ShortToString() p = 1, 0x1 pHex=1 len = 15
 sVal = -1  ShortToString() p = -1, 0xffffffff pHex=FFFF len = 15
 sVal = 12  ShortToString() p = 12, 0xc pHex=C len = 15
 sVal = -12  ShortToString() p = -12, 0xfffffff4 pHex=FFF4 len = 15
     sVal = 12  ShortToString() p = *, 0xc pHex=C len = 2
     sVal = -12  ShortToString() p = *, 0xfffffff4 pHex=* len = 2
     sVal = 12  ShortToString() p = 12, 0xc pHex=C len = 3
     sVal = -12  ShortToString() p = **, 0xfffffff4 pHex=** len = 3
     sVal = 12  ShortToString() p = 12, 0xc pHex=C len = 4
     sVal = -12  ShortToString() p = -12, 0xfffffff4 pHex=*** len = 4
     sVal = -12  ShortToString() p = -12, 0xfffffff4 pHex=FFF4 len = 5
 sVal = 103  ShortToString() p = 103, 0x67 pHex=67 len = 15
 sVal = -103  ShortToString() p = -103, 0xffffff99 pHex=FF99 len = 15
 sVal = 100  ShortToString() p = 100, 0x64 pHex=64 len = 15
 sVal = -100  ShortToString() p = -100, 0xffffff9c pHex=FF9C len = 15
 sVal = 123  ShortToString() p = 123, 0x7b pHex=7B len = 15
 sVal = -123  ShortToString() p = -123, 0xffffff85 pHex=FF85 len = 15
 sVal = 1234  ShortToString() p = 1234, 0x4d2 pHex=4D2 len = 15
 sVal = -1234  ShortToString() p = -1234, 0xfffffb2e pHex=FB2E len = 15
 sVal = 12345  ShortToString() p = 12345, 0x3039 pHex=3039 len = 15
 sVal = -12345  ShortToString() p = -12345, 0xffffcfc7 pHex=CFC7 len = 15
 sVal = 32767  ShortToString() p = 32767, 0x7fff pHex=7FFF len = 15
 sVal = -32767  ShortToString() p = -32767, 0xffff8001 pHex=8001 len = 15