How to convert an input of 3 octal numbers into CHMOD permissions into Binary?

孤者浪人 提交于 2019-12-23 02:41:05

问题


I am trying to create a program that takes input from the user using the command line of 3 octal number, for example 5, 2, 6 or 5,2,6 and convert them into 3 sets of 3 digit binary numbers, like 101 010 110, and also print out those corresponding CHMOD permissions like r-x -w- rw-.

I am having a lot of trouble splicing these numbers apart with substring into 3 separate numbers of 5 2 and 6.

I also need the program to convert from the set of binary digits into 3 numerical digits and permissions. And from the permissions into digits and binary.

Here is what I have so far:

import java.util.Scanner;
class Test {
        public static void main(String cheese[]){

            Scanner scan = new Scanner(System.in);

            String line1 = scan.nextLine();
            //String line2 = scan.nextLine();
            //String line3 = scan.nextLine();
            //String line4 = scan.nextLine();
            //String line5 = scan.nextLine();

            System.out.println(line12(line1));


        }

        public static String line12(String line){
            String ownerPermissions = "";
            String groupPermissions = "";
            String otherPermissions = "";

            int comma = 0;

            int lineLength = line.length();

            if (line.indexOf(" ") != -1){
                comma = line.indexOf(",");
            }else{
                comma = 1;
            }

            String firstNumber = line.substring(0, 1);

            line = line.substring(comma + 1, lineLength);

            int comma2 = line.indexOf(",");
            String secondNumber = line.substring(comma + 1, comma2);

            String thirdNumber = line.substring(lineLength);

            int firstInt= Integer.parseInt(firstNumber);
            int secondInt = Integer.parseInt(secondNumber);
            int thirdInt = Integer.parseInt(thirdNumber);

            String firstBinary = Integer.toBinaryString(firstInt);
            String secondBinary = Integer.toBinaryString(secondInt);
            String thirdBinary = Integer.toBinaryString(thirdInt);

            switch(firstInt){

                case 0:
                    ownerPermissions = "---";
                    break;
                case 1:
                    ownerPermissions = "--x";
                    break;
                case 2:
                    ownerPermissions = "-w-";
                    break;
                case 3:
                    ownerPermissions = "-wx";
                    break;
                case 4:
                    ownerPermissions = "r--";
                    break;
                case 5:
                    ownerPermissions = "r-x";
                    break;
                case 6:
                    ownerPermissions = "rw-";
                    break;
                case 7:
                    ownerPermissions = "rwx";
                    break;
            }

            switch(secondInt){

                case 0:
                    groupPermissions = "---";
                    break;
                case 1:
                    groupPermissions = "--x";
                    break;
                case 2:
                    groupPermissions = "-w-";
                    break;
                case 3:
                    groupPermissions = "-wx";
                    break;
                case 4:
                    groupPermissions = "r--";
                    break;
                case 5:
                    groupPermissions = "r-x";
                    break;
                case 6:
                    groupPermissions = "rw-";
                    break;
                case 7:
                    groupPermissions = "rwx";
                    break;
            }

            switch(thirdInt){

                case 0:
                    otherPermissions = "---";
                    break;
                case 1:
                    otherPermissions = "--x";
                    break;
                case 2:
                     otherPermissions = "-w-";
                    break;
                case 3:
                    otherPermissions = "-wx";
                    break;
                case 4:
                    otherPermissions = "r--";
                    break;
                case 5:
                    otherPermissions = "r-x";
                    break;
                case 6:
                    otherPermissions = "rw-";
                    break;
                case 7:
                    otherPermissions = "rwx";
                    break;
            }
            String output = firstBinary + " " + secondBinary + " " + thirdBinary + " and " + ownerPermissions + " " + groupPermissions + " " + otherPermissions;

            return output;
        }
}

回答1:


You will find it easier if you make your code much simpler. I suggest something like

public static void main(String[] args) {
    System.out.println(line12("5, 2, 6"));
}

public static String line12(String line) {
    String[] nums = line.trim().split(" *, *");
    StringBuilder sb = new StringBuilder();
    for (String s : nums) {
        if (sb.length() > 0) sb.append(" ");
        int num = Integer.parseInt(s);
        sb.append((num & 4) == 0 ? '-' : 'r');
        sb.append((num & 2) == 0 ? '-' : 'w');
        sb.append((num & 1) == 0 ? '-' : 'x');
    }
    return sb.toString();
}

prints

r-x -w- rw-


来源:https://stackoverflow.com/questions/34234598/how-to-convert-an-input-of-3-octal-numbers-into-chmod-permissions-into-binary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!