Java implementation of unsigned number conversion, string completion, MD5, UUID, random number of examples _java

Source: Internet
Author: User
Tags md5 uuid



Copy Code code as follows:

Package com.hongyuan.test;

Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
Import Java.util.Random;
Import Java.util.UUID;


/*
* Miscellaneous (unsigned number conversion, string completion, Md5,uuid, random number)
*/
public class Sundrytest {

Convert to unsigned number
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;
}
}
Left padded
public static string Leftpad (String str,string pad,int len) {
String newstr= (str==null? ": STR);
while (Newstr.length () <len) {
NEWSTR=PAD+NEWSTR;
}
if (Newstr.length () >len) {
Newstr=newstr.substring (Newstr.length ()-len);
}
return newstr;
}
Right padded
public static string Rightpad (String str,string pad,int len) {
String newstr= (str==null? ": STR);
while (Newstr.length () <len) {
Newstr=newstr+pad;
}
if (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<message.length;i++) {
Sb.append (Leftpad//left-padded
Integer.tohexstring (///turn into 16 number
(Integer) Tounsignednumber (Message[i]),//convert to unsigned number
"0", 2). toUpperCase ()); Turn into uppercase
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException ("There is no MD5 service!") ");
}
return sb.tostring ();
}
Uuid
public static String uuid () {
Return Uuid.randomuuid (). toString (). ReplaceAll ("-", ""). toUpperCase ();
}

Random number (including Min, excluding Max)
public static int random (int min,int max) {
if (Min<=max) {
Random random=new Random ();
Return Random.nextint (max-min) +min;
}else{
throw new IllegalArgumentException ("Unable to handle an illegal digital range!") ");
}

}

public static void Main (string[] args) {
System.out.println ("MD5 (123456):" +md5 ("123456"));
System.out.println ("UUID:" +uuid ());
System.out.println ("Random Number:" +random (1,100));
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.