C语言 在 C 中从字节(二进制)到 ASCII 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20444800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion from byte (binary) to ASCII in C
提问by jdepypere
I'm working with a PIC microprocessor, and I need to send values from an AD-Converter to a terminal (in this case HyperTerminal). Now, the terminal only understands ASCII. The value the AD-Converter gives a single byte (so ranging from 0 to 255). How can convert this binary number to ASCII?
我正在使用 PIC 微处理器,我需要将值从 AD 转换器发送到终端(在本例中为超级终端)。现在,终端只能理解 ASCII。AD-Converter 给出单个字节的值(范围从 0 到 255)。如何将此二进制数转换为 ASCII?
To be entirely clear, the byte is binary. So 0100 0000should result in 64. Ofcourse this would mean needing to send 2 ASCII numbers to the pc.
完全清楚,字节是二进制的。所以0100 0000应该导致64. 当然,这意味着需要向电脑发送 2 个 ASCII 数字。
Edit:Apparently I'm not entirely clear here. I need to have the ASCII-code of the binary number in binary as well so I can send it through a USB-template provided by the chip-manufacturer.
编辑:显然我在这里并不完全清楚。我还需要二进制数的 ASCII 码,以便我可以通过芯片制造商提供的 USB 模板发送它。
Edit 2:After some more delving into other topics, thisanswer led me to trying out itoa()and utoa(). Now, itoa()works, but unfortunately is for unsigned charachters. utoa()would be for unsigned chars, but that doesn't work. Here's an example of what should be able to happen:
编辑2:一些更深入研究等话题之后,这个答案促使我尝试itoa()和utoa()。现在,itoa()有效,但不幸的是适用于未签名的字符。utoa()将用于无符号字符,但这不起作用。以下是应该能够发生的事情的示例:
char USB_In_Buffer[64];
char myValue = 0x55;
itoa(myValue, USB_In_Buffer);
putUSBUSART(USB_In_Buffer, 3);
So every ASCII character should be sent to USB_In_Buffer. Then the total of characters written to this buffer should be used as second parameter in putUSBUSART(). I only need to convert numbers to ASCII as well, so not the whole characterset needs to be implemented.
所以每个 ASCII 字符都应该发送到USB_In_Buffer. 然后写入此缓冲区的字符总数应用作putUSBUSART(). 我只需要将数字也转换为 ASCII,所以不需要实现整个字符集。
Edit 3:Because some automated functions do not appear to be supported, I thought it wouldn't be that hard to make my own function.
编辑 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);
But there seems to be an error somewhere. Small numbers do work (0-99), but for some reason when there should be 121, it displays 1<1. For larger numbers, all the chars are non-numeric.
但似乎某处有错误。小数字确实有效(0-99),但由于某种原因,当应该有 时121,它会显示1<1。对于较大的数字,所有字符都是非数字的。
Edit 4:After rechecking my code I found the error. For one, ADCONvaluewas a char, not an unsigned char. Also I forgot to do %10for the tenths. Current working code:
编辑 4:重新检查我的代码后,我发现了错误。一方面,ADCONvalue是char,而不是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);
回答by alk
You might like to look at the printf()family of functions.
您可能想查看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. */
prints out
打印出来
'42'
Update referring the "send" function mentioned in the OP's update:
更新参考 OP 更新中提到的“发送”功能:
After having converted the integer vlaue of byteto its alphanumerical representation using snprintf()above, it could be place in the send buffer doing so:
byte使用snprintf()上面的方法将整数 vlaue 转换为其字母数字表示后,可以将其放置在发送缓冲区中:
putUSBUSART(str, strlen(str));
回答by chux - Reinstate Monica
Build an IntToHex()helper function.
构建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] = '#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;
}
';
send(s);
For the reverse see hexadecimal ASCII chars conversion
反之参见十六进制 ASCII 字符转换
回答by Bert
Here's a way to break down an unsigned charinto decimal by dividing by 100, 10, and then using the remainder:
这是一种unsigned char通过除以 100、10,然后使用余数将a 分解为十进制的方法:
Hundredths: 1
Tenths: 3
Ones: 7
The program outputs:
程序输出:
/*
* 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;
}
To convert to ASCII you ave to add 48 to each integer value. The ASCII codes for each of those numbers would be 49, 51, and 55.
要转换为 ASCII,您必须将 48 添加到每个整数值。每个数字的 ASCII 码是 49、51 和 55。
回答by Richard Chambers
A general purpose function for converting from a signed short binary value to a text string as decimal digits is provided below along with a companion function to convert an unsigned short binary value to a text string as hex digits.
下面提供了用于将有符号短二进制值转换为十进制数字形式的文本字符串的通用函数,以及将无符号短二进制值转换为十六进制数字形式的文本字符串的配套函数。
I think if you change the shortto longand the unsigned shortto unsigned longthe same logic would convert a signed long binary value to decimal digit text and an unsigned long binary value to hex digit text respectively.
我认为,如果您将shorttolong和 the unsigned shortto更改为unsigned long相同的逻辑,则会分别将有符号长二进制值转换为十进制数字文本,将无符号长二进制值分别转换为十六进制数字文本。
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;
}
A simple test harness is as follows.
一个简单的测试工具如下。
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
which produces the following output.
产生以下输出。
##代码##
![C语言 警告:应为“int **”,但参数的类型为“int (*)[(sizetype)(n)]”](/res/img/loading.gif)