Jump to content
Sign in to follow this  
JRowe

Reading binary contents of a file as text?

Recommended Posts

JRowe

Does anyone know an easy method of converting the binary contents of a file to a string, and vice versa? I'd like to see what files look like as pictures, and I thought that if I could convert the binary contents to strings, then I could create 2D arrays of pixels representing 8 bit sections of the binary.

Share this post


Link to post
Share on other sites
JRowe

Ha. 5 minutes of poking around, and I got this: It's a little rough, but it gives you the binary contents of a file as a string displayed in an edit box. You have to type in the name of a file manually in the input box.

CODE
#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Form1", 554, 299, 193, 133)

$Edit1 = GUICtrlCreateEdit("", 8, 8, 537, 249)

$Button1 = GUICtrlCreateButton("Button1", 16, 264, 75, 25, 0)

$Input1 = GUICtrlCreateInput("Input1", 104, 264, 441, 24)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Button1

_GetBinary()

EndSwitch

WEnd

Func _GetBinary()

$readFile = GUICtrlRead($Input1)

$file = FileOpen($readFile, 16)

While 1

$bytes = FileRead($file)

If @error = -1 Then ExitLoop

GUICtrlSetData($Edit1, $bytes)

Wend

FileClose($file)

EndFunc

What I'd like to do is read the binary contents and convert files to gifs, with a standardized format, just cause I think it would be neat to see that type of thing. So now I'm trying to figure out how to format gifs muttley

This is a 10x10 red gif:

This is the binary for it :)

0x4749463839610A000A00800000FF000000000021F90400000000002C000000000A000A00000208848FA9CBED0F632B003B

Anyway, this should be relatively easy to whip up.

Share this post


Link to post
Share on other sites
JRowe

Or not. Wow, things get complicated. I think I'm going to go with 24 bit bitmaps, as that seems to be the most straightforward, readable format so far.

Share this post


Link to post
Share on other sites
JRowe

Share this post


Link to post
Share on other sites
Andreik

Does anyone know an easy method of converting the binary contents of a file to a string, and vice versa? I'd like to see what files look like as pictures, and I thought that if I could convert the binary contents to strings, then I could create 2D arrays of pixels representing 8 bit sections of the binary.

BinaryToString()

StringToBinary()


When the words fail... music speaks

Share this post


Link to post
Share on other sites
JRowe

Share this post


Link to post
Share on other sites
YellowLab

Anyone know of a "string to bitmap" function? All the rest is easy enough to do muttley

http://www.freevbcode.com/ShowCode.asp?ID=1906

It is VB, but may be able to be converted.

Good luck - sounds like an interesting concept.

Bob


You can't see a rainbow without first experiencing the rain.

Share this post


Link to post
Share on other sites
JRowe

Bitmaps seem to be the easiest way to go about doing this, since the structure is really simple. You have some bytes representing the structure at the beginning, then a long list of colors, more structuring, and an EOF marker. I just need to figure out how the format works muttley

woot. I found this http://en.wikipedia.org/wiki/BMP_file_format

Edited by Jrowe

Share this post


Link to post
Share on other sites
JRowe

Wow, interesting stuff. So, in a given bitmap, there's 18 sections:

The file ID and header, which contains

  • 424D - Identifies the file as a bitmap
  • 00000000 to FFFFFFFF - The size of the bitmap in bytes
  • 0000 to FFFF application signature field
  • 0000 to FFFF application signature field
  • 4 byte value of the offset where bitmap data begins (actual array of colors)
  • 28000000 - the size of the DIB header, which starts here
  • 0A000000 - The width of the bitmap in pixels (signed int)
  • 0A000000 - The height of the bitmap in pixels (signed int)
  • 0100 - the number of color planes being used, leave at 0100
  • 1800 - the color depth, bpp setting
  • 00000000 - compression method used, leave at default(none)
  • 42010000 - size of the image data, not the size (WxH) or the file size.
  • 202E0000 - Horizontal resolution (pixel per meter)
  • 202E0000 - Vertical resolution (pixel per meter)
  • 00000000 - Number of colors in the palette, or 0 as default
  • 00000000 - Number of important colors used, 0 for all and as default. leave as 0
  • The raw image data (WIDTH number of colors followed by a 0000 end of line marker)
  • And an EOF marker, which is a 0000 tag after the last 0000 marker.
