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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)