简体   繁体   English

如何向后读取二进制字符串数组,然后转换为十进制int变量? (不解析)

[英]How to read binary string array backwards, then convert to decimal int variable? (Without parse)

I do not understand how to code the following into the program: Read string binary array backwards. 我不明白如何将以下代码编码到程序中:向后读取字符串二进制数组。 Check reversed binary array for 1 (I assume if statements). 检查反向二进制数组是否为1(我假设为if语句)。 Assign bit values for each 1 in binary. 为二进制中的每个1分配位值。 Utilize math.pow to get decimal value for each 1. Add all the active bits. 利用math.pow获得每个1的十进制值。将所有活动位相加。

My program needs to do the following: Open a text file (COMPLETED) Text file contains: 我的程序需要执行以下操作:打开一个文本文件(已完成),该文本文件包含:

 (33CDAEFFAD)  
 (032DAE01AD)  
 (196CDAEFC0)  
 (21A00D0000)  
 (100CDAEFFA)  
 (F3ABCDEFAB)  
 (29A0EDF301)  
 (3ABCDEFABC)  

Read each hex (COMPLETED) --> 阅读每个十六进制(已完成)->
Convert hex value to binary (COMPLETED) --> 将十六进制值转换为二进制(已完成)->
Convert the binary value to decimal value (LOST) 将二进制值转换为十进制值(LOST)

public static String[] hexToBinary () throws IOException //converts from 
hex to binary
{
    Scanner inFile = new Scanner(new File("RAMun3"));  

String result[] = new String[8]; //created array to hold binary values
    String bValue; 
    String x = ""; 
String y = "";
int counter = 0;      

    while (inFile.hasNextLine() && counter <= 7) //reads lines from text 
    file && stops array from                             
    going out of bounds
    {   
        String line = inFile.nextLine();
        Scanner input = new Scanner(line);
        String hex = input.next();

        for (int i = 0; i < hex.length(); i++) //for loop to convert hex 
        digits to binary 
        {
            char hexC = hex.charAt(i);
        switch (hexC) 
            {
             case ('0'):
                    bValue = "0000";
                    break;   
             case ('1'):
                    bValue = "0001";
                    break;
             case ('2'):
                    bValue = "0010";
                    break;
             case ('3'):
                    bValue = "0011";
                    break;
             case ('4'):
                    bValue = "0100";
                    break;
             case ('5'):
                    bValue = "0101";
                    break;
             case ('6'):
                    bValue = "0110";
                    break;
             case ('7'):
                    bValue = "0111";
                    break;
             case ('8'):
                    bValue = "1000";
                    break;
             case ('9'):
                    bValue = "1001";
                    break;
             case ('A'):
                    bValue = "1010";
                    break;
             case ('B'):
                    bValue = "1011";
                    break;
             case ('C'):
                    bValue = "1100";
                    break;
             case ('D'):
                    bValue = "1101";
                    break;
             case ('E'):
                    bValue = "1110";
                    break;
             case ('F'):
                    bValue = "1111";
                    break;    
             default:
                    bValue = "N/A";
                    break;
                } 
    x = bValue;
    y += x;     
            }       
    result[counter] = y;
    counter++; 
    y = "";     
        }
        for (int t = 0; t < result.length; t++)
            {   
        System.out.println(result[t]);      
            }
return result;
}

public static void reverseResult(String[] y) //Attempt at code for  
reversing array
{
    for (int i = y.length-1; i>=0; i--) 
{
    System.out.print(y[i]+" ");     
}
}

public static int binToDecimal (){}

public static void main (String args[]) throws IOException //main
{
    readFromFile();
hexToBinary();   
binToDecimal();
}

} 

Expected outputs after conversion: 转换后的预期输出:

222494130093 -- 13651280301 -- 109200469952 -- 144419127296 -- 68935151610 -- 1046559453099 -- 178793607937 -- 252276832956

After reversing every String in result: 反转结果中的每个字符串后:

public static int binToDecimal (){
    for(String bin:result){
        int dec=0;
        for(int i=0;i<bin.length;i++){
          dec+=Math.pow(2,i)*(bin.charAt(i)-'0');
        }
        println("" + dec);
    }
}

With "if" statement as you mentioned it: 如您提到的那样,使用“ if”语句:

 public static int binToDecimal (){
    for(String bin:result){
        int dec=0;
        for(int i=0;i<bin.length;i++){
          if('1' == bin.charAt(i)){
              dec+=Math.pow(2,i);
          }
        }
        println("" + dec);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM