Import Binary File to variable at runtime

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
gtdalsanto
Pro Scripter
Posts: 51
Joined: Tue Oct 03, 2006 4:22 pm
Location: Akron, Ohio
Contact:

Import Binary File to variable at runtime

Post by gtdalsanto » Tue Dec 20, 2011 9:58 pm

Hello,

I have a need to create a screen shot to a jpg file, and am trying to send it via a stored procedure to a sql server database that needs it in binary format. If I go to the toolbar, I can manually Import a file to binary, which creates a variabel at the end of the script with the Binary formatted data I can later export.

What I need is the same functionality from within my script, so that after I create the screen shot jpg file, I want to import the jpg file in binary format to a variable during script execution, then send it via a stored procedure to a sql server database. Is there any way I can do this programatically? I originally loaded the file via the readfile command, then did the base64 encode function. However, they are expecting it in the raw binary format like the manual import utiility. If I cannot do this in MS, is there a way to do it with VBScript?

Thanks in advance.

Gary
Gary T. DalSanto
Inventive Software Designs, LLC
[email protected]

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 21, 2011 3:25 pm

I originally loaded the file via the readfile command, then did the base64 encode function. However, they are expecting it in the raw binary format like the manual import utiility.
Did you try without converting to base64? The only reason I know to convert to base64 is to be able to save the file in a text file such as saving an image within a Macro Scheduler script as discussed here.

gtdalsanto
Pro Scripter
Posts: 51
Joined: Tue Oct 03, 2006 4:22 pm
Location: Akron, Ohio
Contact:

Post by gtdalsanto » Wed Dec 21, 2011 4:30 pm

I did try that. When I do a readfile, I get ÿØÿà, which I cannot send via the stored procedure. Also, when I use the drop-down menu item of "Import Binary File", it imports the file to the end of the script in the format of

"FFD8FFE000104A464946000101000......"

which is different from when I perform the readfile to a variable, and then run the base64 encode on it to get the following:

"/9j/4AAQSkZJRgABAQAAAQABAAD/2w....."

What I am looking for is a way to create the Binary string that gets created at the end of a file when using the manual operation of "Import Binary File" so I can create the same results programatically.

All I am doing is importing a small jpg file by using the readfile command to read the file contents to a variable. That is where it reads it to a variable in the format ÿØÿà. Then when I do the base64 encode on this variable, I get a text string: "/9j/4AAQSkZJRgABAQAAAQABAAD/2w....."

When I run the Import Binary file, it creates the string "FFD8FFE000104A464946000101000......"
which is different from the other. I am trying to figure out if there is a way in MS to do this via a scripting command?

Thanks.

Gary
Gary T. DalSanto
Inventive Software Designs, LLC
[email protected]

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 21, 2011 5:03 pm

If I'm understanding correctly you want to convert the binary file to hex. Try this script. It takes a while with large files so you may need to set the VBS_TIMEOUT value (don't bother with it unless you get a timeout). This script writes the result to a text file in your temp directory

Also included "Hex2Ascii" to revert back to binary from hex

Code: Select all

//Let>VBS_TIMEOUT=10000

VBSTART
dim ascii_var
dim hex_var
Function Hex2Ascii(strHex)
  Dim I
  For I = 1 To Len(strHex) Step 2
    Hex2Ascii = Hex2Ascii & Chr(Eval("&H" & Mid(strHex, I, 2)))
  Next
  ascii_var = Hex2Ascii
End Function

Function Ascii2Hex(strAscii)
  Dim I
  For I = 1 To Len(strAscii) Step 1
    'Ascii2Hex = Ascii2Hex & Hex(ASCb(Mid(strAscii, I, 1)))
    TestValue = Len (Hex(ASC(Mid(strAscii, I, 1))))
    If TestValue = 1 then
    Ascii2Hex = Ascii2Hex & 0 & Hex(ASC(Mid(strAscii, I, 1)))
    Else
    Ascii2Hex = Ascii2Hex & Hex(ASC(Mid(strAscii, I, 1)))
    End if
    'msgbox Hex(ASCw(Mid(strAscii, I, 1)))
  Next
  hex_var = Ascii2Hex
End Function
VBEND

Input>Filename,Select a file to convert
ReadFile>Filename,ascii_string

  //Write Hex from Ascii
  VBRun>Ascii2Hex,%ascii_string%
  VBEval>hex_var,hex_string

  Let>WLN_NOCRLF=1
  WriteLn>%temp_dir%hex_string.txt,wresult,%hex_string%


/*

//To revert back to binary from hex
ReadFile>%temp_dir%hex_string.txt,data

VBRun>Hex2Ascii,data
VBEval>ascii_var,ascii_string
Let>WLN_NOCRLF=1
WriteLn>%temp_dir%new.jpg,wres,ascii_string
*/

gtdalsanto
Pro Scripter
Posts: 51
Joined: Tue Oct 03, 2006 4:22 pm
Location: Akron, Ohio
Contact:

Post by gtdalsanto » Wed Dec 21, 2011 8:38 pm

That seems to get me to the same hex format that the Import Binary File gets me from the menu item. Thanks so much for the vbscript to do that.

You are correct in that it does take a bit of time for it to do the conversion, but it gets what I was looking for. Thinking about the MS command, it takes some time also. I wonder why there isnt a MS command to do this, since the editor has the capability.

Thanks again for your response and solution.

Gary
Gary T. DalSanto
Inventive Software Designs, LLC
[email protected]

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Dec 21, 2011 8:57 pm

Happy to help.
I wonder why there isnt a MS command to do this, since the editor has the capability.
I don't know the answer, but on the same topic from a different perspective, I've wondered why the binary import saves to a hex format. We already have a base64 function and base64 is more space efficient than hex so it would seem a better format.

gtdalsanto
Pro Scripter
Posts: 51
Joined: Tue Oct 03, 2006 4:22 pm
Location: Akron, Ohio
Contact:

Post by gtdalsanto » Wed Dec 21, 2011 10:51 pm

That is exactly what Marcus would probably say, that the readfile/base64 is a more effiecient process than the Import Binary file.
Gary T. DalSanto
Inventive Software Designs, LLC
[email protected]

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts