2014-05-13 45 views
-1

我有一些自動生成的ids表示爲HEX字符串。我想找到接下來的1000個值。例如,假設我有以下字符串爪哇十六進制數增加1

String keyFrom = "536a11dae4b062cab536549d"; 

如何從java代碼中獲取以下代碼,並將其轉換爲字符串?

536a11dae4b062cab536549e 
536a11dae4b062cab536549f 
536a11dae4b062cab53654a0 
536a11dae4b062cab53654a1 
536a11dae4b062cab53654a2 ... etc. 

回答

0

將您StringBigInteger並加:

BigInteger bigInt = new BigInteger(hexString, 16); 
for(int i = 0 ; i < 1000 ; ++i) { 
    // do something with bigInt... 
    System.out.println(bigInt.toString(16)); 
    bigInt = bigInt.add(BigInteger.ONE); 
} 
+0

挑剔:將q要求下一個1000個值;你打印原始值和下一個999. –

+0

哈哈是的。那麼他可能不得不自己糾正:) –

2

使用的BigInteger如下

BigInteger decimal = new BigInteger("536a11dae4b062cab536549d",16); 
     for (int i=0;i<1000;i++){ 
      decimal = decimal.add(BigInteger.ONE); 
      System.out.println(decimal.toString(16)); 
     } 
0

編輯:如果您使用十六進制字符串長度超過〜8個字符,使用該解決方案使用上面的BigInteger。

使用Integer#parseInt(String,16)將十六進制字符串解析爲一個整數,向其中添加一個,然後使用Integer#toHexString將其重新設置爲十六進制。

String hexString = "A953CF"; 
// 16 sepcifies the string to be in base 16, hexadecimal 
int hexAsInt = Integer.parseInt(hexString, 16); 
hexAsInt += 6; // Add 6 
String newHexString = Integer.toHexString(hexAsInt); 
System.out.println(newHexString); 

--> A953D4