Convert binary data to hexadecimal in a shell script

后端 未结 8 1649
日久生厌
日久生厌 2020-12-13 05:59

I want to convert binary data to hexadecimal, just that, no fancy formatting and all. hexdump seems too clever, and it \"overformats\" for me. I want to take x

8条回答
  •  没有蜡笔的小新
    2020-12-13 06:17

    Perhaps you could write your own small tool in C, and compile it on-the-fly:

    int main (void) {
      unsigned char data[1024];
      size_t numread, i;
    
      while ((numread = read(0, data, 1024)) > 0) {
        for (i = 0; i < numread; i++) {
          printf("%02x ", data[i]);
        }
      }
    
      return 0;
    }
    

    And then feed it from the standard input:

    cat /bin/ls | ./a.out
    

    You can even embed this small C program in a shell script using the heredoc syntax.

提交回复
热议问题