[英]Java Hex number increase by one
我有一些自動生成的ID,表示為十六進制字符串。 我想找到下一個1000個值。 例如,假設我有以下字符串
String keyFrom = "536a11dae4b062cab536549d";
如何從以下Java代碼獲取String?
536a11dae4b062cab536549e
536a11dae4b062cab536549f
536a11dae4b062cab53654a0
536a11dae4b062cab53654a1
536a11dae4b062cab53654a2 ... etc.
如下使用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));
}
將您的String
轉換為BigInteger
並將其遞增:
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);
}
編輯:如果您使用的字符串長度超過〜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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.