Skip to content

Commit

Permalink
デコードの際にNULL文字が入ってしまう問題の解決。
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Sep 11, 2022
1 parent a459da3 commit e4e3373
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Decode(buf []byte) string {

var res bytes.Buffer
// 最終行以外をエンコード。
for i := 0; i < max; i++ {
for i := 0; i < max-1; i++ {
b := sb[4*i : 4*i+4]

data := alphabetMap[string(b[3])] + alphabetMap[string(b[2])]*64 + alphabetMap[string(b[1])]*64*64 + alphabetMap[string(b[0])]*64*64*64
Expand All @@ -164,5 +164,18 @@ func Decode(buf []byte) string {
res.Write([]byte{byte(b1), byte(b2), byte(b3)})
}

b := sb[4*(max-1) : 4*max]
data := alphabetMap[string(b[3])] + alphabetMap[string(b[2])]*64 + alphabetMap[string(b[1])]*64*64 + alphabetMap[string(b[0])]*64*64*64
b1 := data / (256 * 256)
b2 := (data % (256 * 256)) / 256
b3 := data % 256
if b2 == 0 {
res.Write([]byte{byte(b1)})
} else if b3 == 0 {
res.Write([]byte{byte(b1), byte(b2)})
} else {
res.Write([]byte{byte(b1), byte(b2), byte(b3)})
}

return res.String()
}

0 comments on commit e4e3373

Please sign in to comment.