认证0级讲师
Stuck on the step of grouping char[] and then converting it to int
Do it directly like this, convert every 5 to an int, sample code (may have BUG):
char[] chArr = {0,0,0,0,1,1,1,0,0,1}; for (int i = 0; i < chArr.length; i+=5) { // 直接组装为int int result = 0; for (int j = 0; j < 5; j++) { result += (int) chArr[i+j] * MathUtil.pow(2, 4-j); } System.out.println(result); } /* MathUtil.java */ /** * 整数的几次方 * @param baseNumber 底数 * @param exponent 指数 * @return 计算出的值 */ public static int pow(int baseNumber, int exponent) { if (exponent < 0) { throw new IllegalArgumentException("exponent cant not be a negative number"); } if (exponent == 0) { return 1; } return baseNumber << (exponent - 1); }
Do it directly like this, convert every 5 to an int, sample code (may have BUG):