/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
* Description: 上机编程认证
* Note: 缺省代码仅供参考,可自行决定使用、修改或删除
* 只能import Go标准库
*/

package main

import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)

func getDecimalByteLength(decimalLens int) int {
if decimalLens < 0 {
return -1
}
if decimalLens >= 0 && decimalLens < 8 {
return 1
}
if decimalLens >= 8 && decimalLens < 12 {
return 2
}
if decimalLens >= 12 && decimalLens < 17 {
return 3
}
if decimalLens >= 17 && decimalLens < 22 {
return 4
}
if decimalLens >= 22 && decimalLens < 27 {
return 5
}
if decimalLens >= 27 && decimalLens < 32 {
return 6
}
return -1
}

// 待实现函数,在此函数中填入答题代码
func utfEncoding(unicodeVal string) string {
// 转为 二进制判断个数
unicodeIntVal,_ := strconv.ParseInt(unicodeVal, 10, 64)
decimalVal := fmt.Sprintf("%b", unicodeIntVal)
decimalLens := len(decimalVal)
decimalByteLens := getDecimalByteLength(decimalLens)
unicodeBytesVal := ""
if decimalByteLens == 1 {
needLens := 8 - decimalByteLens
needZeroLens := needLens - decimalLens
unicodeBytesVal = "1"
for i:= 0; i < needZeroLens; i++ {
unicodeBytesVal += "0"
}
unicodeBytesVal += decimalVal
} else {
unicodeCount := 0
for i := 0; i < decimalByteLens; i++ {
unicodeBytesVal += "0"
}
unicodeBytesVal += "1"
needHeaderLens := 8 - decimalByteLens - 1
actualHeaderLens := decimalLens - (decimalByteLens-1)*6

needZeroLens := needHeaderLens - actualHeaderLens
for i:= 0; i < needZeroLens; i++ {
unicodeBytesVal += "0"
}
for i:= 0; i < actualHeaderLens; i++ {
unicodeBytesVal += string(decimalVal[i])
}
for i:= actualHeaderLens; i < decimalLens; i++ {
if unicodeCount % 6 == 0 {
unicodeBytesVal += "01"
unicodeCount = 0
}
unicodeBytesVal += string(decimalVal[i])
unicodeCount ++
}
}
//转为16进制
unicodeDecVal,_ := strconv.ParseInt(unicodeBytesVal, 2, 64)
unicodeHexVal := strconv.FormatInt(unicodeDecVal, 16)
// 判断奇偶
if len(unicodeHexVal) % 2 != 0 {
unicodeHexVal = "0" + unicodeHexVal
}
// 转大写
unicodeHexVal = strings.ToUpper(unicodeHexVal)
fmt.Println(unicodeHexVal)
return unicodeHexVal
}

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))
}

最新文章

  1. java中HashMap详解
  2. master-slave(主/从)模式
  3. OpenWrt网络结构
  4. ios开发之C语言第一天
  5. c语言函数---M
  6. ASP.NET 5 :读写数据库连接字符串
  7. PS 滤镜算法原理——曝光过度
  8. 使用 Chrome DevTools 调试 JavaScript
  9. android6.0以上权限动态申请,有视频链接可以看效果。
  10. ACFS-9459: ADVM/ACFS is not supported on this OS version
  11. 算法笔记1 - 编辑距离及其动态规划算法(Java代码)
  12. Individual P1: Preparation
  13. 硬盘读取速度变慢 — 当前传送模式: PIO模式
  14. python 小白学习(1)
  15. websocket搭建错误
  16. Xcode6 itunes完美打包api 方法
  17. C语言 &#183; 求arccos值
  18. Java如何格式化秒数?
  19. 如何获取codeforces的完整数据?(玄学方法)
  20. leecode刷题(10)-- 旋转图像

热门文章

  1. linux中awk命令详解(最全面秒懂)
  2. 3款知名RTMP推流模块比较:OBS VS SmartPublisher VS Flash Media Live Encoder
  3. docker容器资源限制:限制容器对内存/CPU的访问
  4. Zookeeper 分布式事务锁的使用
  5. Nginx location总结
  6. MySQL8 Group By 新特性
  7. 一文了解 Java 中的构造器
  8. Python数据科学手册-机器学习之模型验证
  9. 跟羽夏学 Ghidra ——调试
  10. PAT (Basic Level) Practice 1026 程序运行时间 分数 15