bmp文件头定义:

word 两个字节 16bit

dword 四个字节 32bit

package main 
import (
 "encoding/binary"
 "fmt"
 "os"
)
 
func main() {
 file, err := os.open("tim.bmp")
 if err != nil {
  fmt.println(err)
  return
 }
 
 defer file.close() 
 //type拆成两个byte来读
 var heada, headb byte
 //read第二个参数字节序一般windows/linux大部分都是littleendian,苹果系统用bigendian
 binary.read(file, binary.littleendian, &heada)
 binary.read(file, binary.littleendian, &headb)
 
 //文件大小
 var size uint32
 binary.read(file, binary.littleendian, &size)
 
 //预留字节
 var reserveda, reservedb uint16
 binary.read(file, binary.littleendian, &reserveda)
 binary.read(file, binary.littleendian, &reservedb)
 
 //偏移字节
 var offbits uint32
 binary.read(file, binary.littleendian, &offbits) 
 fmt.println(heada, headb, size, reserveda, reservedb, offbits) 
}

执行结果

66 77 196662 0 0 54

使用结构体方式

package main 
import (
 "encoding/binary"
 "fmt"
 "os"
)
 
type bitmapinfoheader struct {
 size   uint32
 width   int32
 height   int32
 places   uint16
 bitcount  uint16
 compression uint32
 sizeimage  uint32
 xperlspermeter int32
 yperlspermeter int32
 clsrused  uint32
 clrimportant uint32
}
 
func main() {
 file, err := os.open("tim.bmp")
 if err != nil {
  fmt.println(err)
  return
 }
 
 defer file.close() 
 //type拆成两个byte来读
 var heada, headb byte
 //read第二个参数字节序一般windows/linux大部分都是littleendian,苹果系统用bigendian
 binary.read(file, binary.littleendian, &heada)
 binary.read(file, binary.littleendian, &headb)
 
 //文件大小
 var size uint32
 binary.read(file, binary.littleendian, &size)
 
 //预留字节
 var reserveda, reservedb uint16
 binary.read(file, binary.littleendian, &reserveda)
 binary.read(file, binary.littleendian, &reservedb)
 
 //偏移字节
 var offbits uint32
 binary.read(file, binary.littleendian, &offbits)
 
 fmt.println(heada, headb, size, reserveda, reservedb, offbits)
 
 infoheader := new(bitmapinfoheader)
 binary.read(file, binary.littleendian, infoheader)
 fmt.println(infoheader) 
}

执行结果:

66 77 196662 0 0 54

&{40 256 256 1 24 0 196608 3100 3100 0 0}

补充:golang(go语言) byte/[]byte 与 二进制形式字符串 互转

效果

把某个字节或字节数组转换成字符串01的形式,一个字节用8个”0”或”1”字符表示。

比如:

byte(3) –> “00000011”
[]byte{1,2,3} –> “[00000001 00000010 00000011]”
“[00000011 10000000]” –> []byte{0x3, 0x80}

开源库

实际上我已经将其封装到一个开源库了(),其中的一个功能就能达到上述效果:

//byte/[]byte -> string
bs := []byte{1, 2, 3}
s := biu.bytestobinarystring(bs)
fmt.println(s) //[00000001 00000010 00000011]
fmt.println(biu.bytetobinarystring(byte(3))) //00000011
//string -> []byte
s := "[00000011 10000000]"
bs := biu.binarystringtobytes(s)
fmt.printf("%#v\n", bs) //[]byte{0x3, 0x80}

代码实现

const (
 zero = byte('0')
 one = byte('1')
 lsb = byte('[') // left square brackets
 rsb = byte(']') // right square brackets
 space = byte(' ')
)
var uint8arr [8]uint8
// errbadstringformat represents a error of input string's format is illegal .
var errbadstringformat = errors.new("bad string format")
// erremptystring represents a error of empty input string.
var erremptystring = errors.new("empty string")
func init() {
 uint8arr[0] = 128
 uint8arr[1] = 64
 uint8arr[2] = 32
 uint8arr[3] = 16
 uint8arr[4] = 8
 uint8arr[5] = 4
 uint8arr[6] = 2
 uint8arr[7] = 1
}
// append bytes of string in binary format.
func appendbinarystring(bs []byte, b byte) []byte {
 var a byte
 for i := 0; i < 8; i++ {
  a = b
  b <<= 1
  b >>= 1
  switch a {
  case b:
   bs = append(bs, zero)
  default:
   bs = append(bs, one)
  }
  b <<= 1
 }
 return bs
}
// bytetobinarystring get the string in binary format of a byte or uint8.
func bytetobinarystring(b byte) string {
 buf := make([]byte, 0, 8)
 buf = appendbinarystring(buf, b)
 return string(buf)
}
// bytestobinarystring get the string in binary format of a []byte or []int8.
func bytestobinarystring(bs []byte) string {
 l := len(bs)
 bl := l*8 + l + 1
 buf := make([]byte, 0, bl)
 buf = append(buf, lsb)
 for _, b := range bs {
  buf = appendbinarystring(buf, b)
  buf = append(buf, space)
 }
 buf[bl-1] = rsb
 return string(buf)
}
// regex for delete useless string which is going to be in binary format.
var rbdel = regexp.mustcompile(`[^01]`)
// binarystringtobytes get the binary bytes according to the
// input string which is in binary format.
func binarystringtobytes(s string) (bs []byte) {
 if len(s) == 0 {
  panic(erremptystring)
 }
 s = rbdel.replaceallstring(s, "")
 l := len(s)
 if l == 0 {
  panic(errbadstringformat)
 }
 mo := l % 8
 l /= 8
 if mo != 0 {
  l++
 }
 bs = make([]byte, 0, l)
 mo = 8 - mo
 var n uint8
 for i, b := range []byte(s) {
  m := (i + mo) % 8
  switch b {
  case one:
   n += uint8arr[m]
  }
  if m == 7 {
   bs = append(bs, n)
   n = 0
  }
 }
 return
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。