Results 1 to 4 of 4

Thread: math class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    math class

    is there some method in the math class (or any class for that matter) that could calculate hexadecimal inputs?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    If you already know what the Hex input will look like you can just pass that as a String to the Byte.parseByte(String s, int radix) method. If you are working with integers and you are not sure of how to calculate numbering systems then you can just use the Integer.toHexString(int i) method.

    Code:
       public class Test3{
         public static void main(String[] args){
    
         int i = 30; 
         String hexstring = Integer.toHexString(i);
         System.out.println(hexstring); 
    
         byte b = Byte.parseByte(hexstring,16);
         System.out.println(b); 
       }
     }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    If you mean "calculating" by adding or doing some other mathematical procedure then you will probably have to go about that manually. I dont think that there are any functions in the API that take Hex values as arguements and calculates them.
    Code:
      public class Test3{
         public static void main(String[] args){
    
         int i = 330;
         int k = 70; 
    
         String hex1 = Integer.toHexString(i);
         String hex2 = Integer.toHexString(k);
         long l = calHex(hex1, hex2);     
         System.out.println(l);
      }
    
      public static long calHex(String hex1, String hex2){
       // byte b = Byte.parseByte(hex1,16); only values under 128!
          int i = Integer.parseInt(hex1,16);
          int k = Integer.parseInt(hex2,16);
          return i + k;  
      }
     }

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width