data2c.go 609 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. )
  8. func main() {
  9. flag.Parse()
  10. a := flag.Args()
  11. if len(a) != 2 {
  12. fmt.Fprintf(os.Stderr, "[%v]usage: data2s name input-file (writes to stdout)\n", a)
  13. os.Exit(1)
  14. }
  15. n := a[0]
  16. i := a[1]
  17. in, err := ioutil.ReadFile(i)
  18. if err != nil {
  19. fmt.Fprintf(os.Stderr, "%v\n", err)
  20. os.Exit(1)
  21. }
  22. total := len(in)
  23. fmt.Printf("unsigned char %vcode[] = {\n", n)
  24. for len(in) > 0 {
  25. for j := 0; j < 16 && len(in) > 0; j++ {
  26. fmt.Printf("0x%02x, ", in[0])
  27. in = in[1:]
  28. }
  29. fmt.Printf("\n")
  30. }
  31. fmt.Printf("0,\n};\nint %vlen = %v;\n", n, total)
  32. }