Created
January 17, 2014 07:55
-
-
Save oskaritimperi/8469849 to your computer and use it in GitHub Desktop.
Tcl version of `xxdi -i` command. Somewhat inspired by http://www.kroah.com/log/blog/2013/09/11/xxd-i/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if {$argc > 0} then { | |
| set infile [open [lindex $argv 0]] | |
| set doclose 1 | |
| set varname [string map {/ _ . _ - _} [lindex $argv 0]] | |
| } else { | |
| set infile stdin | |
| set doclose 0 | |
| set varname stdin | |
| } | |
| set indata [read $infile] | |
| if {$doclose > 0} then { | |
| close $infile | |
| } | |
| set lendata [string bytelength $indata] | |
| set num_digits_per_line 12 | |
| puts -nonewline "unsigned char $varname\[\] = \{" | |
| for {set i 0} {$i < $lendata} {incr i} { | |
| if {$i % $num_digits_per_line == 0} { | |
| puts -nonewline "\n " | |
| } | |
| scan [string byterange $indata $i $i] "%c" ord | |
| puts -nonewline [format "0x%.2x" $ord] | |
| if {$i < $lendata-1} { | |
| puts -nonewline ", " | |
| } | |
| } | |
| puts "\n\};" | |
| puts "unsigned int $varname\_len = $lendata;" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment