Re: how to know a binary or text file and show all the information in file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 19-12-07 11:00, C_C_Kuo@xxxxxxxxxxxxxx wrote:

I have a file which content is like below 100f1010 0f100f10 0e100e10 (The
format is ASCII text, with CRLF line terminators, which I got by typing
"file myfile" and thanks Manish and Grigoriy ). Right now I want to
transfer it to hex format each 2 chars and save it as binary file like
below:

0f      10    0f      10       original input
Ox0f    0x10  0x0f    0x10     final output
0e      10    0e     10        original input
0x0e    0x10  0x0e   0x10      final output

I expect you don't actually. Since you're probably on a little-endian machine it's more likely that you want to turn the hexademical textual representation "0x0f100f10" of the number 0x0f100f10 into the 4-byte little-endian binary representation "0x10 0x0f 0x10 0x0f" of said number; generally, turn the textual representation "12345678" of the number 0x12345678 into the "0x78 0x56 0x34 0x12" binary one.

You can just use scanf() / fwrite. An extremely simple tool to do what you want would be:

===
#include <stdio.h>

int main(void)
{
	unsigned long val;

	while (scanf("%lx", &val) != EOF)
		fwrite(&val, sizeof val, 1, stdout);

	return 0;
}
===

(works as a filter. Ie, "./the_program <infile >outfile").

The binary output from this is native endian. If you really _do_ need to output big-endian always stick a val = htonl(val) in between the scanf and the fwrite: "network byte order" is big-endian.

Note by the way that libc already caches reads and therefore doing things one character or "thing" at a time isn't particularly inefficient and since it tends to simplify things greatly is generally preferred.

Second, note this is rather off-topic...

Rene.


--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux