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 2 years ago.
Improve this question
I have this integer in java:
int i = 1067030938;
byte b = (byte) i;
which gives me:
-102
How could I go from b back to i ?
I tried :
b = b & 0xff;
but this gives 154
If you have
int i = 1067030938;
And just want the low order 8 bits, then do
i &= 0xff;
System.out.println(i);
Prints
154
If you want the signed value of converted int then do this
i = (byte)i; // will be -102
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
In Character.java of the java.lang package (Oracle JDK 8),
public static final int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000;
public static final int MAX_CODE_POINT = 0X10FFFF;
public static boolean isSupplementaryCodePoint(int codePoint) {
return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT
&& codePoint < MAX_CODE_POINT + 1;
}
why did the author(s) choose to add one to MAX_CODE_POINT and then compare the result to codePoint using the < operator?
Why not just do the following, where the "<=" operator is used:
public static boolean isSupplementaryCodePoint(int codePoint) {
return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT
&& codePoint <= MAX_CODE_POINT;
}
There are other examples in the same file like this, where one is being added to the right-hand operand and then the less-than operator is used to do the comparison. Is it being done this way for performance reasons? Can someone explain the original reasoning? And why isn't it being done for the <= operator but not the >= operator? How relevant is the reasoning today?
Traditionally, Java uses half-open intervals in a lot of situations--that is, intervals where a number is >= the lower bound (i.e. inclusive) and < the upper bound (i.e. exclusive). See, for example, the definitions of substring() and binarySearch in Arrays. It corresponds to how for loops are usually written: for (int i = lower-bound; i < upper-bound; i++), which looks at the same kind of half-open interval from lower-bound to upper-bound. The author may have been just following that pattern. There's no performance difference either way.
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 6 years ago.
Improve this question
I'm looking for a function such as Math.floor(int x) but for a variable of type BigInteger. Does anyone know how to do it?
Let's suppose a and b are int variables. In that case a/b is an int. Test case:
int a = 5;
int b = 2;
System.out.println(a / b); //expected output is 2
Let's suppose a and b are integers, but we intend to convert the result of the division into a float. The result depends on the way of conversion. Test case:
int a = 5;
int b = 2;
System.out.println((float)(a / b)); //expected output is 0.0
System.out.println(((float)a) / b); //expected output is 2.5
Let's suppose a and b are BigInteger variables. a.divide(b) will return a BigInteger value (so floor is not needed) unless b is 0 in which case it will throw an exception. Source.