Base32编码与转码 Golang实现 (AI刷题)求助

109 阅读5分钟

代码

func solution(rawStr, encodedStr string) string {
	// Please write your code here
	res1 := encodeBase32(rawStr)
	// fmt.Println("res1:", res1)
	// fmt.Println("encodeStr = ", encodedStr)
	encodeStrRes := decodeBase32(encodedStr)

	return res1 + ":" + encodeStrRes
}

// 将输入的原始字符串编码为 Base32 字符串
func encodeBase32(rawStr string) string {
	var rawStrRes string
	for _, c := range rawStr {
		rawStrRes += fmt.Sprintf("%08b", c)
	}
	rawRes := strToSlice(rawStrRes)
	rawLen := len(rawRes)
	for rawLen%5 != 0 {
		rawRes = append(rawRes, '0')
		rawLen = len(rawRes)
	}
	// 计算分组数量
	groupCount := rawLen / 5
	// 存储分组结果
	var groups [][]byte
	// 进行分组
	for i := 0; i < groupCount; i++ {
		start := i * 5
		end := (i + 1) * 5
		if end > rawLen {
			end = rawLen
		}
		groups = append(groups, rawRes[start:end])
	}
	// 进行编码处理
	var encodedRes string
	for _, group := range groups {
		decimalValue := binaryToDecimal(group)
		encodedChar := getEncodedChar(decimalValue)
		encodedRes += encodedChar
	}
	// 根据原始二进制数据的长度确定末尾需要补 '+' 的数量
	remainder := len(rawStrRes) % 40
	switch remainder {
	case 8:
		encodedRes += "++++++"
	case 16:
		encodedRes += "++++"
	case 24:
		encodedRes += "+++"
	case 32:
		encodedRes += "+"
	}
	return encodedRes
}

// 将输入的 Base32 编码字符串解码为原始字符串
func decodeBase32(encodedStr string) string {
	// 将编码字符串转换为二进制字符串
	var binaryStr string
	binary := []byte(encodedStr))
	
        count := 0
        // 去除末尾多余的+号
	for i := len(binary) - 1; i > 0; i-- {
		if binary[i] == 43 {
			count++
		} else {
			break
		}
	}

	binary = binary[:len(binary)-count]
        
	s := string(binary)

	for _, c := range s {
		binaryStr += fmt.Sprintf("%05b", getIndex(c))
	}
	// 去除末尾多余的 0
	for binaryStr[len(binaryStr)-1] == '0' && (len(binaryStr)%8 != 0) {
		binaryStr = binaryStr[:len(binaryStr)-1]
	}
	// 8 位一组转换为字节数组
	var byteArr []byte
	for i := 0; i < len(binaryStr); i += 8 {
		binaryGroup := binaryStr[i : i+8]
		decimalValue := binaryToDecimal([]byte(binaryGroup))
		byteArr = append(byteArr, byte(decimalValue))
	}
	return string(byteArr)
}

func getIndex(c rune) int {
	charMap := []string{"9", "8", "7", "6", "5", "4", "3", "2", "1", "0", "m", "n", "b", "v", "c", "x", "z", "a", "s", "d", "f", "g", "h", "j", "k", "l", "p", "o", "i", "u", "y", "t"}
	for i, char := range charMap {
		if rune(char[0]) == c {
			return i
		}
	}
	return -1
}

func strToSlice(s string) []byte {
	return []byte(s)
}

func binaryToDecimal(binary []byte) int {
	// 将 5 位二进制转换为十进制
	value := 0
	for _, bit := range binary {
		value = value << 1
		if bit == '1' {
			value += 1
		}
	}
	return value
}

func getEncodedChar(index int) string {
	// 根据索引获取编码字符
	charMap := []string{"9", "8", "7", "6", "5", "4", "3", "2", "1", "0", "m", "n", "b", "v", "c", "x", "z", "a", "s", "d", "f", "g", "h", "j", "k", "l", "p", "o", "i", "u", "y", "t"}
	return charMap[index]
}

示例

fmt.Println(solution("foo", "b0zj5+++") == "bljhy+++:bar")
fmt.Println(solution("The encoding process represents 40-bit groups of input bits as output strings of 8 encoded characters.  Proceeding from left to right, a 40-bit input group is formed by concatenating 5 8bit input groups. These 40 bits are then treated as 8 concatenated 5-bit groups, each of which is translated into a single character in the base 32 alphabet.  When a bit stream is encoded via the base 32 encoding, the bit stream must be presumed to be ordered with the most-significant- bit first. That is, the first bit in the stream will be the high- order bit in the first 8bit byte, the eighth bit will be the low- order bit in the first 8bit byte, and so on.", "bljhy+++b0zj5+++") == "maf3m164vlahyl60vlds9i6svuahmiod58l3mi6sbglhmodfcbz61b8vb0fj1162c0jjmi6d58jhb160vlk2mu89b0fj1il9b4ls9oogcak2mu89cvp25pncbuls9oo359i79lncbvjh1ln558ahzknsb4aj1lnscbj7917zc0jh3ln4bafhill9bll3yo09vashbu89cajs9id0buf21n89b5z61b8vb0fj1160vlk2mu89bul3yunz58fj3163vul3pln558a2s166vuj33knfbgj37u60vlds9v0928a3su89v4j29unf58dj5oogc8lsi17fv8sj3l093zk79kd0cals9knsbfz21p64vkz21id4b4p3ml89b4ls9c89bvjhiko8cashiknfbgs79v0vb0fj1162c0jjmi6d4zz3mkn6v9z3yla9cuf3sko158fj316fc0zhiiobb4p3ml89v4j21ol9b5z23pncbuh3m166v8zj5kn6casj5160vkz21p6458a37io459ld5168vak3zkn7bgp7i189muf3moa9b5z35pnf58lj1id4b4hs9pnd58shikoxbash116hv4zs9u61bfz35kndbfz63ba9bgj33oo5v4j3cn89caf3m167v4p79iofc0sh7o09vgpj3u89b0ss9i6sbgljmon4bzz21ol9b0ss9oosbasj5ln558ohsu6158p3zl09vgjj3u8vcvfhcod0blfh3kncczhs9kd0czz3bpnscvp7i17fv8zj1160cbh79u61bfz3bpnscvp79kd0czz3soa9caf3m16dcal3mknv58ohso6b58a3m16fv8ss9p60buf7p16xc0s3mia9b0fj1160vkz21p6458d3siddczz6zkd0czz35ynfbfh79u61bfz3mpn2v8p3z167v4p79uo0vah79kd458p3zl09vajjcn09vul31lns58a3su89v4j79u61bfz3bpnscvp79c67v4p79kdlcassk168vls79iox58jhinz+:foobar")

在试题给的例子中第二次使用solution的第二个参数是bljhy+++b0zj5+++,也就是要解码bljhy+++b0zj5+++,根据后面的判断可以得出想要的解码结果是foobar。 但是foobar编码之后我计算出是bljhykd8c1++++++。所以是不是题有问题。