java实现无符号数转换、字符串补齐、md5、uuid、随机数示例
本文导语: 代码如下:package com.hongyuan.test; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Random;import java.util.UUID; /* * 杂项(无符号数转换,字符串补齐,md5,uuid,随机数) */public class SundryTest { //转成无符号数 ...
package com.hongyuan.test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;
/*
* 杂项(无符号数转换,字符串补齐,md5,uuid,随机数)
*/
public class SundryTest {
//转成无符号数
public static Number toUnsignedNumber(Number num){
if(num instanceof Byte){
return (Byte)num & 0xff;
}else if(num instanceof Short){
return (Short)num & 0xffff;
}else if(num instanceof Integer){
return (Integer)num & 0xffffffffL;
}else{
return -1;
}
}
//左补齐
public static String leftPad(String str,String pad,int len){
String newStr=(str==null?"":str);
while(newStr.length()len){
newStr=newStr.substring(newStr.length()-len);
}
return newStr;
}
//右补齐
public static String rightPad(String str,String pad,int len){
String newStr=(str==null?"":str);
while(newStr.length()len){
newStr=newStr.substring(0, len);
}
return newStr;
}
//md5
public static String md5(String str){
StringBuilder sb=new StringBuilder();
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] message=digest.digest(str.getBytes());
for(int i=0;i