I need to write a converter in Java where it asks the user for an input, sourceAlphabet and targetAlphabet). The code should then provide an answer which has converted the sourceAlphabet to the targetAlphabet.
Alphabets should be given in the form:
-"0123456789" (Base 10),
-"abcdefghijklmnopqrstuvwxyz" (Alphabet),
-"0123456789ABCDEF" (Hexadecimal), etc.
Each alphabet value is a single unique ASCII character.
These are some sample inputs and answers I am looking for from the code:
convert("129","0123456789","01") === "10000001"
convert("FF","0123456789ABCDEF","0123456789") === "255"
convert("svip","abcdefghijklmnopqrstuvwxyz","0123456789ABCDEF") === "50C23"
Any help in getting me started on this problem would be greatly appreciated.
As a starting point, create a function that converts from decimal to hexadecimal.
The best tools to help you do this will be the modulo operator (x % y) and the division operator (x / y). Modulo (or mod) gives you the remainder, so if you imagine have a number like 24, 24 % 16 = 8, while 24 / 16 = 1. Notice that if I had 31 % 16, I would receive 15.
With those tools, you can operate on the input decimal and repeatedly mod by the base over the number to get the remainder, and then set the decimal equal to itself divided by the base.
For example.
Step 1.
Decimal : 31
String : ""
Base : 16
31 % 16 = 15
31 / 16 = 1
Step 2
Decimal : 1
String : "E"
Base : 16
1 % 16 = 1
1 / 16 = 1
Step 3
Decimal : 0
String :"1E"
Base : 16
Fin
Hope that helps.
Related
How can I make the Java code to calculate hexadecimal multiplication? (Just 8bit length.)
For example I have hexadecimal ab and 05, And F0 * 02 = fb (maybe).
But I don't know why the answer is fb.
Could you teach me how to make this with java code?
By hand, hexadecimal is base-16, so:
Hex Decimal
base 16 base 10
0 = 0
1 = 1
2 = 2
...
9 = 9
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
so the hexadecimal value AB is ((A = 10) * 16) + (B = 11)) or 160 + 11 == 171.
Bringing "8 bits" into the picture, a single byte (by convention these days, an octet) is made up of 8 bits. Eight bits is enough to represent 256 unique values, and in unsigned form represents values 0-255 decimal, or 0x00 to 0xFF hexadecimal. You may notice that this means that any 8-bit value can be represented by exactly two(2) hexadecimal digits.
In code, you can use 0x prefix to enter literals in hexadecimal notation.
int answer = 0xF0 * 0x02;
System.out.printf("%x%n", answer);
A Formatter (or similar, like System.out.printf) can convert a result back into hex easily enough. But if this is for a homework assignment, your instructor won't like this answer.
I'm trying to re-order some Excel columns using JExcel. I also need to find references to other cells and then re-map them to reference the correct cells. I feel like I've done a lot of the hard work, but I've hit a stumbling block.
I found this code on wikipedia, as linked to from SO:
public static String toBase26(int number){
number = Math.abs(number);
String converted = "";
// Repeatedly divide the number by 26 and convert the
// remainder into the appropriate letter.
do
{
int remainder = number % 26;
converted = (char)(remainder + 'A') + converted;
number = (number - remainder) / 26;
} while (number > 0);
return converted;
}
But when I run the number 35 into it, this is what happens:
number = 35
remainder = 9
converted= char(9+'A')+"" = J
number = (35-9)/26 = 1
1>0
remainder = 1
char(1+'A') = B
converted= char(1+'A')+"J" = BJ
Which is, in a way expected, as Base 10 (35) = Base 26 (19). But I'm actually wanting to refer to column AJ.
I can't untangle what change I need to make to get the right letters out. Whenever I try to work it out on paper, I end up ruining the previous letters extracted. For instance, I don't think this would work, as it means I end up with remainder as 8, the first time, and then that would be converted into I, unless I've missed something?
Any help on this would be greatly appreciated. I've looked around and wasted enough time on this. I just want some help to get it to work.
The stumbling block behind this 'hexavidecimal system' is that it has a 0, but the units column skips the 0 and ranges only from A-Z. Consider the following conversion from decimal:
A 1 (0*26 + 1)
...
Z 26 (0*26 + 26)
AA 27 (1*26 + 1)
...
AZ 52 (1*26 + 26)
BA 53 (2*26 + 1)
...
BZ 78 (2*26 + 26)
CA 79 (3*26 + 1)
...
ZZ 702 (26*26 + 26)
AAA 703 (1*26*26 + 1*26 + 1)
See the problem? There are missing 'zeroes' in the hexavidecimal numbers:
00A 1
...
00Z 26
0AA 27
...
0AZ 52
0BA 53
...
0BZ 78
0CA 79
...
0ZZ 702 (26*26 + 26)
AAA 703 (1*26*26 + 1*26 + 1)
However, the units column does NOT have the zeroes ever!
Obviously we do not print these zeroes, but it should aid your understanding of what is going wrong.
Here's our algorithm. I wrote the algorithm under the assumption that decimal 0 = hexavidecimal A, 1 -> B, 25 -> Z, 26 -> AA and so on because it makes it easier for me to wrap my head around. If this isn't the assumption you want just subtract 1 before running the code :)
0. If number =< 0, return.
1. Modulo by 26. Convert 0-25 to 'A'-'Z'. //This is our units column.
Loop {
2. Divide the number by 26 (integer division rounding down).
3. If number =< 0, return.
4. Modulo by 26. Convert 0-25 to 'Z','A'-'Y'. //This is our next column (prepend to string output).
}
Example
Converting decimal 730 -> ABC hexavigesimal
Modulo of 730 by 26 = 2 -> 'C' for units column
Divide 730 by 26 = 28
Modulo 28 by 26 = 2 -> 'B' for tens column
Divide 28 by 26 = 1
Modulo 1 by 26 = 1 -> 'A' for hundreds column
Divide 1 by 26 = 0
Number is empty, therefore return 'ABC'
Here is a simple Python function to compute the hexavigesimal representation of a number (in an arbitrary base), where a is equal to 1 (not 0).
The tricky part of the problem is that at each step you're taking between 1 and 10 off the remainder, so you need to account for that in your modulo. The code below accounts for it by subtracting 1 from the number each time. Then 0 becomes a very convenient end condition, because you cannot represent 0 in hexavigesimal (the wikipedia entry denotes it λ).
# Formats a number as a bijective base N string.
def bijective(n, base):
chars = ''
while n != 0:
chars = chr((n - 1) % base + 97) + chars
n = (n - 1) / base
return chars
# Examples!
if __name__ == '__main__':
base = 26
for n in range(1, 2 * base * base):
print('{}: {}'.format(n, bijective(n, base)))
See it in action on pythonanywhere.
I included a javascript version in this gist.
I have this code, it results '2'. I googled and searched books but found no good answer that helped me.
int i = 2;
int j = 3;
int x = i & j;
System.out.println (x);
Can anyone can explain.
Here, '&' is the bitwise and operator. Bits are set in the result only if they are set in both of the operands:
Here's the operation:
2: 00000010
& 3: 00000011
-----------
2: 00000010
Of course here, ints are 32 bits, and I only showed the last 8, but the first 24 are all zeroes for these numbers anyway.
It's a bitwise operator.
2 in binary is '10'. 3 in binary is '11'. The bitwise & operator compares them like so:
10
&11
--
10
For each column where both numbers are 1, it will return 1. In this case, the result is '10', which as an int is equal to 2.
Take these examples:
System.out.println(2 & 3);
System.out.println(3 & 4);
System.out.println(8 & 4);
System.out.println(9 & 5);
System.out.println(11 & 7);
// binary representation of above operations
System.out.println(0b10 & 0b11);
System.out.println(0b11 & 0b100);
System.out.println(0b1000 & 0b100);
System.out.println(0b1001 & 0b101);
System.out.println(0b1011 & 0b111);
They result in the output of:
2
0
0
1
3
2
0
0
1
3
Then go back and look at the binary representations and notice how the resulting answers are the Bitwise AND of the numbers (and not the Logical AND &&)
& is a bitwise operator that works as follows:
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
and hence 2 & 3 = 2:
2 ==> 00000000000000000000000000000010
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3 ==> 00000000000000000000000000000011
---------
2 ==> 00000000000000000000000000000010
& is the bitwise AND
&& is the logical AND.
2 & 3 delivers correctly 2.
Expressed in binary representation:
10
11
--
10
I am new to Java, actually programming in general. I understand that the modulus operator (%) returns the remainder of two numbers, however, I do not understand why 17 % 40 = 17.
I understand that 40 % 17 = 6, and 17 % 5 = 2, and 40 % 5 = 0. I get the gist of what value is returned as the remainder. But 17 % 40 = 17 has me stumped.
The only rationalization I can devise is that since the remainder is less than 1 the total value 17 is returned, why not 0? Please help to explain this enigma to me.
When you divide 17/40, quotient is 0 and the remainder is 17.
The modulo operator (%) returns the remainder.
i.e
a % b = remainder of a / b
Equation from Wiki by Knuth:
a = 17
n = 40
floor(a/n) = 0
so r = 17
When n > a then r is simply a.
i guess learning back the 3rd and 4th standard maths is the key point.
if u see (hope understand the division syntax. its the popular 3rd std way )
____
40)17
you will get a reminder 17 as 17 is not divisible by 40.
then there will be an adition of '.' and then the fraction will be added
If you have the numbers a and b, their quotient q and remainder r, then the following has to be true:
q · b + r = a
That is, if you multiply the quotient (q) by the divisor (b) and add the remainder (r), the result is the dividend (a).
In your case a = 17, b = 40, q = 0 and so r has to be 17.
Note: the equation above is just a rearrangement of the equation from Nikolay Kuznetsov's answer, but I think it's easier to understand this way.
Maybe this is a different and more helpful way to think about it.
When we apply division to integer numbers a and b, we are really trying to relate a and b like this:
a = Q * b + R
a is some multiple of b, plus some leftover. Q and R are integers; to keep this simple, let's also just think of non-negative numbers. The multiple, Q, is the quotient and leftover, R, is the remainder -- the smallest ones that make this relation work.
In most languages, a / b gives you Q, and and a % b gives you R. (In fact processors tend to compute both at once -- these are so related.)
So if a is 17 and b is 40, it only works if you write:
17 = 0 * 40 + 17
This is why a % b must be 17.
(Note that it gets more complex when considering negative numbers.)
Given the number n (2 <= n <= 1000), find the lowest nonzero multiple of which is written in base 10 with digits 0 and 1 only. Examples: 2 -> 10, 3 -> 111, 4 -> 100, 7 -> 1001, 11 -> 11, 9 -> 111 111 111.
My idea is not very good:
{/* n|2 and n|5 +"000"(max for apparition(2,5)) ->
n|3 + "111 " */}
I think, follow the remaining division of numbers consist of numbers n which is formatted 0/1.
Thanks for your help!
You can use a breadth first search. Start by enqueing 1, since your number must start with a 1, then each time you extract a number x from your queue, see if it's a multiple of n or not. If yes, you have your answer, if not insert x * 10 and x * 10 + 1 in the queue (in that order).
Note that you do not actually have to store the entire strings of 1s and 0s in your queue: it's enough to store the remainder of division by n and some auxiliary information that lets you reconstruct the actual string. Write back if you need more details about this.
The non-bruteforce approach would be to iterate throught the series of numbers that contain only 0 and 1 then figure out if the number is a multiple of the number in question. This approach will be substantially more efficient than iterating through the multiples of n and determining if it contains only 0 and 1.
IVlad's suggestion is the more efficient way to produce the series (numbers that contain only 0 and 1). However, if you prefer to generate the numbers on-the-fly (no memory overheads of the queue) you can simply iterate through the integers (or use your loop index) and for each value interpret its binary representation as a decimal number.
2 (Decimal) -> 10 (Binary) -> (interpret as decimal 10)
3 (Decimal) -> 11 (Binary) -> (interpret as decimal 11)
4 (Decimal) -> 100 (Binary) -> (interpret as decimal 100)
5 (Decimal) -> 101 (Binary) -> (interpret as decimal 101)
... and so on.
For the conversion, I suspect it can be done by chaining calls to Integer.toBinaryString() and String.parseInt() but there may well be more efficient ways to do that.
Here's an online demo to get you started: http://jsfiddle.net/6j5De/4/
public static int result(int num)
{
int i =2;
while(true)
{
int mult = Integer.parseInt(Integer.toString(i++,2));
if( mult % num == 0) //Check whether it is a multipler of given number or not ?
{
return mult;
}
}
}