zoukankan      html  css  js  c++  java
  • 3.6 二进制,十进制,十六进制转换

    
    package main
    
    import (
    	"fmt"
    	"strconv"
    )
    
    const bin = "10111"
    const hex = "1A"
    const oct = "12"
    const dec = "10"
    const floatNum = 16.123557
    
    func main() {
    
    	// Converts binary value into hex
    	v, _ := ConvertInt(bin, 2, 16)
    	fmt.Printf("Binary value %s converted to hex: %s
    ", bin, v)
    
    	// Converts hex value into dec
    	v, _ = ConvertInt(hex, 16, 10)
    	fmt.Printf("Hex value %s converted to dec: %s
    ", hex, v)
    
    	// Converts oct value into hex
    	v, _ = ConvertInt(oct, 8, 16)
    	fmt.Printf("Oct value %s converted to hex: %s
    ", oct, v)
    
    	// Converts dec value into oct
    	v, _ = ConvertInt(dec, 10, 8)
    	fmt.Printf("Dec value %s converted to oct: %s
    ", dec, v)
    
    	//... analogically any other conversion
    	// could be done.
    
    }
    
    // ConvertInt converts the given string value of base
    // to defined toBase.
    func ConvertInt(val string, base, toBase int) (string, error) {
    	i, err := strconv.ParseInt(val, base, 64)
    	if err != nil {
    		return "", err
    	}
    	return strconv.FormatInt(i, toBase), nil
    }
    
    /*
    Binary value 10111 converted to hex: 17
    Hex value 1A converted to dec: 26
    Oct value 12 converted to hex: a
    Dec value 10 converted to oct: 12
    
    */
    
    
  • 相关阅读:
    数据分析
    爬虫系统
    数据结构
    OpenStack系列
    python全栈开发之路
    机器学习——线性回归算法
    简单回测框架开发
    量化交易——羊驼交易法则
    量化交易——动量策略vs反转策略
    量化交易——PEG策略
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620779.html
Copyright © 2011-2022 走看看