Skip to content

Instantly share code, notes, and snippets.

@onwsk8r
Last active September 24, 2019 14:52
Show Gist options
  • Save onwsk8r/69e22d057594834906972c57b4c1eb89 to your computer and use it in GitHub Desktop.
Save onwsk8r/69e22d057594834906972c57b4c1eb89 to your computer and use it in GitHub Desktop.
Convert 7bit ASCII to 8bit ASCII
import (
"encoding/binary"
"io"
)
// Convert changes 7-bit ASCII to 8-bit ASCII.
// Given 56 bits of input, there are 8x 7-bit characters contained
// in the bits, but Go cannot extract the ASCII text from the input
// because the smallest unit of data in Go is the byte. This function
// will convert each group of 7 bits into an 8-bit byte, which can be
// parsed as a string.
// Given the following binary:
// 00 11 01 10 11 01 00 01 11 01 01 10 11 10
// 7-bit ASCII looks like:
// 00 11 01 1
// 0 11 01 00
// 01 11 01 0
// 1 10 11 10
// ..After conversion the result is:
// (0) 00 11 01 1 = 00 01 10 11 (shift byte 1 right by 1)
// (0) 0 11 01 00 = 00 01 10 11 (shift byte 1 left by 6, byte 2 right by 2)
// (0) 01 11 01 0 = 00 11 10 10 (shift byte 1 left by 5, byte 2 right by 3)
// (0) 1 10 11 10 = 01 10 11 10 (shift byte 2 left by 4, byte 3 right by 4)
func Convert(data []byte) []byte {
// Store the 8-bit ASCII in res.
var res = make([]byte, len(data)*8/7)
fmt.Printf("First 100 bytes of Data: % #x\n", data[:100])
var idx, shift int
for i, offset := 0, 0; i < len(res)-1; i, offset = i+1, offset+7 {
idx, shift = offset / 8, offset % 8
lhs := data[idx] & (255 >> shift)
if shift == 0 {
lhs >>= 1
} else if shift > 1 {
lhs <<= shift - 1
}
rhs := (data[idx+1] & (255 << (9 - shift))) >> (9 - shift)
res[i] = (lhs | rhs) & 127
}
fmt.Printf("First 100 bytes encoded: % #x\nFirst 100 chars: %s", res[:100], res[:100])
return res
}
// Prints
// First 100 bytes of Local Use Data: 0x79 0x3b 0x7c 0x3d 0xfd 0x9f 0x3a 0x79 0x3b 0x7d 0x7f 0x0f 0x9d 0x3c 0x9d 0xbe 0x4e 0xee 0x99 0x77 0x3e 0x74 0xf2 0x76 0xfa 0xda 0x79 0xbe 0x74 0x02 0x9c 0x3d 0x18 0xdd 0x52 0x74 0xb5 0xd3 0xc9 0xdb 0xeb 0x69 0xe6 0xf9 0xd0 0x08 0x7a 0x31 0xba 0xa4 0xe9 0x6b 0xa7 0x93 0xb7 0xd6 0xd3 0xcd 0xf3 0xa0 0x13 0x35 0xec 0xf2 0xea 0x93 0xa5 0xae 0x9e 0x4e 0xdf 0x5b 0x4f 0x37 0xce 0x80 0x44 0xcb 0x99 0xd5 0x27 0x5b 0x5d 0x3c 0x9d 0xbe 0xb6 0x9e 0x6f 0x9d 0x00 0x89 0x97 0x33 0xaa 0x4e 0x96 0xba 0x79 0x3b
// First 100 bytes encoded: 0x3c 0x4e 0x6f 0x43 0x6f 0x76 0x3e 0x3a 0x3c 0x4e 0x6f 0x57 0x78 0x3e 0x3a 0x3c 0x4e 0x6f 0x49 0x6e 0x74 0x65 0x6e 0x3e 0x3a 0x3c 0x4e 0x6f 0x56 0x69 0x73 0x3e 0x3a 0x00 0x53 0x43 0x68 0x63 0x3a 0x52 0x3a 0x2d 0x3a 0x3c 0x4e 0x6f 0x56 0x69 0x73 0x3e 0x3a 0x00 0x43 0x68 0x63 0x3a 0x52 0x3a 0x2d 0x3a 0x3c 0x4e 0x6f 0x56 0x69 0x73 0x3e 0x3a 0x00 0x4c 0x6b 0x6c 0x79 0x3a 0x52 0x3a 0x2d 0x3a 0x3c 0x4e 0x6f 0x56 0x69 0x73 0x3e 0x3a 0x00 0x44 0x65 0x66 0x3a 0x52 0x3a 0x6d 0x3a 0x3c 0x4e 0x6f 0x56 0x69
// First 100 chars: <NoCov>:<NoWx>:<NoInten>:<NoVis>: SChc:R:-:<NoVis>: Chc:R:-:<NoVis>: Lkly:R:-:<NoVis>: Def:R:m:<NoVi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment