Convert a binary file to letters.
Convert a binary file to letters.
Hello my name is Vinostien, and I am new to computers, I grew up with out electricty, and coding is very new to me, my question is? I have a file in binay and I would like to convert the 1's and 0's into letters. So I started with Result=Readfile(#file,filename$) but that would not let me get the file info into a string. so then I used Number.b=ReadByte(#file) but that 'Number.b' gives a syntax error, next I tried Number.w=ReadWord(#file) but again the 'Number.w' stops me. At this point I am lost in the coding, If there is anyone that would like to help me I would like that very much.
vinostien
vinostien
Re: Convert a binary file to letters.
Hello Vinostien, congrats on trying out PureBasic.
I'm not sure why Number.b or Number.w would give you a syntax error... are you sure it isn't the #file or filename$ (#file is just a placeholder for an integer File ID).
Here is an example to get you started:
Be sure to look through the File section of the PB help (or the whole thing) for some examples and explanations.
I'm not sure why Number.b or Number.w would give you a syntax error... are you sure it isn't the #file or filename$ (#file is just a placeholder for an integer File ID).
Here is an example to get you started:
Code: Select all
; Select any (small?) binary file
File.s = OpenFileRequester("Binary File", "", "Any File|*.*", 0)
; Check that the user did not cancel
If (File)
; Try to read the source file
If (ReadFile(0, File))
; Create a new file (add .txt to the original file)
NewFile.s = File + ".txt"
If (CreateFile(1, NewFile))
; Read until the source file is done
While (Not Eof(0))
; Read one byte from file #0
Number.b = ReadByte(0)
; Convert the byte to a binary text string
BitString.s = Bin(Number)
; Pad the string with zeros, so it is eight characters wide
BitString = RSet(BitString, 8, "0")
; Write the text to file #1 (one per line)
WriteStringN(1, BitString)
Wend
; Close file when done!
CloseFile(1)
; Open the new file
RunProgram(NewFile)
EndIf
; Close file when done!
CloseFile(0)
EndIf
EndIf
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Convert a binary file to letters.
Well, in the real world, you generally need to know the format of the file in order to translate it successfully. Many binary files consist of data that includes both strings and numbers (integers, doubles, floats). If the binary file is a propriety format it may have it's own special data type delimiters and/or may require lines of data to be padded so they are of equal/special length. So, lots of hurdles but usually you can find info on the file format to be read, goggle is your friend there.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Convert a binary file to letters.
Thanks
Your start to my program problem was cured, I had a virus that null my point " . ", that is why all my Number.b was a syntar error, I worked on it for days and got to thinking it was bad coding or I just did not know what I was tring to do., So thanks to you and a friend with a antivirus program I am back in coding again. And now to run a test of your program and my program on the binary file.
thanks again vinostien
Your start to my program problem was cured, I had a virus that null my point " . ", that is why all my Number.b was a syntar error, I worked on it for days and got to thinking it was bad coding or I just did not know what I was tring to do., So thanks to you and a friend with a antivirus program I am back in coding again. And now to run a test of your program and my program on the binary file.
thanks again vinostien
Re: Convert a binary file to letters.
Hello, I ran both programs again and both failed to read and write the 1's and 0's, the file is only 1's and 0's, when I open the file 'with nopepad' all I see is 1's and 0's, I know that all info is saved to the hard drive in binary but this file is writen and saved in binary, and all I need is to convert the binary format into letters, don't need to read the binary code writen on the harddrive, only the 1's and 0's in the file.
My program uses the val() and cha(), but I can't get only 8 numbers out of the file at a time, If I had more info in readdata() that just might be the answar, I do not know where to go from here?
I do thank you for the test file, it has given me some ideas in coding.
vinostien
My program uses the val() and cha(), but I can't get only 8 numbers out of the file at a time, If I had more info in readdata() that just might be the answar, I do not know where to go from here?
I do thank you for the test file, it has given me some ideas in coding.
vinostien
Re: Convert a binary file to letters.
Hello\, I rewrote the code, I use VAL() and CHR() to convert the code, but the trouble I am having is that I need each group of 8 numbers at a time to convert, and have the code go to the next 8 numbers, ie,,, on the for x = 0 to 99 that would give me 100 sets of 8 that would convert to 100 letters at a time.
vinostien
vinostien
Re: Convert a binary file to letters.
I think here is a little misundertanding.
You dont read bit by bit, the smallest unit you can directly access is a byte. Dont think about reading 0 and 1 because you dont. You read a number that is at least 8 bit long. To display the character this number represents you can use the chr command, but you need to filter out control characters like 0, because a 0 marks the end of a string.
You dont need to convert the bits to a number, the CPU allready handles them as a number.
You dont read bit by bit, the smallest unit you can directly access is a byte. Dont think about reading 0 and 1 because you dont. You read a number that is at least 8 bit long. To display the character this number represents you can use the chr command, but you need to filter out control characters like 0, because a 0 marks the end of a string.
You dont need to convert the bits to a number, the CPU allready handles them as a number.
Re: Convert a binary file to letters.
Thanks,, I can pull the file apart in binary, but the file is saved in binary, wrote in binary, there the file ie.. is wrote like this 001100011101011001010, and then saved to the harddrive in binary, so each 1 or 0 has 8 bytes, no letters and only
numbers, I am tring to code a program that will pull out a total of 8 numbers at a time, 64 bytes of info to convert to letters and numbers.
thanks vinostien
numbers, I am tring to code a program that will pull out a total of 8 numbers at a time, 64 bytes of info to convert to letters and numbers.
thanks vinostien
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Convert a binary file to letters.
It seems that you are still confused about binary files vinostien - they are not seen as a sequence of ones and zeros. If you have a file that is man-readable and does consist of a sequence of ones and zeros, then you are actually looking at a text file, where the characters '1' and '0' have been used to represent binary. Typically, you would view a binary file in HEX, though that is not exactly man-readable either
Here is a screenshot of an STL (stereolithography) binary file viewed in Ultra Edit:

This Wiki Page: http://en.wikipedia.org/wiki/STL_%28file_format%29
Shows why you need to know the format of a binary file in order to read it correctly.

Here is a screenshot of an STL (stereolithography) binary file viewed in Ultra Edit:

This Wiki Page: http://en.wikipedia.org/wiki/STL_%28file_format%29
Shows why you need to know the format of a binary file in order to read it correctly.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
- Michael Vogel
- Addict
- Posts: 2797
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Convert a binary file to letters.
Maybe you can try to show us such a 10101101... file you have - just load it to the notepad and copy one or two lines. There are a lot of possibilities, why you won't get the results you like to have. Here's just a short example for you, which takes 0/1 values from a data instead of a file, but you can modify this easily to read the data from an opened file...vinostien wrote:Thanks,, I can pull the file apart in binary, but the file is saved in binary, wrote in binary, there the file ie.. is wrote like this 001100011101011001010, and then saved to the harddrive in binary, so each 1 or 0 has 8 bytes, no letters and only
numbers, I am tring to code a program that will pull out a total of 8 numbers at a time, 64 bytes of info to convert to letters and numbers.
thanks vinostien
Code: Select all
DataSection
Data.b 0,1,0,0,1,0,0,0; Bits 0 and 1
Data.b '0','1','0','0','1','0','0','1'; Characters '0' and '1'
EndDataSection
FileLength=16
Chars=FileLength / 8
For i=1 To Chars
Letter=0
For bit=0 To 7
Read.b NullOne
NullOne=NullOne&1; convert Characters to Bits
Letter=Letter * 2 +NullOne
Next bit
Debug Str(Letter)+": "+Bin(Letter,8)+" = " +Chr(Letter)
Next i
Re: Convert a binary file to letters.
Ah, that would make sense.IdeasVacuum wrote:then you are actually looking at a text file, where the characters '1' and '0' have been used to represent binary
@vinostien: If you mean a text file with binary formated numbers in it, say so.
If you tell someone you saved the file in binary format, he would never think of a text file with binary formated numbers.
Binary format on computers is the native format the CPU understands.
Re: Convert a binary file to letters.
Sorry I am so new to computers and don't know the names or words for the terms of this fourm
I am so sorry that I did not know what to call the text file, it did not have a .txt ext on it.
thanks again vinostien
here is a sample "in notepad" of the file I am working with.
so yes this file is a text file of binary 1's and 0'swhen I open the file 'with nopepad' all I see is 1's and 0's, I know that all info is saved to the hard drive in binary but this file is writen and saved in binary,
I am so sorry that I did not know what to call the text file, it did not have a .txt ext on it.
thanks again vinostien
here is a sample "in notepad" of the file I am working with.
Code: Select all
0111000011111111111000000001001100001000000110100000111010101010001001000010100100001101001000010
1011000011110000000001011100000110000110100001010010011111100011111101000000110000010010010001001
1110100101110111110110000001011100010001011111010111100011001100010101110101001010111000110101111
0100011001001000100011010011001011001001111011111000111100011010010011011010100011001001000000101
1110101100101100101000111010110000101011010010100111000100000011101011100100110111110100100010000
0000111001110010010110011001100010011111111001001011011000001101010001010110100011100001111111111
0100101000010100010101011111011100110111000000000000100011010110001100100001000010110111110110001
0011111100000110001001001001011110001001111001110011100111001111001100100110110001111110101000010
0110000011000000110010011100111111110011111110100100111011001111111100001001011010100101001001110
0000010101100101110111101111101000000010011100000110010010111000000000110000001101011110011000010
1011110101010011000010101110000011110100010101111010001100000100011111101010110110010000000110000
0010011111001110011111001101000110011000110101010011100000011100011001010000011101110100111101001
1101001000010011010011101000011011101001111111011000001110101011100100110010110100011001111110101
0100001110001011010011100101011100111000010011110011001101000101100000001111101000000000010001100
0111001001100100000100001010000110011110111000101010001000010001110010000100010001000101010100011
00101001010011100111011000001100000000011111011100111100101110
Re: Convert a binary file to letters.
If that's a notepad representation, you seem to be working with a text file of numeric zeros and ones, not a binary file.
(to my knowledge, notepad does not display binary.)
cheers
(to my knowledge, notepad does not display binary.)
cheers
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Convert a binary file to letters.
Why? What do you hope to accomplish by viewing the file as letters?vinostien wrote: I have a file in binay and I would like to convert the 1's and 0's into letters.
BERESHEIT
Re: Convert a binary file to letters.
Hi,
now it is clear. You want something like this:But unfortunately your example file is not dividable by 8.
Threre is a rest of 4 bits.
And the characters are not a 'valid' text.
Bernd
now it is clear. You want something like this:
Code: Select all
File = ReadFile(#PB_Any, "01.txt")
If File
Text$ = ""
While Not Eof(File)
Text$ + ReadString(File)
Fit = (Len(Text$) / 8) * 8
FitText$ = Left(Text$, Fit)
; Debug FitText$
Text$ = Mid(Text$, Fit)
; Debug Text$
For i = 1 To Fit Step 8
Help$ = "%" + Mid(FitText$, i, 8)
; Debug Help$
Debug Hex(Val(Help$)) + " " + Chr(Val(Help$))
Next i
Wend
CloseFile(File)
EndIf
Threre is a rest of 4 bits.
And the characters are not a 'valid' text.
Bernd