Advertisement
supremeXD

Untitled

Sep 14th, 2021
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. public class SumLongHex {
  2.     public static void main(String[] args) {
  3.         long result = 0;
  4.  
  5.         for (int h = 0; h < args.length; h++) {
  6.             String curString = args[h];
  7.             String newString = "";
  8.             boolean saw_0x = false;
  9.  
  10.             for (int i = 0; i < curString.length(); i++) {
  11.                 if(i < curString.length() - 1 && curString.charAt(i) == '0' && (curString.charAt(i + 1) == 'x' || curString.charAt(i + 1) == 'X')) {
  12.                     saw_0x = true;
  13.                     i++;
  14.                     continue;
  15.                 }
  16.                 while(i < curString.length() && !Character.isWhitespace(curString.charAt(i))) {
  17.                     newString += curString.charAt(i);
  18.                     i++;
  19.                 }
  20.                 try {
  21.                     if(saw_0x == true) {
  22.                         result += Long.parseUnsignedLong(newString, 16);
  23.                     } else {
  24.                         result += Long.valueOf(newString);
  25.                     }
  26.                 } catch (NumberFormatException ex) {}
  27.                 newString = "";
  28.                 saw_0x = false;
  29.             }
  30.         }
  31.  
  32.         System.out.println(result);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement