Jump to content

Writing raw bytes to a file


Recommended Posts

Hi guys,

I've been at this for a little while, so I thought I would turn to the community for what should be a simple answer to my problem.

I want to write raw data to a file. Now, when I say raw data, I mean I want to write exactly the bytes I want in the file, not the hex equivalant of the characters I send. To be clearer, let me show you me code and explain what's wrong with it:

$fOpen = FileOpen(@DesktopDir&"\icons.txt",18)

For $d = 15 To 15

For $c = 12 To 12

For $b = 0 To 15

For $a = 0 To 15

$temp = binary(Hex($b, 1) & Hex($a, 1) & Hex($c, 1) & Hex($d, 1))

FileWrite($fOpen,$temp)

Next

Next

Next

Next

FileClose($fOpen)

(I can't post in the code box right now due to a poor internet connection. Sorry guys.)

Well I've tried different flags or ways about converting the 4 letter hex code into binary so it will write to the file byte by byte.

Nothing has worked so far.

To be clear. I'm looking specifically to write raw data to a file; not the data equivalant of the text I am writing.

i.e.

4E4F is the equivalant to "NO" in a hex editor. I want to write "4E4F" NOT the Hex equivalant.

Thanks before hand for the help guys. :)

Link to comment
Share on other sites

  • Moderators

crislivinitup,

If I have understood you correctly, then this does what you want: :)

; Plain ASCII text
$sText = "NO"

; Convert to binary
$bText = StringToBinary($sText)
MsgBox(0, "Binary", $bText)

; Remove the 0x
$bData = StringTrimLeft($bText, 2)

; Write it to a file
FileWrite("Test.txt", $bData)

; And this is what is in the file
MsgBox(0, "File Content", FileRead("Test.txt"))

If not, then please explain more clearly. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Danwili! I got it! Ok. I would like to clarify the solution for anybody who is interested.

This is the working script:

$fOpen = FileOpen(@DesktopDir & "icons.txt", 18)
For $d = 15 To 15
For $c = 12 To 12
  For $b = 0 To 15
   For $a = 0 To 15
    $temp = String(Hex($b, 1) & Hex($a, 1) & Hex($c, 1) & Hex($d, 1))
    $temp = "0x" & $temp
    FileWrite($fOpen, $temp)
   Next
  Next
Next
Next
FileClose($fOpen)

This was my old script

$fOpen = FileOpen(@DesktopDir&"icons.txt",18)
For $d = 15 To 15
For $c = 12 To 12
For $b = 0 To 15
For $a = 0 To 15
$temp = binary(Hex($b, 1) & Hex($a, 1) & Hex($c, 1) & Hex($d, 1))
FileWrite($fOpen,$temp)
Next
Next
Next
Next
FileClose($fOpen)

My problem was that I was working with hex but I did not include the "0x" as I should have. Daniwilli pointed this out.

If you look at the working code I posted you can see that I made $temp into a string containing the hex values, which however is not considered a hex value in AutoIT UNLESS preceded by a "0x". So I followed by adding an "0x" to the beginning and then $temp was considered a hex/binary value (almost the same thing I guess... I was originally trying to make it into a binary value before passing it to filewrite).

Thanks guys.

Link to comment
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
 Share

×
×
  • Create New...