Last active
February 6, 2021 19:02
-
-
Save neetsdkasu/044abe2d0db425a9170ee3e89c42a052 to your computer and use it in GitHub Desktop.
ファイルをバイナリファイルとして開いてbyte値をカンマ区切りで表示するだけ
This file contains 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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
func usage() { | |
fmt.Println(os.Args[0], "[-s] <filename>") | |
fmt.Println() | |
fmt.Println(" output byte values (8bit unsined integer)") | |
fmt.Println() | |
fmt.Println(" -s output signed integer type") | |
} | |
func main() { | |
var fileName string | |
var signed bool | |
switch len(os.Args) { | |
default: | |
usage() | |
return | |
case 2: | |
fileName = os.Args[1] | |
signed = false | |
case 3: | |
if os.Args[1] != "-i" { | |
usage() | |
return | |
} | |
fileName = os.Args[2] | |
signed = true | |
} | |
buf, err := ioutil.ReadFile(fileName) | |
if err != nil { | |
panic(err) | |
return | |
} | |
for i, b := range buf { | |
if i > 0 && i&31 == 0 { | |
fmt.Println(",") | |
} else if i != 0 { | |
fmt.Print(",") | |
} | |
if signed { | |
fmt.Print(int8(b)) | |
} else { | |
fmt.Print(b) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment