蓝桥杯 基础练习 十六进制转八进制

import java.util.*;
public class Main {
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
        String[] st = new String[n];
for (int i = 0; i < n; i++) {
            st[i] = sc.next();
        }
        sc.close();
for (int i = 0; i < n; i++) {
            String str1 = ttos(st[i]);
int len_str1 = str1.length();
            if (len_str1 % 3 == 1) str1 = "00" + str1;
else if (len_str1 % 3 == 2) str1 = "0" + str1;
            ttoe(str1);
            System.out.println();
        }
    }

public static String ttos(String str) {
int len_str = str.length();
        StringBuilder str2 = new StringBuilder();
for (int i = 0; i < len_str; ++i) {
switch (str.charAt(i)) {
case '0':
                    str2.append("0000");
break;
case '1':
                    str2.append("0001");
break;
case '2':
                    str2.append("0010");
break;
case '3':
                    str2.append("0011");
break;
case '4':
                    str2.append("0100");
break;
case '5':
                    str2.append("0101");
break;
case '6':
                    str2.append("0110");
break;
case '7':
                    str2.append("0111");
break;
case '8':
                    str2.append("1000");
break;
case '9':
                    str2.append("1001");
break;
case 'A':
                    str2.append("1010");
break;
case 'B':
                    str2.append("1011");
break;
case 'C':
                    str2.append("1100");
break;
case 'D':
                    str2.append("1101");
break;
case 'E':
                    str2.append("1110");
break;
case 'F':
                    str2.append("1111");
break;
default:
break;
            }
        }
return str2.toString();
    }

public static void ttoe(String str2) {
int len = str2.length();
        int a;
        a = (str2.charAt(0) - '0') * 4 + (str2.charAt(1) - '0') * 2 + (str2.charAt(2) - '0');
if (a != 0) System.out.print(a);
for (int i = 3; i <= len - 3; i += 3) {
            a = (str2.charAt(i) - '0') * 4 + (str2.charAt(i + 1) - '0') * 2 + (str2.charAt(i + 2) - '0');
            System.out.print(a);
        }
 }
}

在我解决蓝桥练习的十六进制转八进制的练习中,我想要使用Integer的parseInt方法来实现将16进制的数封装到Integer对象中再输出为8进制。

        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            String s=sc.nextLine();
            a[i] = Integer.parseInt(s, 16);
        }
        for (long b : a) {
            System.out.println(Long.toOctalString(b));
        }
        sc.close();

运行小例子没问题,但是送到系统出了错。是parseInt()方法要封装的数值超出了int的范围

例如:

  parseInt(“2147483648”, 10) 抛出 NumberFormatException
  parseInt(“99”, 8) 抛出 NumberFormatException
  parseInt(“Kona”, 10) 抛出 NumberFormatException

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值