如何将3个八进制数的输入转换为CHMOD权限到Binary?

时间:2015-12-12 00:08:42

标签: java unix binary chmod octal

我正在尝试创建一个程序,使用3个八进制数的命令行(例如5,2,6或5,2,6)从用户那里获取输入,并将它们转换为3组3位二进制数,如101 010 110,并打印出相应的CHMOD权限,如rx -w-rw - 。

我很难将这些数字与子串拼接成3个独立的数字5 2和6。

我还需要程序将二进制数字组转换为3位数字和权限。并从权限转换为数字和二进制。

这是我到目前为止所做的:

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 个答案:

答案 0 :(得分:4)

如果您使代码更简单,您会发现它更容易。我建议像

这样的东西
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();
}

打印

r-x -w- rw-