字符二进制转换

字符二进制转换

运用位操作左移和右移来实现字符二进制转换的一个源码(自己也可以去实现)

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package Btos

import (
"errors"
"regexp"
)

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
}
-------------本文结束感谢您的阅读-------------

本文标题:字符二进制转换

文章作者:Wuman

发布时间:2018年09月05日 - 13:09

最后更新:2018年11月03日 - 11:11

原始链接:http://yoursite.com/2018/09/05/字符二进制转换/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。