Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a hex string format below:
2828287798519497FFFF9000 => 2828287798519497 (result)
1122334466667788996FFFF9000 => 1122334466667788996 (result)
which the id is length in between 16 – 19, where right most is fill with 0xF.
What is 0xF?
How can I get id number, wither it is 16, 17, 18 or 19 length from the hex string above?
BigInteger is for arbitrary precision integral math, and it has a constructor that takes a String and an int radix. 0xF is the sixteenth value in base 16 (digits are the usual zero to nine of base-10 and the values A, B, C, D, E and F).
System.out.println(new BigInteger("2828287798519497FFFF9000", 16));
System.out.println(new BigInteger("1122334466667788996FFFF9000", 16));
The base-10 representation of your two values is thus
12427948526435964620659200000
21719411700849473095611778568192
Based on the examples you gave, the ID number you want consists of all but the last eight characters of the given hex string (which are FFFF9000 in both example cases). In other words, a substring starting at the beginning of the string and extending up to, but not including, the eight-to-last character:
String h = "2828287798519497FFFF9000";
String id = h.substring(0, h.length()-8);
System.out.println(h + " => " + id);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a small problem. I think it's simple, but I don't know, how to manage it properly.
I have this simple int:
int birth = 011112;
and I want output to look like this, in this specific format.
"Your birth date is 12.11.01."
I did it with an integer array, but I want only one integer input like this one, not an array.
Could some body help me? Is there any simple method to manage it of, without using loops?
Basically, the conversion of the int representing a date in some format into String should use divide / and modulo % operations, conversion to String may use String.format to provide leading 0.
The number starting with 0 is written in octal notation, where the digits in range 0-7 are used, so the literals like 010819 or 080928 cannot even be written in Java code as int because of the compilation error:
error: integer number too large
int birth = 010819;
However, (only for the purpose of this exercise) we may assume that the acceptable octal numbers start with 01 or 02 then such numbers are below 10000 decimal.
Then the numeric base for division/modulo and the type of output (%d for decimal or %o for octal) can be defined:
public static String rotate(int x) {
int base = x < 10000 ? 0100 : 100;
String type = x < 10000 ? "o" : "d";
int[] d = {
x % base,
x / base % base,
x / (base * base)
};
return String.format("%02" + type + ".%02" + type + ".%02" + type, d[0], d[1], d[2]);
}
Tests:
System.out.println(rotate(011112)); // octal
System.out.println(rotate(11112)); // decimal (no leading 0)
Output:
12.11.01
12.11.01
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am doing a project which requires me to convert arabic text to binary string UTF-16 instead of utf-8. I converted the text to UTF-8 Binarystring but have no idea how to change the process to utf-16 insted of utf-8..? because when i changed it to utf-16 it takes 4bytes for every codepoints instead of 2 bytes .I know Arabic characters range between(range 0600 to FFFF hex) takes exactly 2 bytes for every codepoint in utf-16.So I do not know what is the problem in my code .
// Convert the text to binary
public static String getBinaryFromText(String secretText) {
byte[] bytes = secretText.getBytes(StandardCharsets.UTF_8);
StringBuilder binary = new StringBuilder();
for (byte b : bytes) {
int val = b;
for (int i = 0; i < 8; i++) {
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
return binary.toString();
}
Strings are intrinsically UTF-16. Each char is a UTF-16 codepoint. secretText.charAt(0) is the first UTF-16 character, etc, etc.
You can use a Charset to do the conversion treating the UTF-16 as a byte sequence. Do Charset.forName("UTF-16") and use the encode method.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am reading a number from a file, and I want to convert it to an ASCII text string. I've seen questions relating to this but they all were dealing with single decimal-equivalent ASCII values, like 12 to ASCII, 87 to ASCII, 112 to ASCII, etc. I need to convert a large amount of decimals with no spaces into ASCII text. Can anyone show me how this is done? How would the system ascertain whether the first number to translate is 1, 12, or 123?
For example:
int intval = 668976111;
//CONVERSION CODE
System.out.println(newval);
prints "bylo"
If the int 123, how would it know if I was saying 1,2,3 or 12,3 or 123 or 1,23 etc? How can I convert decimal numbers like this to Unicode characters?
Try this.
static void decode(String s, int index, byte[] decode, int size) {
if (index >= s.length())
System.out.println(new String(decode, 0, size));
else
for (int i = index + 1; i <= s.length(); ++i) {
int d = Integer.parseInt(s.substring(index, i));
if (Character.isISOControl(d)) continue;
if (d > 255) break;
decode[size] = (byte)d;
decode(s, i, decode, size + 1);
}
}
static void decode(String s) {
decode(s, 0, new byte[s.length()], 0);
}
and
decode("668976111"); // -> BYLo
This is a kinda hard problem, as ascii codes can be single, two or three digit long.
Now if your encoding only alphadecimal characters and characters above the decimal number 20 it is pretty easy.
The algorithm wouild be as follows. Iterate through the array(a digit is an element of the array), if the first number is 1, take 3 numbers, as you cant have a char with code less than 20. If the first number is higher than 20, take only 2 numbers.
This way you will get the right decoding, assuming you dont have anything encoded with codes less than 20, which is a very possible assumption, as the first "useful" code is at number 32, which is space
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have the following problem of a task that I need to make. It's about checking if a bankaccount number is correct or not this is the task:
The first group consists of 3 digits and determines which bank it is .
The second group consists of 7 digits and establishes the customer goes to that bank.
The third group consists of two numbers and a check digit which the validity of the bank account is determined .
The verification is done as follows:
The first and second group together form a number consisting of 10 digits. If you divide this number by 97 then the remainder after division by 97 must be equal to the third group of the bankaccount.
Maybe this is what you are looking for:
private boolean checkNumber(String number) {
//number consists of 12 digits
String firstGroup = number.substring(0, 3);
String secondGroup = number.substring(3, 10);
String thirdGroup = number.substring(10, 12);
int firstSecond = Integer.parseInt(firstGroup + secondGroup);
int third = Integer.parseInt(thirdGroup);
int remainderAfterDevision = firstSecond % 97;
return (remainderAfterDevision == third);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to change the meaning of the asterisk symbol (multiplication operator) in Java?
I want to do this:
int ex = 600 * 0.98;
It fails because you can't convert from double to int. Can I make it so that I'm able to convert the double to an integer? Or does the
asterisk only have one meaning that can't be changed?
I just want the OPERATION in math, not a string.
To have an operation, you must have code. One way of doing this is to have a method like
public static double ex(double factor) {
return factor * 0.98;
}
or if the factor is a field
private double factor = 600;
public double ex() {
return factor * 98 / 100;
}
public static void main(String... ignored) {
Main m = new Main();
System.out.println("factor: "+m.factor+" ex: "+ m.ex());
m.factor = 700;
System.out.println("factor: "+m.factor+" ex: "+ m.ex());
}
prints
factor: 600.0 ex: 588.0
factor: 700.0 ex: 686.0
As you can see ex() is re-evaluated each time it is used.
Why do I * 98 / 100? I do this as each value can be represented exactly however 0.98 is not represent exactly and can have a small error.
System.out.println(new BigDecimal(0.98));
prints the closest representable value to 0.98
0.979999999999999982236431605997495353221893310546875
If you want to store text you need to do something like
String ex = "600 * 0.98";
An int value is for storing a whole number which is a 32-bit signed value, nothing else.
Uhhh.
The short answer is that you can't do that. A variable of type int can only store an integer value. The expression you assign to the int has to evaluate to an integer.
What integer value do you want assigned to ex?
int ex = 588; // 600 * 0.98
What meaning are you associating with the asterisk between two numeric values that is not multiplication?
If you want to store an array of characters, then:
char[] ex = "600 * 0.98".toCharArray();