github.com/visualfc/goembed@v0.3.3/bytes.go (about)

     1  package goembed
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  )
     9  
    10  func BytesToList(data []byte) string {
    11  	var ar []string
    12  	for _, v := range data {
    13  		ar = append(ar, fmt.Sprintf("%d", v))
    14  	}
    15  	return strings.Join(ar, ",")
    16  }
    17  
    18  func BytesToHex(data []byte) string {
    19  	var buf bytes.Buffer
    20  	WriteToHex(data, &buf)
    21  	return buf.String()
    22  }
    23  
    24  const hex = "0123456789abcdef"
    25  
    26  func WriteToHex(data []byte, w io.Writer) (n int, err error) {
    27  	buf := []byte{'\\', 'x', 0, 0}
    28  	for _, d := range data {
    29  		buf[2], buf[3] = hex[d/16], hex[d%16]
    30  		_, err = w.Write(buf)
    31  		if err != nil {
    32  			return n, err
    33  		}
    34  		n++
    35  	}
    36  	return n, nil
    37  }