kristenlee1218 2017-11-13 03:39 采纳率: 61.3%
浏览 1811

如何调用这个工具类?

public class Register
{
private static final String smcode = "0123456789ABCDEF";

public static String createRegCode(String mcode)
{
if (mcode == null)
return null;

mcode = mcode.trim().toUpperCase().replace("-", "");
int len = mcode.length();
if (len != 25)
  return null;

for (int i = 0; i < len; ++i) {
  char c = mcode.charAt(i);
  if ("0123456789ABCDEF".indexOf(c) == -1)
    return null;
}

String rcode = createCode(mcode);
String srcode = splitRCode(rcode);
if (srcode != null)
  return srcode;

return null;

}

private static String md5(String str) {
try {
md5 = MessageDigest.getInstance("MD5");
return bytesToHex(md5.digest(str.getBytes())).toUpperCase();
} catch (java.lang.Exception md5) {
}
return null; }

private static String bytesToHex(byte[] bytes) {
StringBuffer md5str = new StringBuffer();

for (int i = 0; i < bytes.length; ++i) {
  int digital = bytes[i];

  if (digital < 0)
    digital += 256;

  if (digital < 16)
    md5str.append("0");

  md5str.append(Integer.toHexString(digital));
}
return md5str.toString().toUpperCase();

}

private static String createCode(String mcode)
{
mcode = mcode.replace("-", "");
char chr = mcode.charAt(20);
mcode = mcode.toUpperCase().substring(0, 20);
String str = md5(mcode);
String rcode = str.substring(0, 20);
rcode = rcode + chr;
rcode = rcode + genExtCode(str);
return rcode;
}

private static String splitRCode(String rcode)
{
StringBuffer buffer = new StringBuffer();
int len = rcode.length();
for (int i = 0; i < len; i += 5) {
if (buffer.length() > 0)
buffer.append("-");

  buffer.append(rcode.substring(i, i + 5));
}
return buffer.toString();

}

private static String genExtCode(String mcode)
{
StringBuffer buffer = new StringBuffer();
for (int i = 3; i >= 0; --i)
buffer.append(mcode.charAt(i * 5));

return buffer.toString();

}
}

  • 写回答

7条回答 默认 最新

  • kristenlee1218 2017-11-13 03:39
    关注

    随机生成字符串,看不懂,不会用。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序