• 字符编码 XUTF



    /*
    * 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))
    }
  • 相关阅读:
    电子书下载:Beginning ASP.NET 2.0 AJAX
    电子书下载:C# 4.0 How To
    电子书下载:Pragmatic Unit Testing in C# with NUnit
    7 个最好的.Net开源CMS系统
    电子书下载:Professional ASP.NET 2.0 Server Control and Component Development
    电子书下载:Beginning ASP.NET 2.0 Databases From Novice to Professional
    电子书下载:Professional .NET 2.0 Generics
    蛙蛙推荐:[算法练习]最长不完全匹配子串频率计算
    蛙蛙推荐:F#实现并行排序算法
    蛙蛙推荐:蛙蛙教你发明一种新语言之二代码生成
  • 原文地址:https://www.cnblogs.com/gongxianjin/p/16755819.html
Copyright © 2020-2023  润新知