java杂记

HexToInteger:

1
2
3
4
5
6
7
8
9
10
11
public class HexToInteger {
    public static void main(String[] args){
        int cid = 78823423;
        System.out.println((short)0x00000FFFF);  //(short)0x00000FFFF == 0xFFFF   结果为-1
        System.out.println(cid & (short)0x00000FFFF); //0x004B2BFF & 0xFFFFFFFF   结果为78823423
        System.out.println(cid & 0x00000FFFF);  //0x004B2BFF & 0x0000FFFF  结果为49151
        System.out.println(Integer.parseInt(Integer.toHexString(cid & 0x00000FFFF), 16)); //49151
        System.out.println(Integer.parseInt(String.valueOf(cid & 0x00000FFFF), 16)); //0x49151 == 299345
        System.out.println(String.valueOf(cid & 0x00000FFFF));  //49151
    }
}

equals的使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class User {
    String name;
    int age;
 
    public User(String name, int age){
        this.name = name;
        this.age = age;
    }
     
     
    public boolean equals(Object obj){
        if(this == obj){  //this代表调用这个函数的那个对象,这个函数位于//User这个类中,则this要调用它,必须为User类型
            return true;   
            }
         
        boolean b = obj instanceof User;
        if(b){
        User u = (User)obj;
        if(this.age == u.age && this.name.equals(u.name)){//基本数据类型用==,引用数据类型用equals
            return true;
            }else{
            return false;
            }
     
        }
        else{
        return false;
        }
    }
 
     
    public int hashCode(){
        int result = 17;
        result = 31 * result + age;
        result = 31 * result + name.hashCode();
        return result;
    }
 
}

HashSet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.HashSet;
import java.util.Set;
public class HashSetTest {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Set<String> set = new HashSet<String>();
        set.add("a");
        set.add("b");
        set.add("c");
        set.add("d");
        set.add("c");      
        int i = set.size();    
        set.remove("a");
    }
}

简单加密算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
 
import skycomm.db.ConnectOracle;
import skycomm.db.ConnectSqlite;
 
public class TestMain {
 
    /**
     * 位置信息解密的key值
     */
    private final static String DECODE_KEYS_ZONE = "skycomm";
 
    public Map<String, String> encMap;
    public Map<String, String> decMap;
     
    public List<ImsiToAddress> list;
 
    public Queue<Object[]> data;
 
    public TestMain() {
        list = new ArrayList<ImsiToAddress>();
    }
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        ConnectSqlite sqlite = new ConnectSqlite();
        Map<String, String> map = sqlite.selectTable();
         
        TestMain tm = new TestMain();
        for (String key : map.keySet()) {
            String addr = map.get(key);
            String addrEnc = tm.enDec(addr);
             
            if (addrEnc.equals("广东 深圳")) {
                System.out.println("addr  ++++++++++++++++++ " + addrEnc);
            }          
             
            System.out.println("addr " + addrEnc);
        }
         
//      ConnectOracle oracle = new ConnectOracle();
//      ResultSet rs = oracle.selectImsiToAddress("INFO_IMSITOADDRESSENC");
//
//      TestMain tm = new TestMain();
//      try {
//
//          while (rs.next()) {
//              String imsi = rs.getString("imsi");
//              String addrDec = rs.getString("address");
//
//              if (!imsi.equals("")) {
//                  // tm.decMap.put(imsi, addrDec);
//
//                  // 加密
//                  String encAddr = tm.enDec(addrDec);
//                  ImsiToAddress im = new ImsiToAddress();
//                  im.setImsi(imsi);
//                  im.setAddress(encAddr);
//                 
//                  tm.list.add(im);
//              }
//          }
//
//          ConnectSqlite sqlite = new ConnectSqlite();
//          sqlite.InsertWithTransaction(tm.list, "tb_CodeToAddress");
 
//      } catch (SQLException e) {
//          e.printStackTrace();
//      }
//      oracle.close();
 
    }
 
    /**
     * 解密位置信息
     *
     * @param addr
     *            位置信息
     * @return
     */
    private String enDec(String addr) {
        byte b_addr[];
        byte b_key[];
        try {
            b_addr = addr.getBytes("GBK");
            b_key = DECODE_KEYS_ZONE.getBytes("GBK");
 
            for (int i = 0; i < b_addr.length;) {
                for (int j = 0; j < b_key.length && i < b_addr.length; ++i, ++j) {
                    b_addr[i] ^= b_key[j];
                }
            }
 
            return new String(b_addr, "GBK");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
}

  

java杂记,布布扣,bubuko.com

java杂记

上一篇:C语言实现读取文件所有内容到字符串


下一篇:The import javax.servlet cannot be resolved