可信考试第三题-20220722
作者:互联网
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
* Description: 上机编程认证
* Note: 缺省代码仅供参考,可自行决定使用、修改或删除
* 只能import Go标准库
*/
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
)
func encodeOne(unicodeVal int) string {
unicodeValByte := strconv.FormatInt(int64(unicodeVal),2)
totalLens := len(unicodeValByte)
needZero := 8 - totalLens - 1
enStr := "1"
for i:= 0; i < needZero; i++ {
enStr += "0"
}
enStr += unicodeValByte
decNum,err := strconv.ParseInt(enStr,2,64)
if err != nil{
return ""
}
res := strconv.FormatInt(decNum,16)
return res
}
func encodeTwo(unicodeVal int) string{
unicodeValByte := strconv.FormatInt(int64(unicodeVal),2)
totalLens := len(unicodeValByte)
firstByte := unicodeValByte[totalLens-6:totalLens]
firstByte = "01" + firstByte
secondLens := totalLens - 6
needZero := 8 - secondLens - 3
secondByte := "001"
for i:= 0; i < needZero; i++ {
secondByte += "0"
}
secondByte += unicodeValByte[:secondLens]
encodeStr := secondByte + firstByte
decNum,err := strconv.ParseInt(encodeStr,2,64)
if err != nil{
return ""
}
res := strconv.FormatInt(decNum,16)
return res
}
func encodeThree(unicodeVal int) string{
unicodeValByte := strconv.FormatInt(int64(unicodeVal),2)
totalLens := len(unicodeValByte)
firstByte := unicodeValByte[totalLens-6:totalLens]
firstByte = "01" + firstByte
secondByte := unicodeValByte[totalLens-12:totalLens-6]
secondByte = "01" + secondByte
threeLens := totalLens - 12
needZero := 8 - threeLens - 4
threeByte := "0001"
for i:= 0; i < needZero; i++ {
threeByte += "0"
}
threeByte += unicodeValByte[:threeLens]
encodeStr := threeByte + secondByte + firstByte
decNum,err := strconv.ParseInt(encodeStr,2,64)
if err != nil{
return ""
}
res := strconv.FormatInt(decNum,16)
return res
}
func encodeFour(unicodeVal int) string{
unicodeValByte := strconv.FormatInt(int64(unicodeVal),2)
totalLens := len(unicodeValByte)
firstByte := unicodeValByte[totalLens-6:totalLens]
firstByte = "01" + firstByte
secondByte := unicodeValByte[totalLens-12:totalLens-6]
secondByte = "01" + secondByte
threeByte := unicodeValByte[totalLens-18:totalLens-12]
threeByte = "01" + threeByte
fourLens := totalLens - 18
needZero := 8 - fourLens - 5
fourByte := "00001"
for i:= 0; i < needZero; i++ {
fourByte += "0"
}
fourByte += unicodeValByte[:fourLens]
encodeStr := fourByte + threeByte + secondByte + firstByte
decNum,err := strconv.ParseInt(encodeStr,2,64)
if err != nil{
return ""
}
res := strconv.FormatInt(decNum,16)
return res
}
func encodeFive(unicodeVal int) string{
unicodeValByte := strconv.FormatInt(int64(unicodeVal),2)
totalLens := len(unicodeValByte)
firstByte := unicodeValByte[totalLens-6:totalLens]
firstByte = "01" + firstByte
secondByte := unicodeValByte[totalLens-12:totalLens-6]
secondByte = "01" + secondByte
threeByte := unicodeValByte[totalLens-18:totalLens-12]
threeByte = "01" + threeByte
fourByte := unicodeValByte[totalLens-24:totalLens-18]
fourByte = "01" + fourByte
fiveLens := totalLens - 24
needZero := 8 - fiveLens - 6
fiveByte := "000001"
for i:= 0; i < needZero; i++ {
fiveByte += "0"
}
fiveByte += unicodeValByte[:fiveLens]
encodeStr := fiveByte + fourByte + threeByte + secondByte + firstByte
decNum,err := strconv.ParseInt(encodeStr,2,64)
if err != nil{
return ""
}
res := strconv.FormatInt(decNum,16)
return res
}
func encodeSix(unicodeVal int) string{
unicodeValByte := strconv.FormatInt(int64(unicodeVal),2)
totalLens := len(unicodeValByte)
firstByte := unicodeValByte[totalLens-6:totalLens]
firstByte = "01" + firstByte
secondByte := unicodeValByte[totalLens-12:totalLens-6]
secondByte = "01" + secondByte
threeByte := unicodeValByte[totalLens-18:totalLens-12]
threeByte = "01" + threeByte
fourByte := unicodeValByte[totalLens-24:totalLens-18]
fourByte = "01" + fourByte
fiveByte := unicodeValByte[totalLens-30:totalLens-24]
fiveByte = "01" + fiveByte
sixLens := totalLens - 30
needZero := 8 - sixLens - 7
sixByte := "0000001"
for i:= 0; i < needZero; i++ {
sixByte += "0"
}
sixByte += unicodeValByte[:sixLens]
encodeStr := sixByte + fiveByte + fourByte + threeByte + secondByte + firstByte
decNum,err := strconv.ParseInt(encodeStr,2,64)
if err != nil{
return ""
}
res := strconv.FormatInt(decNum,16)
return res
}
// 待实现函数,在此函数中填入答题代码
func utfEncoding(unicodeVal string) string {
res := ""
unicodeValInt, err := strconv.Atoi(unicodeVal)
if err != nil {
return ""
}
oneLimit := int(math.Pow(2, 7))
twoLimit := int(math.Pow(2, 15))
if 0 <= unicodeValInt && unicodeValInt < oneLimit {
res = encodeOne(unicodeValInt)
} else if oneLimit <= unicodeValInt && unicodeValInt < twoLimit {
res = encodeTwo(unicodeValInt)
} else if 2048 <= unicodeValInt && unicodeValInt < 65536 {
res = encodeThree(unicodeValInt)
} else if 65536 <= unicodeValInt && unicodeValInt < 2097152 {
res = encodeFour(unicodeValInt)
} else if 2097152 <= unicodeValInt && unicodeValInt < 67108864 {
res = encodeFive(unicodeValInt)
} else if 67108864 <= unicodeValInt && unicodeValInt < 2147483648 {
res = encodeSix(unicodeValInt)
}
res = strings.ToUpper(res)
if len(res) % 2 != 0 {
res = "0" + res
}
return res
}
func main() {
inputReader := bufio.NewReader(os.Stdin)
unicodeVal, err := inputReader.ReadString('\n')
if err != nil && err != io.EOF {
fmt.Println(err.Error())
return
}
unicodeVal = strings.TrimRight(unicodeVal, "\r\n")
unicodeVal = strings.TrimSpace(unicodeVal)
fmt.Println(utfEncoding(unicodeVal))
}
标签:strconv,unicodeValByte,res,totalLens,unicodeVal,20220722,可信,secondByte,考试 来源: https://www.cnblogs.com/gongxianjin/p/16510601.html