Image data in BBGGRR format.

This image's binary data is as follows:

CODE

424D

78010000

0000

0000

36000000

28000000

0A000000

0A000000

0100

1800

00000000

42010000

202E0000

202E0000

00000000

00000000

FFFFFF00FFF0FFFFFFFFFFFF176F0E00FFF0FFFFFFFFFFFFFFFFFFFFFFFF0000

FFFFFFFFFFFFFFFFFFFFFFFF176F0EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000

FFFFFFFFFFFF3F3FA2FFFFFF176F0EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000

FFFFFFFFFFFFFFFFFFFFFFFF176F0EFFFFFF176F0EFFFFFFFFFFFF00FFF00000

FFFFFFFFFFFFFFFFFFFFFFFF176F0EFFFFFF3F3FA2FFFFFFFFFFFFFFFFFF0000

FFFFFFFFFFFF176F0EFFFFFF176F0EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000

00FFF0FFFFFFFFFFFFFFFFFF176F0EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000

FFFFFFFFFFFFFFFFFFFFFFFF176F0EFFFFFF3F3FA2FFFFFFFFFFFFFFFFFF0000

FFFFFF3F3FA2FFFFFF176F0E176F0E176F0EFFFFFFFFFFFFFFFFFFFFFFFF0000

FFFFFFFFFFFFFFFFFF176F0EFFFFFFFFFFFFFFFFFFFFFFFF00FFF0FFFFFF0000

0000

Anyway, now that I've taken one apart, I'm going to try a 20x20 image and see what changes.

Once I figure out how to take one apart, then I can create a system for arbitrarily sized bitmaps. Then I can insert any old data, and use the application signature space as a method of tracking how many bytes I had to tack onto the end for formatting. Once that's done, any file can be converted to a bitmap and vice versa. muttley

Share this post


Link to post
Share on other sites
ProgAndy

the wiki-article was intersting muttley With it's help, I converted the VB-Example ithout SetPixel:

$file = FileOpenDialog("Choose Fileto BMPize",@DesktopDir,"All (*.*)")
If Not FileExists($file) Then Exit MsgBox(0, '', "No file Chosen")
$savepath = FileSaveDialog("Choose BMP Name",@DesktopDir,"BMP Files (*.bmp)")
If $savepath = "" Or @error Then Exit MsgBox(0, '', "No BMP name Chosen")
If Not (StringRight($savepath,4) = ".bmp") Then $savepath &= ".bmp"
$handle = FileOpen($file,16)
$len = FileGetSize($file)
$width = Int(Sqrt($len))
$height = $width + 2

; Fill up lines to multiply of 4 bytes
$zusatz = Mod($width*3,4)
If $zusatz Then $zusatz = StringTrimLeft("00000000",$zusatz*2)

$bmp = "" ; Temp Variable
For $n = 1 To $height
;~ q(n) = 255 - Asc(Mid$(t, n, 1))
For $i = 1 To $width
    $data = FileRead($handle,1)
    If @error Then
        $bmp &= "FFFFFF" ; Fill up empty bytes with white ( end of file )
    Else
        $bmp &= "0000" & Hex(255-Dec(Hex($data,2)),2)
    EndIf
Next
$bmp &= $zusatz
Next
FileClose($handle)

$lentgh = Hex(Binary(54+BinaryLen($bmp)),8)
$bmpHeader = "0x424D" & $lentgh & "00000000" & "36000000"
$bmpHeader &= "28000000" & StringLeft(Hex(Binary($width),8),8) & StringLeft(Hex(Binary(-1*$height),8),8) & "0100" & "1800"
$bmpHeader &= "00000000" & Hex(Binary(BinaryLen($bmp)),8) & "202E0000" & "202E0000" & "00000000" & "00000000"
$write = FileOpen($savepath,18)
FileWrite($write,$bmpHeader)
FileWrite($write,"0x" & $bmp )
ShellExecute($savepath)

