473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Issue with binary to ascii conversion

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
Mar 22 '08 #1
1 2574
Vic <ve**********@gmail.comwrites:
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>

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ferran Foz | last post by:
Hello, I'm using ADODB.Stream to open a binary file on the server and write it down to the browser using Response.BinaryWrite. It's working fine, but i need to make some changes to the binary...
7
by: Drake | last post by:
Well, I'm stuck in legacy land and I need a helping hand. We're trying to give some modern value-added functionality to a circa-1985 fortran proggie. The program produces a binary file, by...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
6
by: SandyMan | last post by:
Hi, I am able to open a binary file for reading but can someone tell me as how to go about converting a Binary file to ASCII file using C. Thanks In Advance SandyMan
10
by: Wrecked | last post by:
Could someone suggest a method to convert a Binary data to an ASCII data, with very less or no increase in the memory . The problem basically is there is an excrypted message which needs to be...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
24
by: pbd22 | last post by:
Hi. I want to know the size of a file (prior to FTP) in ASCII Mode. The reason is that I want to know how bit the file is going to be once it reaches the FTP server and there is a size...
12
by: fermineutron | last post by:
I am trying to write a function which will convert a large ascii number, say 100 ascii digits, to its binary representation. It seems that evey algorithm I am trying to think of is backwards. ...
7
by: azrael | last post by:
Hy folks, I googled, and searched, and can not bealive that I have not found a built in way to convert the easy and elegant python way a function to easily convert simple ascii data to binary...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.