脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - go 读取BMP文件头二进制读取方式

go 读取BMP文件头二进制读取方式

2021-03-17 00:53清明-心若淡定 Golang

这篇文章主要介绍了go 读取BMP文件头二进制读取方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

BMP文件头定义:

WORD 两个字节 16bit

DWORD 四个字节 32bit

go 读取BMP文件头二进制读取方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

使用结构体方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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”字符表示。

比如:

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

开源库 biu

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

?
1
2
3
4
5
6
7
8
9
//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}

代码实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
}

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

原文链接:https://www.cnblogs.com/saryli/p/11064837.html

延伸 · 阅读

精彩推荐
  • GolangGo语言基础单元测试与性能测试示例详解

    Go语言基础单元测试与性能测试示例详解

    这篇文章主要为大家介绍了Go语言基础单元测试与性能测试示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助祝大家多多进步...

    枫少文7812021-12-05
  • GolangGo语言实现自动填写古诗词实例代码

    Go语言实现自动填写古诗词实例代码

    这篇文章主要给大家介绍了关于Go语言实现自动填写古诗词的相关资料,这是最近在项目中遇到的一个需求,文中通过示例代码介绍的非常详细,需要的朋...

    FengY5862020-05-14
  • GolangGolang 语言极简类型转换库cast的使用详解

    Golang 语言极简类型转换库cast的使用详解

    本文我们通过 cast.ToString() 函数的使用,简单介绍了cast 的使用方法,除此之外,它还支持很多其他类型,在这没有多多介绍,对Golang 类型转换库 cast相关知...

    Golang语言开发栈6112021-12-02
  • Golanggo语言获取系统盘符的方法

    go语言获取系统盘符的方法

    这篇文章主要介绍了go语言获取系统盘符的方法,涉及Go语言调用winapi获取系统硬件信息的技巧,具有一定参考借鉴价值,需要的朋友可以参考下 ...

    无尽海3862020-04-24
  • Golang深入浅析Go中三个点(...)用法

    深入浅析Go中三个点(...)用法

    这篇文章主要介绍了深入浅析Go中三个点(...)用法,需要的朋友可以参考下...

    踏雪无痕SS6472021-11-17
  • GolangGO语言字符串处理Strings包的函数使用示例讲解

    GO语言字符串处理Strings包的函数使用示例讲解

    这篇文章主要为大家介绍了GO语言字符串处理Strings包的函数使用示例讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加...

    Jeff的技术栈6882022-04-14
  • GolangGolang实现四种负载均衡的算法(随机,轮询等)

    Golang实现四种负载均衡的算法(随机,轮询等)

    本文介绍了示例介绍了Golang 负载均衡的四种实现,主要包括了随机,轮询,加权轮询负载,一致性hash,感兴趣的小伙伴们可以参考一下...

    Gundy_8442021-08-09
  • GolangGo语言range关键字循环时的坑

    Go语言range关键字循环时的坑

    今天小编就为大家分享一篇关于Go语言range关键字循环时的坑,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来...

    benben_20154202020-05-23