[英]How to convert char to int and then display it in hex format
我正在用c char receivebuffer[100]
一個程序,在該程序中我從串行設備獲取數據,並將其存儲在緩沖區char receivebuffer[100]
。 當我顯示receivebuffer的內容時,輸出顯示
(這是ASCII格式)。 但是預期的輸出為十六進制格式。 如何將其轉換為十六進制?
我也想知道,如果我將buffer轉換為int,輸出將是一樣的。 請告訴我如何將char緩沖區也轉換為int?
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tcgetattr\n\n", errno);
printf("Error Opening the device\n\n");
exit(0);
//error_message ("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("error %d from tcsetattr\n\n", errno);
printf("Error Opening the device\n\n");
exit(0);
//error_message ("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf("error\n\n");
printf("Error Opening the device\n\n");
exit(0);
//error_message ("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf("Error Opening the device\n\n");
//error_message ("error %d setting term attributes", errno);
}
int main()
{
char *portname = "/dev/ttyUSB0";
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
//error_message ("error %d opening %s: %s", errno, portname, strerror (errno));
printf("error");
}
set_interface_attribs (fd, B9600, 0); // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0); // set no blocking
// send 7 character greeting
usleep ((7 + 25) * 100); // sleep enough to transmit the 7 plus
while(1)
{
char receivebuffer [100];
read (fd, receivebuffer, sizeof receivebuffer); // read up to 100 characters if ready to read
printf("value of buffer is %s\n\n", receivebuffer);
return 0;
}
}
您應該替換為:
printf("value of buffer is %s\n\n", receivebuffer);
與:
for (int tmpfoo = 0; receivebuffer[tmpfoo] != '\0'; tmpfoo++)
{
printf("value of buffer is %X\n\n", (int)receivebuffer[tmpfoo]);
}
如果只希望在HEXvalue之后跟隨HEXvalue。
您需要將接收到的字節數存儲在某個地方,並在for循環中使用它,像這樣嘗試
char receivebuffer[100];
int count;
int i;
count = read (fd, receivebuffer, sizeof receivebuffer); // read up to 100 characters if ready to read
for (i = 0 ; i < count ; ++i)
{
printf("0x%02X ", receivebuffer[i]);
if ((i + 1) % 8 == 0)
printf("\n");
}
if ((i + 1) % 8 == 0)
只是要連續打印8
個字節,則可以更改或刪除它,但是它有助於檢查數據。
您不需要轉換它。 要將char
顯示為十六進制數字,請在%hhx
printf
組中使用%hhx
格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.