JAVA 实现手机号码压缩,解压
{
int encnum = Integer.parseInt(number.substring(2));
int prefix = (number.charAt(1) - ‘2‘) /2; // ‘3‘=>0, ‘5‘=>1, ‘[67]‘=>2, ‘8‘=>3
encnum |= (prefix <<= 30);
final byte[] raw = new byte[4];
raw[0] = (byte)(encnum >>> 24);
raw[1] = (byte)(encnum >>> 16);
raw[2] = (byte)(encnum >>> 8);
raw[3] = (byte) encnum;
return Base64.encodeBase64URLSafeString(raw);
}
String decodePhoneNumber(String encoded)
{
final byte[] raw = Base64.decodeBase64(encoded);
int encnum = (raw[0]) << 24
| ((raw[1] & 0xFF) << 16)
| ((raw[2] & 0xFF) << 8)
| (raw[3] & 0xFF);
StringBuilder number = new StringBuilder(11);
number.append(‘1‘);
final char[] prefix = {‘3‘,‘5‘,‘6‘,‘8‘};
number.append(prefix[encnum >>> 30]);
String suffix = Integer.toString(encnum & ~(3<<30));
int pad = 9 - suffix.length();
if (pad > 0)
{
final char[] padding = {‘0‘,‘0‘,‘0‘,‘0‘,‘0‘,‘0‘,‘0‘,‘0‘}; // max 8 ‘0‘s
number.append(padding, 0, pad);
}
number.append(suffix);
return number.toString();
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。