Display hexadecimal File Content

Source: Internet
Author: User

Today, I saw a program to display file content in the hexadecimal format. I ran a script on the machine and found that the hexadecimal format was not fully displayed. After GDB debugging, I found that the sprintf format was not correct, A space-_-is missing in the book -_-!

To put it bluntly, go to the Code:

/* <Br/> * = ========================================================== =============< br/> * <br/> * filename: myoD. c <br/> * Description: OD <br/> * version: 1.0 <br/> * created: 06/24/2011 02:52:00 <br/> * revision: None <br/> * Compiler: GCC <br/> * Author: Your name (), <br/> * company: <br/> * = ====================================== =================================< Br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> int main (INT argc, char * argv []) <br/>{< br/> int N, C; <br/> file * FP; <br/> char Buf [101], hexbuf [101]; <br/> If (argc! = 2) <br/>{< br/> fprintf (stderr, "Usage: % s filename/N", argv [0]); <br/> exit (0); <br/>}< br/> If (FP = fopen (argv [1], "R") = NULL) <br/>{< br/> perror ("fopen"); <br/> exit (0); <br/>}< br/> n = 0; <br/> memset (BUF, 0x00, sizeof (BUF); <br/> memset (hexbuf, 0x00, sizeof (hexbuf )); <br/> while (C = fgetc (FP ))! = EOF) <br/>{< br/> If (n> 15) <br/>{< br/> fprintf (stderr, "%-48 S % s/n", hexbuf, Buf); <br/> n = 0; <br/> memset (BUF, 0x00, sizeof (BUF); <br/> memset (hexbuf, 0x00, sizeof (hexbuf )); <br/>}< br/> Buf [N] = (unsigned char) C; <br/> sprintf (hexbuf + 3 * n, "% 02x", C ); <br/> N ++; <br/>}< br/> fprintf (stderr, "%-48 S % s/n", hexbuf, Buf ); <br/> fclose (FP); <br/> return 0; <br/>}

 

Code running result:

[Root @ localhost linuxc] #. /MyoD. /stdout. redirect <br/> 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6D this is a test m <br/> 65 73 73 61 67 65 2E 0a essage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.