//Edit : This Version has a Bug, get the corrected one in #14

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
YellowLab

Interesting.

Opening text files produces a lot of red. Is this due to the byte values of the letters coordinating with that color?

Would be interesting to apply some sort of scalar to the text value to gradate the colors more and give more vibrant images.

Bob


You can't see a rainbow without first experiencing the rain.

Share this post


Link to post
Share on other sites
JRowe

Neat :P I'm definitely gonna enjoy playing with this. Not practical, at all, but hey, it's still very neat. I want to get a converter done, so you can turn any file into a pic, and back into a file again.

Share this post


Link to post
Share on other sites
evilskaap

WOW! A few years back i used a hex editor which had a "graphical representation" of whatever file you opened, and it showed very interesting images. This was also useful for getting maps out of game files. The nice thing was that you could change the row size on the fly to adjust the output. Wish i could remember the name....


[font="Lucida Console"]The truth is out there[/font]

Share this post


Link to post
Share on other sites
ProgAndy

I corrected a BUG, when the image nneded no fill-Up of the lines. Alos I added the possibility of scaling the image :P

$file = "D:\settings.txt"

$file = FileOpenDialog("Choose Fileto BMPize",@DesktopDir,"All (*.*)")
If Not FileExists($file) Then Exit MsgBox(0, '', "No file Chosen")
$savepath = FileSaveDialog("Choose BMP Name",@DesktopDir,"BMP Files (*.bmp)")
If $savepath = "" Or @error Then Exit MsgBox(0, '', "No BMP name Chosen")
If Not (StringRight($savepath,4) = ".bmp") Then $savepath &= ".bmp"
$handle = FileOpen($file,16)
$len = FileGetSize($file)
$width = +Int(Sqrt($len))
$height = +$width + 2

$Input = InputBox("Bitmap Scale","What sould the Line Height be? ( in pixels )",2)
$pixelSizeY = Int($Input+($Input<1))
$Input = InputBox("Bitmap Scale","What should the width of one byte-square be? ( in pixels )",$pixelSizeY )
$pixelSizeX = Int($Input+($Input<1))

$width *= $pixelSizeX
$height *= $pixelSizeY

$zusatz = Mod($width*3,4)
If $zusatz Then 
    $zusatz = StringTrimLeft("00000000",$zusatz*2)
Else
    $zusatz = ""
EndIf
$bmp = ""
For $n = 1 To $height Step $pixelSizeY
 $line = ""
For $i = 1 To $width Step $pixelSizeX
    $data = FileRead($handle,1)
    If @error Then
        For $k = 1 To $pixelSizeX
            $line &= "FFFFFF"
        Next
    Else
        For $k = 1 To $pixelSizeX
            $line &= "0000" & Hex(255-Dec(Hex($data,2)),2)
        Next
    EndIf
Next
For $k = 1 To $pixelSizeY
    $bmp &= $line & $zusatz
Next
;~ $bmp &= $zusatz
Next
FileClose($handle)

$lentgh = Hex(Binary(54+BinaryLen($bmp)),8)
$bmpHeader = "0x424D" & $lentgh & "0000"&"0000" & "36000000"
$bmpHeader &= "28000000" & StringLeft(Hex(Binary($width),8),8) & StringLeft(Hex(Binary(-1*$height),8),8) & "0100" & "1800"
$bmpHeader &= "00000000" & Hex(Binary(BinaryLen($bmp)),8) & "202E0000" & "202E0000" & "00000000" & "00000000"
MsgBox(0, '', BinaryLen($bmpHeader))
$write = FileOpen($savepath,18)
FileWrite($write,$bmpHeader)
FileWrite($write,"0x" & $bmp )
ShellExecute($savepath)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×