JAVA: Converting BigInteger to array of numbers - java

I have a biginteger
BigInteger b = new BigInteger("2389623956378561348065123807561278905618906");
And I need to print all its digits (2, 3, 8, and so on...). How can I do it?

Convert to char array, then decrease from each char the ASCII code of '0' char, to get digits between 0 and 9
char[] digits = b.toString().toCharArray();
for (char digit : digits) {
digit -= '0';
System.out.println((int)digit);
}
Note that if you just want to print, don't reduce the '0' ASCII value, and don't cast to int when printing

Already answered by Evan Knowles and user3322273, but here is another implementation:
byte[] digits = b.getBytes();
for (byte digit : digits) {
System.out.println (digit & 0xf);
}
What it does is that it masks the (ASCII) value of the digit. For example:
'0' = 48 | In ASCII and Unicode - Decimal
= 110000 | In binary
Hence, if we obtain the last four bits, then we can get the number. So
48 & 0xf (i.e. 15)
= 11 0000
& 00 1111
= 0000
= 0 | Ultimately in decimal.

Related

Decimal to Hex help understanding

import java.util.*;
public class Dec2Hex {
public static void main (String[] args){
Scanner input new Scanner (System.in);
System.out.print("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = "";
while (decimal != 0) {
int hexValue = decimal % 16;
char hexDigit = (0 <= hexValue && hexValue <= 9)?
(char)(hexValue + '0'): (char)(hexValue - 10 + 'A');
hex = hexDigit + hex;
decimal = decimal/16;
}
System.out.println("The hex number is " + hex);
}
}
This is an example in my book. I understand most of it but can't quite grasp a few parts. The example uses 1234 as the decimal entered.
The remainder of 1234/16 is 2 so hexValue is 2. Therefore hexDigit is 2 + '0'. But hexDigit is a character so what is the meaning of combining or adding 2 and '0'?
The final answer is 4D2. Where in the code did we ever provide instruction for any letter other than 'A'? How did it come up with a 'D'?
Thank you!
I understand now that the second part of the condition is takin a number like "13", subtracting 10 and adding that to 'A'. 3 + 'A' is 'D' in unicode.
I still do not understand adding the '0' to the first part of the condition. Couldn't we just say (char)(hexValue)? Why add '0'?
Thanks!
The remainder of 1234/16 is 2 so hexValue is 2. Therefore hexDigit is 2 + '0'. But hexDigit is a character so what is the meaning of combining or adding 2 and '0'?
The value 2 is an integer value, however the variable type is char. No problem in assigning such a value to a char variable, however this would result in the 0x02 character/byte, which is a control character, not a visible character to display. The digits in the ASCII table to display begins at 0x30 (which is the digit '0'). To get the character value to save in the char variable you add your calculated value 2 with the offset 0x30. To make this calculation easier you use the expression '0'. This is the exact value of 0x30 (or 48 in decimal).
The final answer is 4D2. Where in the code did we ever provide instruction for any letter other than 'A'? How did it come up with a 'D'?
The calculation is as follow:
1234 divided by 16 is 77 with a remainder of 2.
77 divided by 16 is 4 with a remainder of 13 (which is D).
4 divided by 16 is 0 with a remainder of 4.
This results in 4D2. To verify:
4*16*16 + 13*16 + 2 = 1234
Every char has a value number between 0 to 256, each of these numbers encode a printable sign.
For example the value of '0' is 48, and the value of 'A' is 65.
So when adding a constant offset to a character it's like going to that offset in that encoding.
For example '0' + 5 is '5', or 'A' + 3 is 'D'.
So as for your questions:
'0' + 2 is '2', i.e. the printable character of 2.
In the code hexValue had the value of 13, so hexDigit got the value (hexValue - 10 + 'A'), which is (hexValue - 10 + 'A') = 13 - 10 + 'A' = 'A' + 3 = 'D'.
2 + '0' = 2 + 48 = 50
(char)50 = '2'
Note that
'0' = 48
'1' = 49
'2' = 50
...
'9' = 57
Check ASCII Table to learn more about ASCII values.
Demo:
public class Main {
public static void main(String[] args) {
System.out.println((char) 50);
}
}
Output:
2
Ascii table is used for char type, so characters can be represented by decimal values. For example:
'0' in decimal 48
'1' in decimal 49
'2' in decimal 50
...
'9' in decimal 57
'A' in decimal 65
'B' in decimal 66
'C' in decimal 67
'D' in decimal 68
...
'Z' in decimal 90
'a' in decimal 97
'b' in decimal 98
...
'z' in decimal 122
Having that in mind your here are the answers to your questions:
We focus on the line
char hexDigit = (0 <= hexValue && hexValue <= 9)?
(char)(hexValue + '0'): (char)(hexValue - 10 + 'A');
The remainder of 1234/16 is 2 so hexValue is 2. Therefore hexDigit is
2 + '0'. But hexDigit is a character so what is the meaning of
combining or adding 2 and '0'?
For this question the part of interest is
(char)(hexValue + '0')
The value hexValue is 2, so hexValue + '0' is 50, because in ascii table '0' has decimal value 48. Than value of 50 is casted to char, which by ascii table is '2'.
The final answer is 4D2. Where in the code did we ever provide
instruction for any letter other than 'A'? How did it come up with a
'D'?
For this question the part of interest is
(char)(hexValue - 10 + 'A')
The value hexValue is 13. By asci table 'A' has value of 65 and 'D' has value of 68. So hexValue - 10 is 3 and when we sum 3 and 'A' we get 68. Finally we do cast to char and get 'D'.
You can easyly test the answers by executing this piece of code:
public class Main {
public static void main(String[] args) {
System.out.println("Decimal value: "+(2+'0')+" Char value:" +(char)(2+'0'));
System.out.println("Decimal value: "+(3 + 'A')+" Char value: "+ (char)(3+'A'));
}
}
Regards

Sum Of Digits BigInteger

I was trying to find the sum of digits of a BigInteger and found this code.
BigInteger big = BigInteger.valueOf(2).pow(1000);
String digits = big.toString();
int sum = 0;
for(int i = 0; i < digits.length(); i++) {
int digit = (int) (digits.charAt(i) - '0');
sum = sum + digit;
}
System.out.println(sum);
I don't understand why - '0' is added in 6th line of the code.
int digit = (int) (digits.charAt(i) - '0');
If I remove that part it gives me a wrong answer.
For example sum of digits of 16 without - '0' gives 103 as the answer instead of 7.
So, can anyone explain the importance of that part in the code?
Any help is appreciated.
The code digits.charAt(i) gives you the ASCII code for the digit. So if you look at an ASCII table, you'll see the value of the character 7 is 55 and the value of character 0 is 48. If you subtract 0 from 7, you'll get 7, which is the digit you're looking for.
For example if you take a '5' from digits.getCharAt(i):
'5' - '0' = ?
According to the ASCII table Can be translated to:
53 - 48
Which is 5 and it's the result you're looking for

How to decode a 9 digit integer to some random 4 digit word

How to encode a 7 digit integer to a 4 digit string In java?
I have a base36 decoder, which is generating 6 characters,
ex:230150206 is converted to 3T0X1A.
The code for it is as follows:
String f = "230150206";
int d = Integer.parseInt(f.toString());
StringBuffer b36num = new StringBuffer();
do {
b36num.insert(0,(base36(d%36)));
d = d/ 36;
} while (d > 36);
b36num.insert(0,(base36(d)));
System.out.println(b36num.toString());
}
/**
Take a number between 0 and 35 and return the character reprsenting
the number. 0 is 0, 1 is 1, 10 is A, 11 is B... 35 is Z
#param int the number to change to base36
#return Character resprenting number in base36
*/
private static Character base36 (int x) {
if (x == 10)
x = 48;
else if (x < 10)
x = x + 48;
else
x = x + 54;
return new Character((char)x);
}
Can some one share me some other way to achieve this?.
The obtained string can be made in to a substring, but i am looking any other way to do it.
Here is a method, in a simple test program. This method allows any String to represent the digits for the result. As the initial print shows, 62 digits should be sufficient to cover all 7 decimal digit numbers with no more than a 4 character output, so I recommend the decimal digits, lower case alpha and upper case alpha for the 7 digit case.
To cover 9 decimal digits in four encoded digits you would need at least 178 characters, which is not possible using only the 7-bit ASCII characters. You would have to decide which additional characters to use as digits.
public class Test {
public static void main(String[] args) {
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println(Math.pow(characters.length(), 4));
testit(230150206, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
testit(230150206, characters);
}
private static void testit(int num, String characters){
System.out.println(num + " "+compact(num, characters));
}
public static String compact(int num, String characters){
StringBuffer compacted = new StringBuffer();
while(num != 0){
compacted.insert(0, characters.charAt(num % characters.length()));
num /= characters.length();
}
return compacted.toString();
}
}
Output:
1.4776336E7
230150206 3T0X1A
230150206 fzGA6
All 7 digit numbers in base 10 can fit inside 24 bits (log2(9999999) < 24), that is 3 bytes. Ascii85 requires 20% of extra space to encode which will make it fit in 4 bytes.
Based on this answer by Mat Banik, you can do this:
public class Ascii85Test {
static int[] numbers = {
9999999,
490,
7910940,
};
public static void main(String[] args) {
for(int number : numbers) {
// Convert the number into 3 bytes
byte[] numberBytes = new byte[3];
numberBytes[0] = (byte) ((number >> 16) & 0xFF);
numberBytes[1] = (byte) ((number >> 8) & 0xFF);
numberBytes[2] = (byte) (number & 0xFF);
// Ascii85 encode the bytes
String encoded = Ascii85Coder.encodeBytesToAscii85(numberBytes);
// The encoded string will be "<4 characters>~\n", so we only need to keep the first 4 characters
encoded = Ascii85Coder.encodeBytesToAscii85(numberBytes).substring(0, 4);
// Decode them again, add the trailing ~ that we trimmed
byte[] decodedBytes = Ascii85Coder.decodeAscii85StringToBytes(encoded + "~");
// Convert the 3 bytes into a number
int decodedNumber = ((decodedBytes[0] << 16) & 0xFF0000)
| ((decodedBytes[1] << 8) & 0xFF00)
| (decodedBytes[2] & 0xFF);
System.out.printf("%s -> %s -> %s%n", number, encoded, decodedNumber);
}
}
}
Output:
9999999 -> R$N4 -> 9999999
490 -> !!2? -> 490
7910940 -> Gd\R -> 7910940
An int in Java can have a maximum of 10 digits (11 if you count the minus sign) and take up 4 bytes. With the 20% overhead of Ascii85 this means that we can encode any integer using 5 characters.

Only 7 bits for Java char?

I'm trying to make a UUencode algorithm and part of my code contains this:
for(int x = 0; x < my_chars.length; x++)
{
if((x+1) % 3 == 0)
{
char first = my_chars[x-2];
char second = my_chars[x-1];
char third = my_chars[x];
int first_binary = Integer.parseInt(Integer.toBinaryString(first));
int second_binary = Integer.parseInt(Integer.toBinaryString(second));
int third_binary = Integer.parseInt(Integer.toBinaryString(third));
int n = (((first << 8) | second) << 8) | third;
System.out.print(my_chars[x-2] + "" + my_chars[x-1] + my_chars[x] + Integer.toBinaryString(n));
}
}
System.out.println();
System.out.println(Integer.toBinaryString('s'));
What I'm trying to achieve is to combine those 8 bits from the chars that I get into a big 24 bits int. The problem I'm facing is that the result is a 23 bit int. Say my first 3 chars were:
'T' with a binary representation of 01010100
'u' with a binary representation of 01110101
'r' with a binary representation of 01110010
The result that I get from my program is a int formed from these bits:
10101000111010101110010
Which is missing the 0 at the beginning from the representation of 'T'.
Also I have included the last 2 lines of code because the binary string that I get from 's' is: 1110011 which is missing the 0 at the beginning.
I have checked if I scrolled by mistake to the right but it does not seem that I have done so.
The method Integer.toBinaryString() does not zero-pad the results on the left; you'll have to zero-pad it yourself.
This value is converted to a string of ASCII digits in binary (base 2)
with no extra leading 0s.

Character subtraction in String

Here is the code snippet i am trying to figure out whats happening
String line = "Hello";
for (int i = 0; i < line.length(); i++) {
char character = line.charAt(i);
int srcX = 0;
if (character == '.') {
}else{
srcX = (character - '0') * 20;
System.out.println("Character is " + (character - '0') +" " + srcX);
}
}
and executing that code will result to this
Character is 24 480
Character is 53 1060
Character is 60 1200
Character is 60 1200
Character is 63 1260
How a character minus the string which is "0" result in integer?? and where does the system base its answer to have 24,53,60,60,63?
You are allowed to subtract characters because char is an integer type.
The value of a character is the value of its codepoint (more or less, the details are tricky due to Unicode and UTF-16 and all that).
When you subtract the character '0' from another character, you are essentially subtracting 48, the code point of the character DIGIT ZERO.
So, for example, something like '5' - '0' would evaluate to 53 - 48 = 5. You commonly see this pattern when "converting" strings containing digits to numeric values. It is not common to subtract '0' from a character like 'H' (whose codepoint is 72), but it is possible and Java does not care. It simply treats characters like integers.
http://www.ascii.cl/
'0' is 48 in ascii
'H' is 72.
Therefore 72-48 gives you 24

Categories

Resources