News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Save bits/bytes as text file

Started by vikernes, February 24, 2006, 10:37:44 AM

Previous topic - Next topic

vikernes

Hi,

I'm pretty new to Assembly but I'm trying to learn how to open a file (in binary mode) and save it's bits or bytes as text in a text file.

Can anyone help me out, provide some examples or point me in the right direction?

I'm trying to learn MASM.

Tedd

Files are always opened in binary mode. The way you get 'text mode' is just from the programs you use which decide to do special things with certain bytes (tabs, spaces, newlines, etc) and possibly complain if they find anything that isn't considered 'normal' text.
Anyway..

Once you've learned to do a simple windows app (if not, go away, learn, and come back :wink) opening a file is as simple as using the CreateFile function with appropriate parameters (filename and a few others.)
The you use ReadFile to read some or all of its contents.
And the you CloseHandle to close the file and clean up like a good little progrmmer :bg

Writing to file is almost exactly the same, expect that you don't ReadFile, you WriteFile. Simple, eh?

So the only thing in between is to converting the file contents to text (a hex dump?)

Shout again for further help :wink
(But it helps if you've actually tried to do something first, so we can help you fix that, rather than writing the whole thing for you - in which case you should just download an appropriate tool instead.)
No snowflake in an avalanche feels responsible.

donkey

Quote from: vikernes on February 24, 2006, 10:37:44 AM
Hi,

I'm pretty new to Assembly but I'm trying to learn how to open a file (in binary mode) and save it's bits or bytes as text in a text file.

Can anyone help me out, provide some examples or point me in the right direction?

I'm trying to learn MASM.

Hi vikernes,


First off welcome to assembler !

A text file like any other file on your PC is just a string of binary numbers, many different langauges have file preprocessors that expect specific data formats and for that reason you have to open them using different "modes". Assembler on the other hand only ever deals with the actual data so the letter "A" is seen as 042h as is the byte value 042H and the low order byte of 01231242h, they are all the same. The only difference between them is what function is used to display them and translate these numbers for human consumption. So as Tedd said, all files are actually opened in binary mode and in reality even for text files you are only manipulating numbers, however in text files, a specific number represents a specific character than will be displayed if the program opening it translates it to human readable text. The important thing to remember here is that all the processor can ever really see is a stream of numbers, nothing else. So just open you file in "binary mode" and manipulate it as usual.

There is however an advantage to knowing how your file will be accessed, whether sequentially or randomly as passing a hint to Windows can speed up access. For text files you are almost always better to use FILE_FLAG_SEQUENTIAL_SCAN with CreateFile as text files are usually read from start to finish without jumping aronnd.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

Hi vikernes,

You can check Iczelion's win32asm tutorials, some of them are focussed on reading and writing files.

vikernes

Hi, sorry about this late reply.

Thanks for all the help, I decided to learn more of the basics of Assembly before continueing with that project.
I will check out those tutorials.