473,544 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

16 New Member
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 9381
Ganon11
3,652 Recognized Expert Specialist
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 Recognized Expert MVP
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 New Member
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 New Member
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 Recognized Expert Specialist
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 Recognized Expert Expert
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 Recognized Expert MVP
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
2627
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
5141
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 calculate the totals in each row. when i try however, i receive the error: "Error: 'elements' is null or not an object"
5
11939
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 SizeOfArray(int a) { return (sizeof(a)/sizeof(a)); } main() { int a={1,2,3}; printf("%d\n",SizeOfArray(a));
6
3619
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,'@','@','@', '@','_','@','@','@','@','@','@','@','@','@', 32,'@','@','@','@', ' ','!','"','#','@','%','&', 39,'(',')','*','+',',','-','.','/',...
8
3436
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 I access the array elements. Thanks for your help. #include <stdio.h>
14
16201
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 provide me with small sample code for that.
14
14214
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: 0xffffff81 Octal: O37777777601 Binary: 1098 7654 3210 9876 5432 1098 7654 3210
6
16887
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
9748
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
0
7438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7374
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7392
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7720
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1850
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.