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.
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
My question is about Java int data structure.
If I have a buffer of 4 bytes
byte[] buffer = {(byte)A, (byte)B, (byte)C, (byte)D};
How to build a positive and a negative int values using this array and logic operators (&, |, <<, >>)?
Say int = 4 and int = 130; and -4 and -130.
to construct 32 bit value from four 8 bit values you need to know Endianness
if we assume that high bits are stored into first byte, then you can do:
int v = 0;
for (int i=0; i<4; i++)
v = (v<<8) | (buffer[i]&0xff);
if high bits are stored in last byte, then you need to reverse loop
int v = 0;
for (int i=3; i>=0; i--)
v = (v<<8) | (buffer[i]&0xff);
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);
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert a string representation of a hex dump to a byte array using Java?
For example, I have a string "DEADBEEF". How can I convert it to byte[] bytes = { 0xDE, 0xAD, 0xBE, 0xEF } ?
Loop through each pair of two characters and convert each pair individually:
byte[] bytes = new byte[str.length()/2];
for( int i = 0; i < str.length(); i+=2 )
bytes[i/2] = ((byte)Character.digit(str.charAt(i),16))<<4)+(byte)Character.digit(str.charAt(i),16);
I haven't tested this code out (I don't have a compiler with me atm) but I hope I got the idea through. The subtraction/addition simply converts 'A' into the number 10, 'B' into 11, etc. The bitshifting <<4 moves the first hex digit to the correct place.
EDIT: After rethinking it a bit, I'm not sure if you're asking the correct question. Do you want to convert "DE" into {0xDE}, or perhaps into {0x44,0x45} ? The latter is more useful, the former is more like a homework problem type question.
getBytes() would get you the bytes of the characters in the platform encoding. However it sounds like you want to convert a String containing a Hex representation of bytes into the actual represented byte array.
In which case I would point you toward this existing question: Convert a string representation of a hex dump to a byte array using Java? (note: I personally prefer the 2nd answer to use commons-codec but more out of philosophical reasons)
You can parse the string to a long and then extract the bytes:
String s = "DEADBEEF";
long n = Long.decode( "0x" + s ); //note the use of auto(un)boxing here, for Java 1.4 or below, use Long.decode( "0x" + s ).longValue();
byte[] b = new byte[4];
b[0] = (byte)(n >> 24);
b[1] = (byte)(n >> 16);
b[2] = (byte)(n >> 8);
b[3] = (byte)n;
tskuzzy's answer might be right (didn't test) but if you can, I'd recommend using Commons Codec from Apache. It has a Hex class that does what you need.