简体   繁体   English

一次打印十六进制数字2个字节

[英]Printing hex digits 2 bytes at a time

I am looking to convert existing code in C to perform the following: I am attempting to write a hex dump program that prints out address: values printable characters. 我希望转换C中的现有代码执行以下操作:我正在尝试编写一个打印出地址的十六进制转储程序:值可打印字符。 Currently, the code for values is printing in the following format: 目前,值的代码按以下格式打印:

0003540: 05 04 06 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ...unsigned char

Desired hex output: 期望的十六进制输出:

0003540: 0504 0675 6e73 6967 6e65 6420 6368 6172 ...unsigned char

Current code printing in pairs: 当前代码打印成对:

addr = 0;
  while ( ( cnt = ( long )
    fread ( buf, sizeof ( unsigned char ), 16, filein ) ) > 0 ) {

    b = buf;
    /* Print the address in hexadecimal. */
    fprintf ( fileout, "%07lx  ", addr );
    addr = addr + 16;
    /* Print 16 data items, in pairs, in hexadecimal. */
    cnt2 = 0;
    for ( m = 0; m < 16; m++ ) {
      cnt2 = cnt2 + 1;
      if ( cnt2 <= cnt ) {
        fprintf ( fileout, "%02x", *b++ );
      }
      else {
        fprintf ( fileout, "  " );
      }
      fprintf ( fileout, " " );
    }
    /* Print the printable characters, or a period if unprintable. */
    fprintf ( fileout, " " );
    cnt2 = 0;
    for ( n = 0; n < 16; n++ ) {
      cnt2 = cnt2 + 1;
      if ( cnt2 <= cnt ) {
        if ( ( buf[n] < 32 ) || ( buf[n] > 126 ) ) {
          fprintf ( fileout, "%c", '.' );
        }
        else {
          fprintf ( fileout, "%c", buf[n] );
        }
      }
    }
    fprintf( fileout, "\n" );
  }

How can I alter this code to achieve the AB12 CD34 format? 如何更改此代码以实现AB12 CD34格式? Thanks! 谢谢!

Use the modulo (remainder) operator % to test if m is divisible by 2. Only write the space when it is: 使用modulo(余数)运算符%来测试m是否可被2整除。只有在以下情况下写入空格:

for ( m = 0; m < 16; m++ ) {
  if ( m > 0 && m % 2 == 0 ) {
    fprintf ( fileout, " " );
  }
  fprintf ( fileout, "%02x", *b++ );
}

Edit 3: 编辑3:

for ( m = 0; m < 16; m++ ) {
  if ( m > 0 && m % 2 == 0 ) {
    fprintf ( fileout, " " ); // space between every second byte
  }
  if ( m < cnt ) {
    fprintf ( fileout, "%02x", *b++ );
  } else {
    fprintf ( fileout, "  " ); // blank if run out of bytes on this line
  }
}

I think this can be simplified quite a bit. 我认为这可以简化一点。 For example, I'd consider starting with something like this: 例如,我考虑从这样的事情开始:

#include <stdio.h>
#include <ctype.h>

int main(){
    char buffer[17];
    size_t bytes;
    int i;
    while (0 < (bytes = fread(buffer, 1, 16, stdin))) {
        for (i = 0; i < bytes / 2; i++) // print out bytes, 2 at a time
            printf("%02x%02x ", buffer[i * 2], buffer[i * 2 + 1]);
        if (i * 2 < bytes)              // take care of (possible) odd byte
            printf("%02x   ", buffer[i * 2]);

        for (; i < 8; i++)              // right pad hex bytes
            printf("     ");

        for (i = 0; i < bytes; i++)     // change unprintable to '.'
            if (!isprint(buffer[i]))
                buffer[i] = '.';

        buffer[i] = '\0';               // terminate string
        printf("\t%s\n", buffer);   // print out characters
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM