import java.math.BigDecimal;
public class AbMathUtil{
/**
* 四舍五入.
*
* @param number 原数
* @param decimal 保留几位小数
* @return 四舍五入后的值
*/
public static BigDecimal round(double number, int decimal){
return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP);
}
/**
* 描述:字节数组转换成16进制串.
*
* @param b the b
* @param length the length
* @return the string
*/
public static String byte2HexStr(byte[] b, int length){
String hs = "";
String stmp = "";
for (int n = 0; n < length; ++n) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else {
hs = hs + stmp;
}
hs = hs + ",";
}
return hs.toUpperCase();
}
/**
* 二进制转为十六进制.
*
* @param binary the binary
* @return char hex
*/
public static char binaryToHex(int binary) {
char ch = ' ';
switch (binary){
case 0:
ch = '0';
break;
case 1:
ch = '1';
break;
case 2:
ch = '2';
break;
case 3:
ch = '3';
break;
case 4:
ch = '4';
break;
case 5:
ch = '5';
break;
case 6:
ch = '6';
break;
case 7:
ch = '7';
break;
case 8:
ch = '8';
break;
case 9:
ch = '9';
break;
case 10:
ch = 'a';
break;
case 11:
ch = 'b';
break;
case 12:
ch = 'c';
break;
case 13:
ch = 'd';
break;
case 14:
ch = 'e';
break;
case 15:
ch = 'f';
break;
default:
ch = ' ';
}
return ch;
}
/**
*
* 一维数组转为二维数组
*
*
* @param m the m
* @param width the width
* @param height the height
* @return the int[][]
*/
public static int[][] arrayToMatrix(int[] m, int width, int height) {
int[][] result = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int p = j * height + i;
result[i][j] = m[p];
}
}
return result;
}
/**
*
* 二维数组转为一维数组
*
*
* @param m the m
* @return the double[]
*/
public static double[] matrixToArray(double[][] m) {
int p = m.length * m[0].length;
double[] result = new double[p];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
int q = j * m.length + i;
result[q] = m[i][j];
}
}
return result;
}
/**
* 描述:int数组转换为double数组.
*
* @param input the input
* @return the double[]
*/
public static double[] intToDoubleArray(int[] input) {
int length = input.length;
double[] output = new double[length];
for (int i = 0; i < length; i++){
output[i] = Double.valueOf(String.valueOf(input[i]));
}
return output;
}
/**
* 描述:int二维数组转换为double二维数组.
*
* @param input the input
* @return the double[][]
*/
public static double[][] intToDoubleMatrix(int[][] input) {
int height = input.length;
int width = input[0].length;
double[][] output = new double[height][width];
for (int i = 0; i < height; i++) {
// 列
for (int j = 0; j < width; j++) {
// 行
output[i][j] = Double.valueOf(String.valueOf(input[i][j]));
}
}
return output;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(int[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(double[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
}
public class AbMathUtil{
/**
* 四舍五入.
*
* @param number 原数
* @param decimal 保留几位小数
* @return 四舍五入后的值
*/
public static BigDecimal round(double number, int decimal){
return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP);
}
/**
* 描述:字节数组转换成16进制串.
*
* @param b the b
* @param length the length
* @return the string
*/
public static String byte2HexStr(byte[] b, int length){
String hs = "";
String stmp = "";
for (int n = 0; n < length; ++n) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else {
hs = hs + stmp;
}
hs = hs + ",";
}
return hs.toUpperCase();
}
/**
* 二进制转为十六进制.
*
* @param binary the binary
* @return char hex
*/
public static char binaryToHex(int binary) {
char ch = ' ';
switch (binary){
case 0:
ch = '0';
break;
case 1:
ch = '1';
break;
case 2:
ch = '2';
break;
case 3:
ch = '3';
break;
case 4:
ch = '4';
break;
case 5:
ch = '5';
break;
case 6:
ch = '6';
break;
case 7:
ch = '7';
break;
case 8:
ch = '8';
break;
case 9:
ch = '9';
break;
case 10:
ch = 'a';
break;
case 11:
ch = 'b';
break;
case 12:
ch = 'c';
break;
case 13:
ch = 'd';
break;
case 14:
ch = 'e';
break;
case 15:
ch = 'f';
break;
default:
ch = ' ';
}
return ch;
}
/**
*
* 一维数组转为二维数组
*
*
* @param m the m
* @param width the width
* @param height the height
* @return the int[][]
*/
public static int[][] arrayToMatrix(int[] m, int width, int height) {
int[][] result = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int p = j * height + i;
result[i][j] = m[p];
}
}
return result;
}
/**
*
* 二维数组转为一维数组
*
*
* @param m the m
* @return the double[]
*/
public static double[] matrixToArray(double[][] m) {
int p = m.length * m[0].length;
double[] result = new double[p];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
int q = j * m.length + i;
result[q] = m[i][j];
}
}
return result;
}
/**
* 描述:int数组转换为double数组.
*
* @param input the input
* @return the double[]
*/
public static double[] intToDoubleArray(int[] input) {
int length = input.length;
double[] output = new double[length];
for (int i = 0; i < length; i++){
output[i] = Double.valueOf(String.valueOf(input[i]));
}
return output;
}
/**
* 描述:int二维数组转换为double二维数组.
*
* @param input the input
* @return the double[][]
*/
public static double[][] intToDoubleMatrix(int[][] input) {
int height = input.length;
int width = input[0].length;
double[][] output = new double[height][width];
for (int i = 0; i < height; i++) {
// 列
for (int j = 0; j < width; j++) {
// 行
output[i][j] = Double.valueOf(String.valueOf(input[i][j]));
}
}
return output;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(int[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
/**
* 计算数组的平均值.
*
* @param pixels 数组
* @return int 平均值
*/
public static int average(double[] pixels) {
float m = 0;
for (int i = 0; i < pixels.length; ++i) {
m += pixels[i];
}
m = m / pixels.length;
return (int) m;
}
}