I have looked at a lot of post here on stackoverflow concerning this problem, I have found partial solutions but so far I have yet to find a solution that works for me.
new BigDecimal("3324679375210329505").toString(2);
Seems to work best for me (from: Convert a large 2^63 decimal to binary) but I do need leading and trailing zeros. Is there anyway I can convert a large (bigger than a long) hex to a binary (String) representation?
Thanks in advance.
The BigInteger class handles arbitrarily large numbers.
Trailing zeroes are allready handled. To handle leading zeroes, simply add a "1" at the front, which ensures a leading "1" bit, then strip it back off again:
String bits = new BigInteger("1" + hex, 16).toStting(2).substring(1);
You need leading zeroes? I don't know of a built-in function, but you can easily implement it yourself:
public static String hex2binary(String hex) {
StringBuilder result = new StringBuilder(hex.length() * 4);
for (char c : hex.toUpperCase().toCharArray()) {
switch (c) {
case '0': result.append("0000"); break;
case '1': result.append("0001"); break;
case '2': result.append("0010"); break;
case '3': result.append("0011"); break;
case '4': result.append("0100"); break;
case '5': result.append("0101"); break;
case '6': result.append("0110"); break;
case '7': result.append("0111"); break;
case '8': result.append("1000"); break;
case '9': result.append("1001"); break;
case 'A': result.append("1010"); break;
case 'B': result.append("1011"); break;
case 'C': result.append("1100"); break;
case 'D': result.append("1101"); break;
case 'E': result.append("1110"); break;
case 'F': result.append("1111"); break;
default: throw new IllegalArgumentException("Invalid hex: '" + hex + "'");
}
}
return result.toString();
}
Related
private void Button_CalculateActionPerformed(java.awt.event.ActionEvent evt) {
try{ a=Double.parseDouble(TextField_First.getText());
b=Double.parseDouble(TextField_Second.getText());
switch(operation){
case 1:result=a+b;
break;
case 2:result=a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
break;
case 5:result=a%b;
break;
case 6:result=Math.pow(a,b);
break;
case 7:
TextField_Second.setText(""+a);
result=Math.sqrt(a);
TextField_Second.setText(""+a);
break;
case 8:result=Math.abs(a);
default:System.out.println("Please select an operation");
}
TextField_Result.setText(""+result);
}catch(NumberFormatException e){Label_error.setText("Please use your
common sense ;-;");}
}
What I'm trying to do is set the value (and text) of TextField_Second to 0.0, if TextField_Second is null/has no value. So if TextField_First's value is 9, I get the square root, I get an error (NumberFormatException), but I used try and catch, so if that does happen, a label will appear saying, "Please use your common sense ;-;".
P.S. It only works if TextField_Second has a value, and it doesn't affect the answer.
Try something like this;
Note: you have not add break statement to case 8:
try
{
if (!TextField_First.getText().equals(""))
{
if(TextField_Second.getText().equals(""))
{
TextField_Second.setText("0.0");
}
a=Double.parseDouble(TextField_First.getText());
b=Double.parseDouble(TextField_Second.getText());
switch(operation){
case 1:result=a+b;
break;
case 2:result=a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
break;
case 5:result=a%b;
break;
case 6:result=Math.pow(a,b);
break;
case 7:
TextField_Second.setText(""+a);
result=Math.sqrt(a);
TextField_Second.setText(""+a);
break;
case 8:result=Math.abs(a);
break;
default:System.out.println("Please select an operation");
break;
}
}
In a switch(string) can i make a case to start with a specific char f.e case('N') and then followed by other characters that i don't care about them? f.e NOC
case("N**"):
case("1**"):
case("0**"):
BalancedTrenary term = new BalancedTrenary(parts[i]);
You can use charAt and use char in the cases
String str = "NOC";
switch (str.charAt(0)) {
case 'N':
// do something here
break;
}
Be aware that the case is case sensitive. To make it ignored case use another case with n
switch (str.charAt(0)) {
case 'N':
case 'n':
// do something here
break;
}
I just run over something strange in my java code:
switch (result) {
case 0:
result_amount = 500;
case 1:
result_amount = 600;
case -1:
result_amount = 700;
}
result is from primitive type int.
For value 1 case 1 and case -1 are executed.
Is this a normal switch case behaviour? If yes: why?
You need to use the break keyword after a case block:
switch (result) {
case 0:
result_amount = all_amounts[i];
break;
case 1:
result_amount = all_amounts[i];
break;
case -1:
result_amount = all_amounts[i+1];
}
The switch statement will make a jump to the correct case tag, then execute all the code that follow, ignoring potential other case tags. You can consider the switch statement just like a goto one.
Fall Through.
quoting from docs
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
You have to add the break after each case.
case 1:
result_amount = 600;
break;
Remember:
This is a mistake that almost every beginner will make. That's why I like C# more. It doesn't allow "fall-through".
What you did wrong is you fell-through the switch statement. Try using 0 as the value of result. It will go through all the cases. When a switch case finishes execution, the next case gets executed. That's why we need to add a break; statement for each case of the switch statement.
switch (result) {
case 0:
result_amount = 500;
break;
case 1:
result_amount = 600;
break;
case -1:
result_amount = 700;
break;
}
But sometimes we want fall-through. For example when we want to calculate number of days in a month:
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
days = 28;
case 4:
case 6:
//Lots of code...
}
Without break statement next cases will be executed.
Generally Case Label Should be Integer / Constant
Negative numbers having no decimal point are also considered as Integer
Thus it is not necessary to have Case Labels greater than Zero
This is fall through condition.
switch (result) {
case 0:
result_amount = all_amounts[i];
case 1:
result_amount = all_amounts[i];
case -1:
result_amount = all_amounts[i+1];
}
To avoid it you must put break statement
switch (result) {
case 0:
result_amount = all_amounts[i];
break;
case 1:
result_amount = all_amounts[i];
break;
case -1:
result_amount = all_amounts[i+1];
break;
}
Now only matching case will be executed.
You should include break and default to run program in a proper way. Let me give you a example of it.
switch (result) {
case 0:
result_amount = 500;
break;
case 1:
result_amount = 600;
break;
case -1:
result_amount = 700;
break;
}
I've seen similar questions about this issue, but what happens to me is a little different;
I am developing a remote control application and I'm sending keystrokes to my computer.
The Robot class in java only accepts VK_CODES for keystrokes, so I have to translate non ascii characters into keystrokes combinations, like this:
public void type(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'á': doType(KeyEvent.VK_A); break;
case 'à': doType(KeyEvent.VK_A); break;
case 'ä': doType(KeyEvent.VK_A); break;
case 'â': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;
etc...
}
private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
This works well, but when I try to combine ALT+numbers characters (for example to type the '#' character) I do:
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_6,KeyEvent.VK_4); break;
It won't type it. If I type it directly with my keyboard, it works.
Is there a reason for this? How can I make a Robot instance to accept all the unicode characters and not just ascii? Is there a better way to do what I am doing?
Thanks for reading and sorry for my English!
I found out that the problem was, I was entering the VK_number directly.
Now it works like a charm using the VK_NUMPAD keys:
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_NUMPAD6,KeyEvent.VK_NUMPAD4); break;
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_NUMPAD3,KeyEvent.VK_NUMPAD5); break;
etc..
I have been working on a program to count vowels in inputted text. It uses this method to recursively add to a vowel count every time a vowel is found. However, I get an out of bounds error every time lastPos reaches negative 1. How can I get this to stop once lastPos reaches -1?
static int R_countVowels(String s, int lastPos)
{
switch (s.charAt(lastPos))
{ case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U': return (1 + R_countVowels(s, --lastPos));
default: return R_countVowels(s, --lastPos);
}
}
I'm assuming this is homework so no code.
Recursive functions require a base case. You need to define your base case to return 0 (no vowels) for an empty input and check for the base case before your inductive step (recursive call).
Insert an if before the switch():
if (lastpos < 0) {
// stop the recursion
}