Computer Hope

Software => Computer programming => Topic started by: sal.ventre on January 13, 2018, 03:00:22 AM

Title: File conversion to .txt file
Post by: sal.ventre on January 13, 2018, 03:00:22 AM
Hi there everyone!

SHORT QUESTION: Is there a tool or code to convert a file (e.g. .EXE, .JPG, .PDF) to a .TXT file and vice versa?

LONG QUESTION: In 1997 I made a program (using Qbasic in DOS on my PC) that converted any file to ASCII (.TXT) and then encrypted it changing every character in a string and then doing other "transformation" (of course also vice versa). It's long time I haven't programmed and now I want to start again by making such a program (maybe using Visual Basic) but I can't find a tool to convert a normal file to ASCII. In that time a friend of mine gave me a converter (a DOS tool, something.COM) but I can't remember the name nor have I found it.
So I am asking if there is a separate program that does the conversion or if there is a code I can use in Visual Basic.

Thank you!

Salvatore
Title: Re: File conversion to .txt file
Post by: BC_Programmer on January 13, 2018, 07:43:11 AM
You can't convert binary files into ASCII text.

You can create representations of the binary files contents in ASCII- by say writing the file contents as hexadecimal.
Title: Re: File conversion to .txt file
Post by: sal.ventre on January 13, 2018, 08:06:05 AM
Oh, thank you, that's what I needed to know...
So, if I use C, how can my program make this representation (and of course the reverse)? Is there a code to get in?
Thank you again.  :)
Salvatore
Title: Re: File conversion to .txt file
Post by: Salmon Trout on January 13, 2018, 09:01:44 AM
You read the binary program, byte by byte, and for each byte, write out its hexadecimal representation, thus, if a byte had a value of 1, you'd write 01, and if it was 255, you'd write FF. To do the reverse is trivial.



Title: Re: File conversion to .txt file
Post by: sal.ventre on January 13, 2018, 10:31:03 AM
Ok, it's so clear and interesting!
Another question: every file is binary (for example, .JPG, .PDF, .EXE... even .TXT), isn't it? .
And in the file architecture, bits are organised in bytes, aren't they?
As written in first answer, what I'm doing is just to "represent" every byte in hexadecimal, am I right?
Thank you!
Salvatore
Title: Re: File conversion to .txt file
Post by: Mark. on January 13, 2018, 01:21:57 PM
curious as to why and what benefit you get from 'converting', say, a .EXE or .JPG file to a .TXT file?
if you just want to 'look' or heaven forbid 'tweak' at the binary data why not use a hex editor?
Title: Re: File conversion to .txt file
Post by: Salmon Trout on January 13, 2018, 01:45:35 PM
why not use a hex editor?
++1
Title: Re: File conversion to .txt file
Post by: sal.ventre on January 14, 2018, 02:12:47 AM
Ok, maybe the "problem" is my 1997-Qbasic way of thinking...
So, what you all are telling me is that I can read every byte from a file (any file) and then encrypt it (that's the purpose of my project)...
And I don't need to "convert" to .TXT, as I did in 1997 with my Qbasic program...
Now I'm starting learning C and the final/update/real question (spoiler!) is: which is the C command  (or the code) to read any single byte from a file to use it as a variable? And how can I do the reverse?
Title: Re: File conversion to .txt file
Post by: Mark. on January 14, 2018, 04:40:13 AM
I'm still at a loss as to WHY ?
you seem intent on re-inventing the wheel.

if this is some sort of logic problem or coding test, OK then, but there are already programs out there that will do what you want, that is, look at any sort of file at the binary (hex) level and if desired, also encrypt that file.
Title: Re: File conversion to .txt file
Post by: sal.ventre on January 14, 2018, 05:41:34 AM
I'm still at a loss as to WHY ?
you seem intent on re-inventing the wheel.

if this is some sort of logic problem or coding test, OK then, but there are already programs out there that will do what you want, that is, look at any sort of file at the binary (hex) level and if desired, also encrypt that file.

Ok... why? Maybe I want to re-invent the wheel...  :) The point is that I want to start again with programming and - as a sort of exercise - I want to make again that program that I create in 1997... There is no why... I'm not a professional, it's my hobby...  ;D So, as you were saying, it's like a coding test... Where do I start? I've just started learning C, so I'll arrive to the point, but I just wanted a "spoiler".  ;D So, now I know that what I want to do is possible (even if in a different way than I thought), but I'd like to know also how.

P.S. What's "++1" above?
Title: Re: File conversion to .txt file
Post by: Salmon Trout on January 14, 2018, 05:51:12 AM
P.S. What's "++1" above?
A compact way of saying "I agree with the above".

You would be better looking at Stack Exchange or Rosetta Code.


Title: Re: File conversion to .txt file
Post by: BC_Programmer on January 14, 2018, 01:04:41 PM
Code: [Select]
which is the C command  (or the code) to read any single byte from a file to use it as a variable? And how can I do the reverse?

It's unclear from what context you are asking this question; even in QBASIC there was no "command" (or statement) that would read a single byte from a file to a variable or the reverse. It was a series of operations that you put together.

There is loads of documentation for the C language and it's standard libraries. Generally speaking, sticking to C, you'd open the file with fopen(), read from it with fread(), translate it to a hexadecimal character array using one of the formatted print functions(printf, fprintf, etc) and close the file using fclose().
Title: Re: File conversion to .txt file
Post by: sal.ventre on January 14, 2018, 01:21:11 PM
Code: [Select]
which is the C command  (or the code) to read any single byte from a file to use it as a variable? And how can I do the reverse?

It's unclear from what context you are asking this question; even in QBASIC there was no "command" (or statement) that would read a single byte from a file to a variable or the reverse. It was a series of operations that you put together.

There is loads of documentation for the C language and it's standard libraries. Generally speaking, sticking to C, you'd open the file with fopen(), read from it with fread(), translate it to a hexadecimal character array using one of the formatted print functions(printf, fprintf, etc) and close the file using fclose().

Ok, thank you... This is what I needed to know... I didn't know that this was possible even in Qbasic... I'm an autodidact, so I don't know the whole theory...