-1

I'm adding two hexadecimal numbers and return them as a String, How can i store the String value into int? it's not working with me. here's the code I'm working on.

the int A and B are not taking the String of num1 and num2 as int, how can i do it?

 public static String Addtion(String num1, String num2){
  int sum, carry=0;
  int A = num1.length()-1;
  int B = num2.length()-1;
  String astn="";
  if ( B <  A && B!= A){
      num2 = '0' + num2;
      B++;
  }

   while(B>=0){  
      sum = (con(num1.charAt(A))+con(num2.charAt(B))+carry)%16;
      carry= (con(num1.charAt(A))+con(num2.charAt(B))+carry)/16;
      astn = toHexChar(sum) + astn;
   A--;
   B--;
 }
  return astn ;
}
  • 1
    @redFIVE - By my reading of this post, that other question is not close to being a duplicate. Despite the poorly worded title of this post, OP's problem here seems to have nothing to do with converting a `String` to an `int`. – Ted Hopp Dec 21 '15 at 20:04
  • he clearly says in his "question" `How can i store the String value into int?` – user1231232141214124 Dec 21 '15 at 20:07
  • @redFIVE - I know, but look at the code. OP is trying to simulate hex addition character-by-character. The `A` and `B` are actually the _lengths_ of the hex strings (minus 1), not the parsed hex values. The entire algorithm would not make sense otherwise. OP's problem is due to not dealing with those lengths correctly (as per my answer). OP is clearly not a native English speaker and some allowances should be made to that. (Note that for all we know, these could be 10,000-digit-long hext strings that couldn't possibly be parsed into native integer types in Java.) – Ted Hopp Dec 21 '15 at 21:02
  • @TedHopp Its not our job to deduce that. Until the title and question are changed its still a duplicate for all those people who are going to search for a similar problem and get this garbage thread – user1231232141214124 Dec 21 '15 at 21:08
  • @redFIVE - If the thread's garbage, then, fine, downvote the question, or ask OP in a comment to clarify and/or vote to close as "unclear what you're asking" or some other appropriate reason. But it's a disservice to the community to point to an irrelevant thread as being a duplicate based on a couple of keyword matches. – Ted Hopp Dec 21 '15 at 21:34

2 Answers2

0

You aren't dealing with length differences correctly. First, you need to change the if to a while, since the length difference may be more than 1. Second, your code only checks whether num2 is shorter than num1 and you need to do the reverse as well. Finally, you are ignoring the possibility of a non-zero carry after the addition loop ends. This might do the trick:

public static String Addtion(String num1, String num2){
    int sum, carry=0;
    int A = num1.length()-1;
    int B = num2.length()-1;
    String astn="";
    while (A < B) {
        num1 = "0" + num1;
        A++;
    }
    while (B <  A) {
        num2 = "0" + num2;
        B++;
    }

    while(B>=0){  
        sum = (con(num1.charAt(A))+con(num2.charAt(B))+carry)%16;
        carry= (con(num1.charAt(A))+con(num2.charAt(B))+carry)/16;
        astn = toHexChar(sum) + astn;
        A--;
        B--;
    }
    if (carry > 0) {
        astn = toHexChar(carry) + astn;
    }
    return astn ;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
-1

If your input is a String representation of a base-16 number and you need it converted to a (base-10) int, then use:

Integer.parseInt(hex, 16);
Keith
  • 3,079
  • 2
  • 17
  • 26