[英]Add integer in 27 char hex string in java
我有一個十六進制字符串240300000800000000000000004。我想在這個十六進制代碼中添加整數並在java中獲取十六進制代碼。
由於java不支持這個muck長度的值,所以我們如何做到這一點?
謝謝Abhishek
String str = "240300000800000000000000004";
BigInteger bi = new BigInteger(str, 16);
Integer ii = 10; // Integer to add
bi = bi.add(new BigInteger(ii.toString()));
System.out.println(bi.toString(16)); // 24030000080000000000000000e
最簡單的解決方案是已經發布的使用BigInteger的解決方案。 但是,您也可以在塊中處理添加,以便結果和任何進位都適合長。 從右端開始,帶有進位零。 取一組最多15個十六進制數字,轉換為long,添加包括從該塊右邊添加的任何進位,將結果的15個最低有效數字存儲到輸出字符串中,並記住最多從這個加法中用作進位的有效數字,左邊的下一個加法的進位。
BigInteger b = new BigInteger("FF", 16);
BigInteger bPlus1 = b.plus(1);
String hex = bPlus1.toString(16); // 0x100
BigInteger bigint = new BigInteger("240300000800000000000000004" ,16);
System.out.println(bigint);
BigInteger s = new BigInteger("8");
bigint = bigint.add(s);
System.out.println(bigint);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.