ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

可信考试第三题-20220722

2022-07-23 10:04:22  阅读:140  来源: 互联网

标签:strconv unicodeValByte res totalLens unicodeVal 20220722 可信 secondByte 考试


/*
* 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有