import java.util.Locale;
import java.util.regex.Pattern;
public class BinaryUtil {
public static final String TRANSVERSE= "-";
//10进制转16进制
public static String IntToHex(int n){
char[] ch = new char[20];
int nIndex = 0;
while ( true ){
int m = n/16;
int k = n%16;
if ( k == 15 )
ch[nIndex] = 'F';
else if ( k == 14 )
ch[nIndex] = 'E';
else if ( k == 13 )
ch[nIndex] = 'D';
else if ( k == 12 )
ch[nIndex] = 'C';
else if ( k == 11 )
ch[nIndex] = 'B';
else if ( k == 10 )
ch[nIndex] = 'A';
else
ch[nIndex] = (char)('0' + k);
nIndex++;
if ( m == 0 )
break;
n = m;
}
StringBuffer sb = new StringBuffer();
sb.append(ch, 0, nIndex);
sb.reverse();
String strHex = new String("0x");
strHex += sb.toString();
return strHex;
}
//16进制转10进制
public static int HexToInt(String strHex){
int nResult = 0;
if ( !IsHex(strHex) )
return nResult;
String str = strHex.toUpperCase();
if ( str.length() > 2 ){
if ( str.charAt(0) == '0' && str.charAt(1) == 'X' ){
str = str.substring(2);
}
}
int nLen = str.length();
for ( int i=0; i<nLen; ++i ){
char ch = str.charAt(nLen-i-1);
try {
nResult += (GetHex(ch)*GetPower(16, i));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nResult;
}
//计算16进制对应的数值
public static int GetHex(char ch) throws Exception{
if ( ch>='0' && ch<='9' )
return (int)(ch-'0');
if ( ch>='a' && ch<='f' )
return (int)(ch-'a'+10);
if ( ch>='A' && ch<='F' )
return (int)(ch-'A'+10);
throw new Exception("error param");
}
//计算幂
public static int GetPower(int nValue, int nCount) throws Exception{
if ( nCount <0 )
throw new Exception("nCount can't small than 1!");
if ( nCount == 0 )
return 1;
int nSum = 1;
for ( int i=0; i<nCount; ++i ){
nSum = nSum*nValue;
}
return nSum;
}
//判断是否是16进制数
public static boolean IsHex(String strHex){
int i = 0;
if ( strHex.length() > 2 ){
if ( strHex.charAt(0) == '0' && (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x') ){
i = 2;
}
}
for ( ; i<strHex.length(); ++i ){
char ch = strHex.charAt(i);
if ( (ch>='0' && ch<='9') || (ch>='A' && ch<='F') || (ch>='a' && ch<='f') )
continue;
return false;
}
return true;
}
/**
* Mac加法
* @author YOLANDA
* @param mac Mac地址,eg:ABCDEF56BFD0
* @param add 要加的数
* @return
*
*/
public static String getMacAdd(String mac, long add) {
return Long.toHexString(Long.parseLong(mac, 16) + add).toUpperCase(Locale.getDefault());
}
/**
* Mac减法
* @author YOLANDA
* @param mac Mac地址:eg:ABCDEF56BFD0
* @param minus 要减的数
* @return
*
*/
public static String getMacMinus(String mac, long minus) {
String s = Long.toHexString(Long.parseLong(mac, 16) - minus).toUpperCase(Locale.getDefault());
//不足 补0 例如 00 00 00 00 00 12
if (s.length() < 12){
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0 ;i< 12-s.length() ; i++ ){
stringBuilder.append("0");
}
s = stringBuilder.toString()+s ;
}
return s;
}
/**
* @param mac "7000000000BD"
* @param split "-"
* @return "70-00-00-00-00-BD"
*/
public static String macAppendSplit(String mac, String split) {
int length = mac.length();
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < length; i++) {
char charAt = mac.charAt(i);
buffer.append(charAt);
if (i % 2 != 0 && i != length - 1) {
buffer.append(split);
}
}
return buffer.toString();
}
/**
*
* @param mac "70-00-00-00-00-BD"
* @param split "-"
* @return 7000000000BD
*/
public static String macRemoveSplit(String mac, String split) {
String[] newMac = mac.split(split);
StringBuffer buffer = new StringBuffer("");
for (String s : newMac) {
buffer.append(s);
}
return buffer.toString();
}
/**
* 计算两个mac地址 之间有多少个
* @param macStart mac地址初始位置 70-00-00-00-00-00
* @param macEnd mac地址结束位置 70-00-00-00-00-09
* @param split 分割 "-"
* @return 返回10个
*/
public static long countMac(String macStart, String macEnd,String split){
String hexStart = macStart.replaceAll(split, "");
String hexEnd = macEnd.replaceAll(split, "");
long start = Long.parseLong(hexStart, 16);
long end = Long.parseLong(hexEnd, 16);
return end-start+1;
}
/**
* 判断是否为mac地址 [a-fA-F0-9]匹配十六进制
* @param mac 70-00-00-00-00-09
* @param split "-"
* @return
*/
public static Boolean isMacAddress(String mac,String split) {
String patternMac = "^[a-fA-F0-9]{2}"+split
+ "[a-fA-F0-9]{2}"+split
+ "[a-fA-F0-9]{2}"+split
+ "[a-fA-F0-9]{2}"+split
+ "[a-fA-F0-9]{2}"+split
+ "[a-fA-F0-9]{2}$";
Pattern pa = Pattern.compile(patternMac);
boolean isMac = pa.matcher(mac).find();
return isMac;
}
}
MAC地址处理工具类
于 2022-03-21 09:33:49 首次发布