字符轉換工具類

public class Utils {


/**
* IpAddress 轉成String
* @param i
* @return
*/
public static String intToString(int i) {
return ((i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + ((i >> 24) & 0xFF));
}

public static boolean ipIsExact(String ip) {
Pattern pattern = Pattern
.compile("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
return pattern.matcher(ip).matches();
}

/**
* url轉成String
* @param url
* @return
*/
public static String[] urlToString(String url){
String[] result=url.split("//");
String[] str=result[1].split(":");
System.err.println(str[0]);
System.err.println(str[1]);
return str;
}

/**
* 將路徑轉換成 可用路徑
* @param path
* @return
*/
public static String matchPath(String path){
if(path.startsWith("/")){
return path.substring(1, path.length());
}
return path;
}

public static int bytesToInt(byte[] intByte) {   
int fromByte = 0;
for (int i = 0; i < 2; i++) {
int n = (intByte[i] < 0 ? (int) intByte[i] + 256 : (int) intByte[i]) << (8 * (i - 1));
fromByte += n;
}
return fromByte;
}

public static long StringToLong(String str){
long result = Long.valueOf(str, 16);
return result;
}

public static String getStringLengisEight(String str){
String result = str;
for (int i = str.length(); i < 8; i++) {
result = "0" + result;
}
if (result.length() > 8) {
result = result.substring(result.length() - 8);
}
result = result.toUpperCase();
return result;
}

public static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}

public static long bytes2Long(byte[] b){
String str = bytes2HexString(b);
return Long.valueOf(str, 16);
}

public static int bytes2Int(byte[] b){
String str = bytes2HexString(b);
return Integer.valueOf(str, 16);
}

//把8位16進制字符串轉爲整數
public static int tranferHexStringToInt(String hex){
long l1=Long.parseLong(hex, 16);
if(l1>0x7fffffff){
l1=l1-0x7fffffff;
}
return (int)l1;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章