Jump to content

File to byte array function?


Delta
 Share

Recommended Posts

I did a bit of search but since I'm not exactly sure what I'm searching for I found little results. Essentially I'm trying to load a file into an array. _FileReadToArray does this on a line by line basis but I want to do it on a byte by byte basis. Does such a function exist to spit a file or string into an array by bytes?

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

By bytes or by character, such as converting spaces to the number 32, the letter "a" to the number 97, etc.?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I'm not sure which I'm going to need so I suppose for now both. The end result of this whole endeavor is to hopefully stream a media file from autoit to another program such as VLC, WMP, flash, etc.

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

That will open the file but that won't split it which is fine if I can split what's returned and stick it in an array.

Edit: Ah wait I think I see where you're going with this. FileRead has an optional parameter to read a number of characters at a time. Combine that with _ArrayAdd and I think I have my solution!

Edited by Delta

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

How about this:

$file = FileOpen("test.txt", 16)
$Text = FileRead($file)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Text = ' & $Text & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

Line 1 opens the file in BINARY mode, line 2 reads the entire file to the $Text string, line 3 displays what is inside the variable $Text in SciTE's console.

This is what JohnOne was referring to.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Well Depending on what is in the file.

A simple file read would suffice, and a StringSplit($file,"") will give you your array.

Each ascii char is one byte, so transfer that to a dllstruct byte array, and your there you are.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Ah I'm an idiot. Been too long since I've done any coding. I just stumbled over using stringsplit without a delimiter in the help file a few seconds ago which was exactly what I was looking for. Thanks for the help guys!

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

  • 4 years later...

Hello, layman here(non programmer).

This post got me curious. Is it possible to convert a file such as a pdf to a continuous stream of bytes, and later generate that same pdf file using nothing but the stream of bytes.

Here are the uses of such an operation,

Say you want to hide a file from other users, but dont want others to find out that you are hiding something from them. In such a scenario, all you have to do is,

  1. convert the file into its byte array,
  2. Save that byte array into a text file (Create a text file with a huge wall of text like some 100s of pages long novel. Paste the byte array somewhere in the middle of the text so that nobody stumbles upon it.)
  3. Delete the original pdf file.

So, essentially you have hidden the pdf file within the text file and no one is even aware that you are hiding it from them.

Later, when you want to recover the file, just copy the byte array into an autoit script that can generate the file as output(if possible).

 

So, what i want to know is, is it possible to do such an operation using Autoit v3.3.14.1?

Also, a list of the functions required (or a working code) would be much appreciated

 

Thanks.

Link to comment
Share on other sites

Thanks JohnOne for the quick reply

Here is where i got to so far.

I use this code to get the binary data of the required file

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <MsgBoxConstants.au3>
$file = FileOpen("D:\testing\new  4.zip", 16)
$Text = FileRead($file)
Clipput($Text)
MsgBox($MB_SYSTEMMODAL,"Byte Stream",$Text)

"D:\testing\new  4.zip" is an existing file that i want to operate upon.

After running this code, I can see the resulting byte stream in the Message Box and it is also pasted in the Clip Board if i want to save it somewhere.

Next thing is I want to Generate the file from the byte stream.

So, I use this code to do that after i have put the byte stream in the Clip Board.

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
$sFilePath = "D:\testing\New folder\new  4.zip"
Local $hFileOpen = FileOpen($sFilePath, $FO_BINARY)
MsgBox($MB_SYSTEMMODAL,"Result",$hFileOpen)
$sData=ClipGet()
FileWrite($hFileOpen,$sData)

"D:\testing\New folder\new  4.zip" does not exist, so I want it to be created with the same contents as "D:\testing\new  4.zip"

FileWrite says it can write data to a previously opened file. Hence I have to use the FileOpen command before FileWrite.

But after running this code, I get $hFileOpen as -1 indicating a failure.

What am i doing wrong?

Link to comment
Share on other sites

17 minutes ago, mikell said:

You didn't open the file in write mode  :)

Well FileOpen has to have

  1. $FO_APPEND because I'm writing to the file
  2. $FO_BINARY because the contents are in binary (isn't it?)
  3. $FO_CREATEPATH because it has to create the file.

So, i have to give a combination of these. The help file says i can do that but I'm not sure how.

Thanks in advance.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...