package com.shengshihui.util;import java.util.HashMap;
import java.util.Map;public class PhoneUtil {
private static Map map;
static{
map = new HashMap();
map.put("k", "134");
map.put("v", "159");
map.put("134", "k");
map.put("159", "v");
map.put("d", "138");
map.put("138", "d");
} //将手机号转换为字符串,作为url参数传递
public String toHexString(String phone){

String key = (String) map.get(phone.substring(0, 3));//将手机号前3位转换成一个字母
System.out.println("前三位:"+phone.substring(0,3));
System.out.println("key----------"+key);
System.out.println("后八位:"+phone.substring(3, phone.length()));
String phone_new = Integer.toHexString(Integer.parseInt(phone.substring(3, 11)));//4到10位
System.out.println("phone_new======="+phone_new);
String m = key.concat(phone_new);
return m; }
//将字符串还原为手机号
public String toPhone(String m){
String sec_phone = m.substring(0,1);
String phone_pre = (String)map.get(sec_phone);
String phone_aft = Integer.toString(Integer.parseInt(m.replaceFirst(sec_phone, ""), 16));
String phone_true = phone_pre.concat(phone_aft);//真实的手机号
return phone_true;
}

public static void main(String[] args) {
String phone1 = "13436896227"; //13801130377(错) 13436896227(对)
PhoneUtil util = new PhoneUtil();
String m1 = util.toHexString(phone1); System.out.println("m1======="+m1);

System.out.println("真实的手机号:====="+util.toPhone(m1));
}


}

解决方案 »

  1.   


    public String toPhone(String m){
    String sec_phone = m.substring(0,1);
    String phone_pre = (String)map.get(sec_phone);
    String phone_aft = Integer.toString(Integer.parseInt(m.replaceFirst(sec_phone, ""), 16));
    //加这段,自己揣摩原因
    if(phone_aft.length() < 8)
    {
        int lengthI = phone_aft.length();
        for(int i=0;i<8-lengthI;i++)
            phone_aft = "0" + phone_aft;
    }
    String phone_true = phone_pre.concat(phone_aft);
    return phone_true; 
    }