LA_DAN 2018-11-26 08:31 采纳率: 0%
浏览 1809
已采纳

JAVA怎么转化8进制字符串

例如我得到的字符串为:\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-11-26 09:52
    关注

    如果问题得到解决,请点我回答左上角的采纳和向上的箭头

    class Untitled {
        public static String toOct(String s)
        {
            String result = "";
            for(char c : s.toCharArray())
            {
                result += "\\" + (c / 64) % 8 +  "" + (c / 8) % 8 + "" + c % 8;
            }
            return result;
        }
    
        public static String getOct(String s)
        {
            String result = "";     
            for (String i : s.split("\\\\"))
            {
                int sum = 0;
                int base = 64;
                for (char c : i.toCharArray())
                {
                    sum += base * ((int)c - '0');
                    base /= 8;
                }
                result += Character.toString((char)sum);
            }
            return result;
        }
    
        public static void main(String[] args) {
            String s = "abcd";
            String o = toOct(s);
            System.out.println(o);
            s = getOct(o);      
            System.out.println(s);
        }
    }
    

    运行结果
    \141\142\143\144
    abcd

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?