介绍
如何在去中利用二进制对BMP文件头进行读取?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
BMP文件头定义:
词两个字节16位字
四个字节32位
package main import ( ,“编码/binary" ,“fmt" ,“os" ) , func main (), { ,文件,err :=, os.Open (“tim.bmp") ,if err  !=, nil { fmt.Println才能(err) ,返回 ,} , ,defer file.Close (), ,//类型拆成两个字节来读 ,var 只头颅,headB 字节 ,//读第二个参数字节序一般windows/linux大部分都是LittleEndian,苹果系统用BigEndian ,binary.Read(文件,binary.LittleEndian,,,头疼) ,binary.Read(文件,binary.LittleEndian,,, headB) , ,//文件大小 var size uint32 ,binary.Read (binary.LittleEndian,文件,和大小) , ,//预留字节 ,var reservedA reservedB uint16 ,binary.Read(文件,binary.LittleEndian,,, reservedA) ,binary.Read(文件,binary.LittleEndian,,, reservedB) , ,//偏移字节 var offbits uint32 ,binary.Read(文件,binary.LittleEndian,,, offbits), ,fmt.Println (headB,头疼,,,,reservedA,, reservedB,, offbits), }
执行结果
66 77 196662 0 0 54
引用>使用结构体方式
package main import ( ,“编码/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 (), { ,文件,err :=, os.Open (“tim.bmp") ,if err  !=, nil { fmt.Println才能(err) ,返回 ,} , ,defer file.Close (), ,//类型拆成两个字节来读 ,var 只头颅,headB 字节 ,//读第二个参数字节序一般windows/linux大部分都是LittleEndian,苹果系统用BigEndian ,binary.Read(文件,binary.LittleEndian,,,头疼) ,binary.Read(文件,binary.LittleEndian,,, headB) , ,//文件大小 var size uint32 ,binary.Read (binary.LittleEndian,文件,和大小) , ,//预留字节 ,var reservedA reservedB uint16 ,binary.Read(文件,binary.LittleEndian,,, reservedA) ,binary.Read(文件,binary.LittleEndian,,, reservedB) , ,//偏移字节 var offbits uint32 ,binary.Read(文件,binary.LittleEndian,,, offbits) , ,fmt.Println (headB,头疼,,,,reservedA,, reservedB,, offbits) , ,infoHeader :=,新(BitmapInfoHeader) ,binary.Read(文件,binary.LittleEndian,, infoHeader) ,fmt.Println (infoHeader), }执行结果:
66 77 196662 0 0 54
, {40 256 256 24 196608 3100 3100 0 0}
引用><强>补充:golang(语言)字节/[]字节与二进制形式字符串互转强>
<>强效果强>
把某个字节或字节数组转换成字符串01的形式,一个字节用8个“0”或“1”字符表示。
比如:
字节(3),→,“00000011” []字节{1,2,3},→,”(00000001,00000001,00000001)” “00000011,10000000”,→,[]字节{0 x3, 0 x80}<强>开源库国际强>
实际上我已经将其封装到一个开源库了(国际),其中的一个功能就能达到上述效果:
//字节/[]byte →字符串 bs :=,[]字节{1,,2,,3} s :=, biu.BytesToBinaryString (bs) fmt.Println (s),//(00000001, 00000001, 00000001) fmt.Println (biu.ByteToBinaryString(字节(3))),//00000011//string →[]字节 s :=,“(00000011, 00000011), bs :=, biu.BinaryStringToBytes (s) fmt.Printf (“% # v \ n",, b),//[]字节{0 x3, 0 x80}代码实现
const (=,,zero 字节(& # 39;0 & # 39;)=,,one 字节(& # 39;1 & # 39;)=,,lsb 字节(& # 39;[& # 39;),//,left square 括号=,,rsb 字节(& # 39;]& # 39;),//right square 括号=,,space 字节(& # 39;,& # 39;) ) var uint8arr [8] uint8//,ErrBadStringFormat represents a  error of input 字符串# 39;s format is illegal 。 var ErrBadStringFormat =, errors.New (“bad string format")//,ErrEmptyString represents a  error of empty input 字符串。 null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在去中利用二进制对BMP文件头进行读取