Linux下怎么查看16进制文件

Linux下怎么查看16进制文件,第1张

1、xxd - make a hexdump or do the reverse.

能够显示文件对应的十六机制格式,同时还能将修还后的十六进制格式反写回文件,一般与vim结合使用。(在 vi 或 vim 的命令状态下)

:%!xxd 将当前文本转换为16进制格式。

:%!xxd -r 将当前文件转换回文本格式。

如果没有此命令,请先安装vim-common

rpm -qf /usr/bin/xxd

vim-common-7.4.160-1.el7_3.1.x86_64

2、od - dump files in octal and other formats

可以通过指定参数为查看十六进制

-A 指定左边侧栏显示的地址基数,默认为八进制

-t 指定输出单元的进制格式和字节

-x = -t x2 输出单元为双字节(注意:每单元内排列顺序从左到右为 [高字节|低字节])

例:od -A x -t x1 file| more

3、hexdump - ascii, decimal, hexadecimal, octal dump

查看十六机制的首选工具。

-c 每单元以字节为单位,显示出对应的ASCII码

-C 每单元以字节为单位,同时显示十六机制和ASCII码

4、linux也有GUI的十六进制编辑器,例如ghex和bless

Linux系统有一个od命令,可以将文件的内容以16进制形式显示出来,它的用法是这样的:

od -t xCc 文件名

-t xCc表示用16进制来输出文件的内容,同时用ASCII字符对照显示,例子:

od -t xCc /etc/issue

详细细节可以参考下面的连接。

网页链接

欢迎使用腾讯电脑管家来全方位的实时保护你的电脑。

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <errno.h>

#include <sys/types.h>

#include <fcntl.h>

#include <unistd.h>

#include <termios.h>

#include <stdlib.h>

#define BUFFER_SIZE 1024

#define HOST_PORT 0

int set_port(int fd, int baud_rate, int data_bits, char parity, int stop_bits)

{

    struct termios newtio,oldtio

    if( tcgetattr(fd,&oldtio) != 0)

    {

        perror("Setup Serial 1")

        return -1

    }

    bzero(&newtio,sizeof(newtio))

    newtio.c_cflag |= CLOCAL | CREAD

    newtio.c_cflag &= ~CSIZE

    /* set baud_speed*/

    switch(baud_rate)

    {

        case 2400:

            cfsetispeed(&newtio,B2400)

            cfsetospeed(&newtio,B2400)

            break

        case 4800:

            cfsetispeed(&newtio,B4800)

            cfsetospeed(&newtio,B4800)

            break

        case 9600:

            cfsetispeed(&newtio,B9600)

            cfsetospeed(&newtio,B9600)

            break

        case 19200:

            cfsetispeed(&newtio,B19200)

            cfsetospeed(&newtio,B19200)

            break

        case 38400:

            cfsetispeed(&newtio,B38400)

            cfsetospeed(&newtio,B38400)

            break

        default:

        case 115200:

            cfsetispeed(&newtio,B115200)

            cfsetospeed(&newtio,B115200)

            break

    }

    /* set data_bits upon 7 or 8*/

    switch(data_bits)

    {

        case 7:

            newtio.c_cflag |= CS7

            break

        default :

        case 8:

            newtio.c_cflag |= CS8

            break

    }

    /**/

    switch(parity)

    {

        default:

        case 'N':

        case 'n':

        {

            newtio.c_cflag &= ~PARENB

            newtio.c_iflag &= ~INPCK

        }

        break

        case 'o':

        case 'O':

        {

            newtio.c_cflag |= (PARODD | PARENB)

            newtio.c_iflag |= INPCK

        }

        break

        case 'e':

        case 'E':

        {

            newtio.c_cflag |= PARENB

            newtio.c_cflag &= ~PARODD

            newtio.c_iflag |= INPCK

        }

        break

        case 's':

        case 'S':

        {

            newtio.c_cflag &= ~PARENB

            newtio.c_cflag &= ~CSTOPB

        }

        break

    }

    /*set stop_bits 1 or 2 */

    switch(stop_bits)

    {

        default:

        case 1:

        {

            newtio.c_cflag &= ~CSTOPB

        }

        break

        case 2:

        {

            newtio.c_cflag |= CSTOPB

        }

        break

    }

    newtio.c_cc[VTIME] = 0

    newtio.c_cc[VMIN] = 1

    tcflush(fd,TCIFLUSH)

    if((tcsetattr(fd,TCSANOW,&newtio)) != 0)

    {

        perror("com set error")

        return -1

    }

    printf("set UART done!\n")

    return 0

}

int open_port(int com_port)

{

    int fd = 0

    char *dev[] = {"/dev/ttyAMA0", "/dev/ttyS1", "/dev/ttyS2","/dev/ttyS3",

                    "/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6"}

    if((com_port < 0) || (com_port > 6) )

    {

        printf("the port is out range")

        return -1

    }

    fd = open(dev[com_port], O_RDWR | O_NOCTTY | O_NDELAY)

    if(fd < 0)

    {

        perror("open serial port")

        return -1

    }

    if(fcntl(fd, F_SETFL,0) < 0)

    {

        perror("fcntl F_SETFL")

        return -1

    }

    if(isatty(fd) == 0)

    {

        perror("isatty is not a terminal device")

        return -1

    }

    return fd

}

int main(void)

{

    int fd = 0

    char Buffer[BUFFER_SIZE] = {0}

    if((fd = open_port(HOST_PORT)) == -1)

    {

        perror("open port")

        return -1

    }

    if(set_port(fd,115200,8,'N',1)== -1)

    {

        perror("set port")

        return -1

    }

    int ReadByte = read(fd,Buffer,512)

char NFC[1024]=""

    if(ReadByte>0)

    {

        int i

        printf("readlength=%d\n",ReadByte) 

        printf("The Data is:")

        for (i=0i<ReadBytei++)

        {

            printf("%02x ",Buffer[i])

            int lennn = strlen(NFC)

            sprintf(NFC+lennn, "%02x ",Buffer[i])

        }

        printf("\n") 

        printf("%s\n", NFC)

        sleep(3) 

    }

    else printf("Read data failure ")

    close(fd)

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7630495.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-08
下一篇2023-04-08

发表评论

登录后才能评论

评论列表(0条)

    保存