Reputation: 1273
I'm creating a big integer class. I read in two longs as function arguments and what I want to do is have each number in the long read in to occupy one index of an array.
I've done this by converting the long into a string then char array and storing them somewhere temporarily. This works fine and when printed out prints out exactly like the number read in. Now what I want to do is add them now to a new array and have their data type as long
. I have made two arrays to deal with this.
Problem is when I try cast the char to a long, it gives me a completely different value. It seems that it's converting the char into it's own respective number? Character class doesn't seem to have any way of converting to a long.
What's the best way to do this?
EDIT: It seems that if I change the long arrays to int arrays and then use Character.getNumericValue(char ch) it works adding it to the array properly.
Since I'm planning to be returning a long at the end of this function should I make sure that those arrays are long for safety? Or storing them as ints in the array is fine? Thanks
public static long hugeMultiplication(long value1, long value2){
System.out.println("originalvalue: "+value1);
int lengthOfWordOne = String.valueOf(value1).length();
int lengthOfWordTwo = String.valueOf(value1).length();
System.out.println("length1: "+lengthOfWordOne);
System.out.println("length2: "+lengthOfWordTwo);
long[] numberOne = new long[lengthOfWordOne+1];
long[] numberTwo = new long[lengthOfWordTwo+1];
//make those longs into string to convert char array
char[] tempValueOne = String.valueOf(value1).toCharArray();
char[] tempValueTwo = String.valueOf(value2).toCharArray();
//copy each value of a char array to long array and change to long again
//this will set up the array having each number in it so we can do the multiplication
for (int i = 0; i < tempValueOne.length; i++){
numberOne[i] = (long) tempValueOne[i];
}
for (int i = 0; i < numberOne.length; i++){
System.out.print(numberOne[i]);
}
Upvotes: 3
Views: 23612
Reputation: 4810
The only real difference between a char
and a long
is the data size. A char
is typically 1 byte, while a long
is typically 8 bytes. When you convert a long
to a string
, you are creating an array of ASCII characters that would represent this number. For example, the long
12345 would be represented in memory as:
00000000 00000000 00000000 00000000 00000000 00000000 00110000 00111001
But when converted to a string
(an array of 1-byte char
s), it would look more like this:
00110001 00110010 00110011 00110100 00110101
Where each byte is a character in the string. The first byte being an ASCII '1', '2', etc.
When you cast each char
back to a long
, you are actually getting the ASCII value of each character in the string. So the char
'1' is actually represented in memory as an integer 49, '2' is '50', and so on. What you will need to do is convert the string as a whole back to the integer representation.
Upvotes: 6
Reputation: 880
long one = Character.getNumericValue('1');
http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getNumericValue%28char%29
Upvotes: 9