473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 9361
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

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

Similar topics

6
by: serpent17 | last post by:
Hello, I was looking at this: http://docs.python.org/lib/module-struct.html and tried the following >>> import struct >>> struct.calcsize('h') 2 >>> struct.calcsize('b')
25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
6
by: Anjali | last post by:
Hi, I am handling an array with a hexadecimal index for the first time. What actually does the below means. arr = { '@','£','$','@','@','@','@','@','@','@', 10,'@', 13,'@','@','@',...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
14
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to...
14
by: me2 | last post by:
I am writing a little base conversion utility called base.c. This is what base does. $ base -127 Signed decimal: -127 Unsigned decimal: 4294967169 Hexidecimal: ...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.