# SqlQuantumLeap/BinaryFormatter

Format binary file into hex string file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
BinaryFormatter
.gitattributes
.gitignore
BinaryFormatter.sln

# BinaryFormatter

Simple command-line utility that reads in a binary file and converts it to a string representation of those bytes (e.g. single byte of "3C" converts to two characters "3" followed by "C").

Output options are any combination of:

1. Text File
2. Clipboard (to then paste via Control + V)
3. Console (mostly for debugging / testing / research)

A feature / benefit of BinaryFormatter is that it splits the hex string across N lines of "ChunkSize" bytes each, with all lines prior to the final line being appended with the T-SQL line-continuation character: \. This makes it easier to work with long hex strings (anything over 100k characters).

This was originally intended for automating builds of SQLCLR projects (i.e. convert the DLL into a hex bytes string for the CREATE ASSEMBLY [name] FROM 0x\ statement) and for creating T-SQL scripts that create Certificates (i.e. convert the .cer file, and optionally the .pvk file, into a hex bytes strings for the CREATE CERTIFICATE [name] FROM BINARY = 0x\ statement). However, it can be used for many other purposes. And, if needed, it would be easy enough to add an input parameter to allow for overriding the line-continuation character (e.g. ^ for DOS).

### Example

Source binary file = 0123456789ABCDEF

Output, using a ChunkSize of 3, contains:

012345\
6789AB\
CDEF


## Command Prompt / Automation Usage

BinaryFormatter path\to\binary_file_name.ext [ path\to\OutputFile.sql ] [ ChunkSize ]
[ /Clipboard ] [ /Console ] [ /NoFile ]

• ChunkSize = the number of bytes per row. A byte is 2 characters: 00 - FF.
• /Clipboard = Copy output to clipboard (to then paste with Control-V)
• /Console = Send output to console
• /NoFile = Do not save to file, even if OutputFile is specified

Notes:

• Default ChunkSize = 10000
• Default OutputFile = {path\to\binary_file_name}.sql
• Maximum line length = (ChunkSize * 2) + 1.
• If ChunkSize is not supplied, the user is prompted to enter a value. A default value is shown inside square-brackets (e.g. [ 10000 ]), and hitting Enter without entering anything else will accept that default. Or, you can enter an integer value that is above zero. Any other value will cause the request to be repeated.

## Point-and-Click / "Send to" Usage

BinaryFormatter.exe can be used as a "Send to" destination so that you can easily convert binary files in Windows File Explorer simply by right-clicking on them, going to "Send to", and selecting BinaryFormatter.

To set this up, do the following:

1. Open File Explorer
2. Navigate to the C:\Users\{your_windows_logon}\AppData\Roaming\Microsoft\Windows\SendTo folder
3. Right-click in that folder, go to New >, and select Shortcut (2nd from the top)
4. Click the Browse... button
5. Select BinaryFormatter.exe and click the OK button
6. Click the Next button
7. Change the name to: Binary Formatter
8. Click the Finish button

At this point you have a shortcut that will always prompt you to enter in a ChunkSize value, or at least just hit Enter to accept the default value, and will save to a file (no Console or Clipboard) using the default filename. If you want to convert files without being prompted to specify the Chunk Size, and/or if you some other combination of output options, do the following:

9. Right-click on the Binary Formatter shortcut and select Properties
10. Go to the Shortcut tab
11. In the Target: text field, add any combination of the following options to the right of ...BinaryFormatter.exe:
• An <integer> >= 1 (for ChunkSize). I find that a value between 39 and 70 works best for scripts that will be posted online, and up to 10,000 (the default) is fine for Assemblies in release scripts.
• /Clipboard
• /Console
• /NoFile
12. For the Run: drop-down, select Minimized
13. Click the OK button

And, you can even have multiple shortcuts, each one with a different combination of options 😄 .

### Example Target:

I use the following for testing. I use a ChunkSize of 39 since 39 bytes * 2 = 78 characters, plus 1 for the backslash = 79 characters total on all but the final line (unless there is only a single line, then no backslash), which is under the 80 character limit of some environments.

...BinaryFormatter.exe 39 /Clipboard /NoFile