首页 > 解决方案 > 在 MIPS 中将十六进制转换为十进制

问题描述

我将如何将此 Java 代码转换为 MIPS?我很难在 MIPS 中实现“基础”的概念。我将如何使用 mips 并弄清楚它是什么基础?例如,假设我们有 0xFFF、0x3C、0xAA

它会是什么样子?

// Function to convert hexadecimal to decimal 
    static int hexadecimalToDecimal(String hexVal) 
    {    
        int len = hexVal.length(); 

        // Initializing base value to 1, i.e 16^0 
        int base = 1; 

        int dec_val = 0; 

        // Extracting characters as digits from last character 
        for (int i=len-1; i>=0; i--) 
        {    
            // if character lies in '0'-'9', converting  
            // it to integral 0-9 by subtracting 48 from 
            // ASCII value 
            if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9') 
            { 
                dec_val += (hexVal.charAt(i) - 48)*base; 

                // incrementing base by power 
                base = base * 16; 
            } 

            // if character lies in 'A'-'F' , converting  
            // it to integral 10 - 15 by subtracting 55  
            // from ASCII value 
            else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F') 
            { 
                dec_val += (hexVal.charAt(i) - 55)*base; 

                // incrementing base by power 
                base = base*16; 
            } 
        } 
        return dec_val; 
    } 

标签: javamips

解决方案


推荐阅读