Issue with binary to ascii conversion

V

Vic

I have a c program which writes mac address entries onto a text file.
Text file when opened in vim looks like this

index mac
1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01)
2 ^@^@^Q^@^@^B ascii of (00.00.11.00.00.02)
.. ........
Likewise
I read the mac entries from the text file using a perl script and do
some processing
The problem occurs when i have a mac entry with last octet 0x0A, the
entry looks like this
idx ^@^@^Q^@^@ ascii of (00.00.11.00.00.0A)

This happens because ascii for 0x0A ('^J') is line feed for Unix text
editors. Hence the mac read using perl script is incorrect.
Can i get a solution for this so as to
1) Avoid a new line in text file
2) Get correct value(0x0A) read in the perl script

Thanks in advance
V
 
W

Walter Roberson

I have a c program which writes mac address entries onto a text file.
Text file when opened in vim looks like this
index mac
1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01)
2 ^@^@^Q^@^@^B ascii of (00.00.11.00.00.02)
. ........
Likewise
I read the mac entries from the text file using a perl script and do
some processing
The problem occurs when i have a mac entry with last octet 0x0A, the
entry looks like this
idx ^@^@^Q^@^@ ascii of (00.00.11.00.00.0A)
This happens because ascii for 0x0A ('^J') is line feed for Unix text
editors.

What you are doing (writing binary to a text file) is inherently
unsafe. Either write a binary file instead, or use some kind of
encoding (e.g., uuencode or quoted-printable) so that you never
output any unsafe characters to the text file. There are
only a few control characters can be safely sent to a text stream
in C (and the numeric values of those safe characters are
not specified within the C standard -- C just assures you that
they will exist.)
 
K

Keith Thompson

Vic said:
I have a c program which writes mac address entries onto a text file.
Text file when opened in vim looks like this

index mac
1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01)
2 ^@^@^Q^@^@^B ascii of (00.00.11.00.00.02)
. ........
Likewise
I read the mac entries from the text file using a perl script and do
some processing
The problem occurs when i have a mac entry with last octet 0x0A, the
entry looks like this
idx ^@^@^Q^@^@ ascii of (00.00.11.00.00.0A)

This happens because ascii for 0x0A ('^J') is line feed for Unix text
editors. Hence the mac read using perl script is incorrect.
Can i get a solution for this so as to
1) Avoid a new line in text file
2) Get correct value(0x0A) read in the perl script

Writing non-printable characters to a text file is hazardous, as
you've seen.

If you read a file in text mode in a C program, using, say, fgets(),
and some of the lines contain null characters, you'll get the data but
it will appear to be truncated whenever you do anything that treats it
as a string. <OT>Perl doesn't use '\0' as a string terminator, so
this probably isn't an issue for what you're doing.</OT> But, as
you've seen, whenever you write binary data that happens to match the
representation for new-line, you'll get a genuine new-line in the
file.

You have (at least) two choices. You can use a binary file, which
means you'll have to open it in binary mode (fopen("name", "wb")) in
your C program, *and* you'll have to rigorously define your output
format (e.g., you'll need an unambiguous way of determining the end of
each record, either by a marker, by a byte count, or something else).
Or you can use text, which means you'll need some scheme for encoding
non-printable characters as printable characters. The
"quoted-printable" format is one possibility. It's likely that you
can get away with writing *some* non-printable characters to a text
file, but it's easier and safer to encode everything in printable
form.

<OT>Perl is cabable of dealing with either kind of input file.</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top