Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write binary data

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB
I need this script to output whatever my hex string is to binary file.

In this example it is a simple zip folder so the output file is zip.zip however mystring can be extremely long and will always be different.

This is my code and it doesn’t work it errors on Clng I don’t know why because I don’t really understand how it should work.

If anyone could correct my code so that it does what I need I would be very grateful.

Option Explicit
Dim mystring
mystring = "50 4B 05 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

Binary mystring, 512

Dim strSource
Dim lp, lSize
Sub Binary(strSource, lSize)
strSource = Replace(strSource, " ", "")

With CreateObject("Scripting.FileSystemObject").CreateTextFile(".\zip.zip", True)
For lp = 1 To Len(strSource) Step lSize
.WriteLine Chr(Clng("&h" & Mid(strSource, lp, lSize),lp,2))
Next
End With
End Sub
 
I don't follow. It sounds like you're asking "convert mystring into a binary representation string.", but that is already being accomplished in your code by simply add [tt]&H[/tt] to the beginning of all hex numbers. What is your end goal?

-Geates

 
I already have &h in the code which is why I do need a working code example posted as my vbscript skills are very poor.

The end goal is to be able to put any length hex string to the variable mystring add the file and extension to CreateTextFile(".\zip.zip", True) in the example I posted it is zip.zip run the code and have the binary file created
 
so you are looking for something like this?

mystring = "CatBurgler"
filename = "CatBurgler.zip"

mystring = "50 4B 05 06"
filename = "504B0506.zip"

I'm still confused. Can you post another example that pertains to you end goal, "zip.zip" is ambiguous.

-Geates
 
If you run the following script it creates a compressed zip folder

Option Explicit
Dim output
output = ".\New Compressed (zipped) Folder.zip"

Dim ts, x
If Instr(output, "\") = 0 Then output = Left(Wscript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\")) & output
Set ts = CreateObject("Scripting.FileSystemObject").OpenTextFile(output, 2, True)
On Error Resume Next
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&h" & Mid("504B0506000000000000000000000000000000000000",x,2))) : Next
ts.Close

If you look again at my first posting I need the script to accept any length hex string which will always be 2 characters followed by space but can be extremely long the zip folder string is very short as follows

mystring = "50 4B 05 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

All the spaces need to be removed and then the whole string converted to binary

The example I posted is the string for a compressed zip folder which is why the file name and extension is zip.zip. the file name and extension will always be different depending on the string so it can be a variable like the mystring variable if it makes it easier.


 
Sorry, I'm still not following. If you want to know how to "eat an apple", you ask, "How do I eat an apple?" You don't ask "How do I measure my brothers right arm, mow the lawn, and write congress a letter". In other words, it is unclear what you are trying to do.

If mystring is the input, what is the output?

-Geates


 
>If you run the following script it creates a compressed zip folder

Actually to be pedantic it doesn't. It creates a zip file (by creating the zip header), which the Windows shell then treats as a folder (as long as no other program has become associated with the .zip extension).

And you've unfortunately slightly mangled the solution I gave in your previous thread, which would in fact have been just right for this - here have my version:

Code:
[blue]Dim mystring
mystring = "4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 00 00 00 00 00 40"

Breakup mystring, 2

Sub Breakup(strSource, lSize)
    strSource = Replace(strSource, " ", "")
    
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(".\output.txt", True)
        For lp = 1 To Len(strSource) Step lSize
            .Write Chr(CByte("&H" & Mid(strSource, lp, lSize)))
        Next
    End With
End Sub[/blue]

I'm not going to do all the work here - you might like to try to figure out how to pass (and then use) a filename of your own choice to the procedure
 
Would this be the correct full code including how you would pass and use the filename

Dim mystring, filename
mystring = = "4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 00 00 00 00 00 40"
filename = ".\output.txt"

Breakup mystring, 2, filename

Sub Breakup(strSource, lSize, filename)
strSource = Replace(strSource, " ", "")

With CreateObject("Scripting.FileSystemObject").CreateTextFile(filename, True)
For lp = 1 To Len(strSource) Step lSize
.Write Chr(CByte("&H" & Mid(strSource, lp, lSize)))
Next
End With
End Sub

Thank you for helping me to learn
 
Yep, looks about right

Note that I used Cbyte on the assumption that we'd be working with string representations of byte values (which is only really the case when lSize = 1 or 2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top