471,852 Members | 913 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,852 software developers and data experts.

Calculating the sum of an array with decimal & hexadecimal elements.

16
I need to add the sum of hex and decimal
right now is not adding anything

A5
165
A5
165A50
FAA
4010
FAA
4010FAA0
Here is my code
Expand|Select|Wrap|Line Numbers
  1. public class Numbers{
  2.  
  3.     public static int Numbers(String s) {
  4.         String digits = "0123456789ABCDEF";
  5.         s = s.toUpperCase();
  6.         int val = 0;
  7.         for (int i = 0; i < s.length(); i++) {
  8.             char c = s.charAt(i);
  9.             int d = digits.indexOf(c);
  10.             val = 16 * val + d;
  11.         }
  12.         return val;
  13.     }
  14.  
  15.    public static String Numbers(int d) {
  16.         String digits = "0123456789ABCDEF";
  17.         if (d == 0) return "0";
  18.         String hex = "";
  19.         while (d > 0) {
  20.             int digit = d % 16;                
  21.             hex = digits.charAt(digit) + hex;  
  22.             d = d / 16;
  23.         }
  24.         return hex;
  25.     }
  26.  
  27.  
  28.  
  29.     public static void main(String[] args) {
  30.       int sum = 0;
  31.  
  32.       for (int i = 0; i < args.length; i++) {
  33.         System.out.println(args[i]);
  34.  
  35.       int decimal = Numbers(args[i]);
  36.         System.out.println(decimal );
  37.  
  38.         String hex = Numbers(decimal);
  39.         System.out.println(hex);
  40.  
  41.         System.out.println( decimal + hex + sum );
  42.     }
  43. }
  44. }
  45.  
Oct 5 '07 #1
7 9094
Ganon11
3,652 Expert 2GB
Well, you haven't added a method to add anything. Think about how you'll do this: especially of concern is, how will you tell if a number is base 16 (hex) or base 10 (decimal)?
Oct 5 '07 #2
JosAH
11,448 Expert 8TB
I need to add the sum of hex and decimal
right now is not adding anything
It's ambiguous: does 42 the decimal number 42 or the hexadecimal representation
of the numer 66 (in decimal)?

kind regards,

Jos
Oct 5 '07 #3
Jromero
16
It's ambiguous: does 42 the decimal number 42 or the hexadecimal representation
of the numer 66 (in decimal)?

kind regards,

Jos
Here is my code
Expand|Select|Wrap|Line Numbers
  1. //LabAssignment2
  2. //Jessica Romero -10-05-07
  3. public class Numbers{
  4.  
  5.     public static int conv1(String s) {
  6.         String digits = "0123456789ABCDEF";
  7.         s = s.toUpperCase();
  8.         int val = 0;
  9.         for (int i = 0; i < s.length(); i++) {
  10.             char c = s.charAt(i);
  11.             int d = digits.indexOf(c);
  12.             val += d*Math.pow(16, s.length()-1-i);
  13.         }
  14.         return val;
  15.     }
  16.  
  17.    public static String conv2(int d) {
  18.         String digits = "0123456789ABCDEF";
  19.         if (d == 0) return "0";
  20.         String hex = "";
  21.         while (d > 0) {
  22.             int digit = d % 16;                
  23.             hex = digits.charAt(digit) + hex;  
  24.             d = d / 16;
  25.         }
  26.         return hex;
  27.     }
  28.  
  29.  
  30.  
  31.     public static void main(String[] args) {
  32.       int sum = 0;
  33.  
  34.       for (int i = 0; i < args.length; i++) {
  35.         System.out.println(args[i]);
  36.  
  37.  
  38.       int decimal = conv1(args[i]);
  39.         System.out.println(decimal );
  40.  
  41.         String hex = conv2(decimal);
  42.         System.out.println(hex);
  43.  
  44.  
  45.  
  46.     }
  47. }
  48. }
Oct 5 '07 #4
Jromero
16
Well, you haven't added a method to add anything. Think about how you'll do this: especially of concern is, how will you tell if a number is base 16 (hex) or base 10 (decimal)?
I have hard time with arrays can you please help me
Expand|Select|Wrap|Line Numbers
  1.  public static int conv1(String s) {
  2.         String digits = "0123456789ABCDEF";
  3.         s = s.toUpperCase();
  4.         int val = 0;
  5.         for (int i = 0; i < s.length(); i++) {
  6.             char c = s.charAt(i);
  7.             int d = digits.indexOf(c);
  8.             val += d*Math.pow(16, s.length()-1-i);
Oct 5 '07 #5
Ganon11
3,652 Expert 2GB
Yes, we can help you, but you haven't done anything to even try to add. Please take a look at our posting guidelines (click Help at the top of your screen, click Posting Guidelines, and read through all of them).
Oct 5 '07 #6
pbmods
5,821 Expert 4TB
Heya, Jessica.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Please use CODE tags when posting source code:

[CODE=java]
Java code goes here.
[/CODE]
Oct 5 '07 #7
JosAH
11,448 Expert 8TB
Heya, Jessica.

Changed thread title to better describe the problem
If that is the correct topic title then all I can say is: read my reply #3 again.

kind regards,

Jos
Oct 6 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by serpent17 | last post: by
5 posts views Thread by sugaray | last post: by
6 posts views Thread by Anjali | last post: by
14 posts views Thread by me2 | last post: by
6 posts views Thread by sweeet_addiction16 | last post: by
23 posts views Thread by neha_chhatre | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.