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 -
public class Numbers{
-
-
public static int Numbers(String s) {
-
String digits = "0123456789ABCDEF";
-
s = s.toUpperCase();
-
int val = 0;
-
for (int i = 0; i < s.length(); i++) {
-
char c = s.charAt(i);
-
int d = digits.indexOf(c);
-
val = 16 * val + d;
-
}
-
return val;
-
}
-
-
public static String Numbers(int d) {
-
String digits = "0123456789ABCDEF";
-
if (d == 0) return "0";
-
String hex = "";
-
while (d > 0) {
-
int digit = d % 16;
-
hex = digits.charAt(digit) + hex;
-
d = d / 16;
-
}
-
return hex;
-
}
-
-
-
-
public static void main(String[] args) {
-
int sum = 0;
-
-
for (int i = 0; i < args.length; i++) {
-
System.out.println(args[i]);
-
-
int decimal = Numbers(args[i]);
-
System.out.println(decimal );
-
-
String hex = Numbers(decimal);
-
System.out.println(hex);
-
-
System.out.println( decimal + hex + sum );
-
}
-
}
-
}
-
7 9361
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 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
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 -
//LabAssignment2
-
//Jessica Romero -10-05-07
-
public class Numbers{
-
-
public static int conv1(String s) {
-
String digits = "0123456789ABCDEF";
-
s = s.toUpperCase();
-
int val = 0;
-
for (int i = 0; i < s.length(); i++) {
-
char c = s.charAt(i);
-
int d = digits.indexOf(c);
-
val += d*Math.pow(16, s.length()-1-i);
-
}
-
return val;
-
}
-
-
public static String conv2(int d) {
-
String digits = "0123456789ABCDEF";
-
if (d == 0) return "0";
-
String hex = "";
-
while (d > 0) {
-
int digit = d % 16;
-
hex = digits.charAt(digit) + hex;
-
d = d / 16;
-
}
-
return hex;
-
}
-
-
-
-
public static void main(String[] args) {
-
int sum = 0;
-
-
for (int i = 0; i < args.length; i++) {
-
System.out.println(args[i]);
-
-
-
int decimal = conv1(args[i]);
-
System.out.println(decimal );
-
-
String hex = conv2(decimal);
-
System.out.println(hex);
-
-
-
-
}
-
}
-
}
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 -
public static int conv1(String s) {
-
String digits = "0123456789ABCDEF";
-
s = s.toUpperCase();
-
int val = 0;
-
for (int i = 0; i < s.length(); i++) {
-
char c = s.charAt(i);
-
int d = digits.indexOf(c);
-
val += d*Math.pow(16, s.length()-1-i);
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).
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]
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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')
|
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...
|
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...
|
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,'@','@','@',...
|
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...
|
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...
|
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: ...
|
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.
|
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
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |