I have an int array int[] stegoBitsArray = {0,1,1,0,1,1,0,0,0,1,1,0,0,0,0,1...} I want each 8 bits converted to byte and added to byte array which i would later convert to char array because 01101100 is letter "l" and so on so ... So i tried using ByteBuffer but it throws me BufferOverflowException here's my code snippet:
int[] stegoBitsArray = getStegoTextFromImage(stegoImage);
ByteBuffer byteBuffer = ByteBuffer.allocate(stegoBitsArray.length);
for (int i = 0; i < stegoBitsArray.length; i++) {
byteBuffer.putInt(stegoBitsArray[i]);
}
Or it's more better to convert from int array to characters?
To convert from binary to char you have to convert each 8 bit binary into byte and then you can cast from byte to char. Example:
byte b = Byte.parseByte("01100001", 2);
System.out.println((char)b);
Prints
a
How come you can have a value like 0110110001100001 in an integer array. Either it can be stored as a string or there is another way.
You can have an array of integer values something like int array = {123, 456, 333} etc then you can directly convert those integers to characters by looping on the array.
for(int i = 0; i < array.length; i++){
char c = (char)array[i];
System.out.println(c);
}
Or if you want to convert integer to binary then Java supports in Integer class a method called toBinaryString. you can find a good resource for it HERE.
The combination of these two can lead you to your desired solution.
Related
I've been suggested a TCP-like checksum, which consists of the sum of the (integer) sequence and ack field values, added to a character-by-character sum of the payload field of the packet (i.e., treat each character as if it were an 8 bit integer and just add them together).
I'm assuming it would go along the lines of:
char[] a = data.toCharArray();
for (int i = 0; int < len; i++) {
...
}
Though I'm pretty clueless as to how I could complete the actual conversion?
My data is string, and I wish to go through the string (converted to a char array (though if there's a better way to do this let me know!)) and now I'm ready to iterate though how does one convert each character to an int. I will then be summing the total.
As String contains Unicode, and char is a two-byte UTF-16 implementation of Unicode, it might be better to first convert the String to bytes:
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
data = new String(bytes, StandardCharsets.UTF_8); // Inverse.
int crc = 0;
for (byte b : bytes) {
int n = b & 0xFF; // An int 0 .. 255 without sign extension
crc ^= n;
}
Now you can handle any Unicode content of a String. UTF-8 is optimal when sufficient ASCII letters are used, like Chinese HTML pages. (For a Chinese plain text UTF-16 might be better.)
I need a way to do this
String username = "Snake";
int usernameLength = username.length(); // 5
converting it to
0x05
Should I use a for loop to get each number and add a zero if the result is less than two numbers?
Try the ByteBuffer class...
byte[] byteArray = ByteBuffer.allocate(1).putInt(username.length()).array();
I'm wondering how I can convert a string like of letters to their numerical value in an array. For example, A is 0, B is 1. I know I need to use like a for loop like this:
for (int i = 0; i < 26; i++), but I'm not sure what code fragment to actually use to do the converting into an int array? Help?
Converting a letter (char) to an integer representing its place in the alphabet is easier than some people realize; all you have to do is:
(int)(c - 'A') // the "distance" between c and 'A' = place of c in alphabet
Loop through the characters of your string and perform this operation for each, storing the results in a new int array.
You can get the each Char by yourString.charAt(i) and then cast it with (int) this will gave you the Corresponding ASCII then subtract from the ASCII of 'A'. You will have what you want
result = (int)yourString.charAt(i) - (int)'A'
I get hex strings of 14 bytes, e.g. a55a0b05000000000022366420ec.
I use javax.xml.bind.DatatypeConverter.parseHexBinary(String s) to get an array of 14 bytes.
Unfortunately those are unsigend bytes like the last one 0xEC = 236 for example.
But I would like to compare them to bytes like this:
if(byteArray[13] == 0xec)
Since 235 is bigger than a signed byte this if statement would fail.
Any idea how to solve this in java?
Thx!
Try if(byteArray[13] == (byte)0xec)
You can promote the byte to integer:
if((byteArray[13] & 0xff) == 0xec)
since java doesn't support (atleast with primitives) any unsigned data types, you should look at using int as your data type to parse the string..
String str = "a55a0b05000000000022366420ec";
int[] arrayOfValues = new int[str.length() / 2];
int counter = 0;
for (int i = 0; i < str.length(); i += 2) {
String s = str.substring(i, i + 2);
arrayOfValues[counter] = Integer.parseInt(s, 16);
counter++;
}
work with the arrayOfValues...
Basically, I'm looking for .NET's BitConverter.
I need to get bytes from String, then parse them to long value and store it. After that, read long value, parse to byte array and create original String. How can I achieve this in Java?
Edit: Someone did already ask similar question. I am looking more like for samples then javadoc reference ...
String has a getBytes method. You could use this to get a byte array.
To store the byte-array as longs, I suggest you wrap the byte-array in a ByteBuffer and use the asLongBuffer method.
To get the String back from an array of bytes, you could use the String(byte[] bytes) constructor.
String input = "hello long world";
byte[] bytes = input.getBytes();
LongBuffer tmpBuf = ByteBuffer.wrap(bytes).asLongBuffer();
long[] lArr = new long[tmpBuf.remaining()];
for (int i = 0; i < lArr.length; i++)
lArr[i] = tmpBuf.get();
System.out.println(input);
System.out.println(Arrays.toString(lArr));
// store longs...
// ...load longs
long[] longs = { 7522537965568945263L, 7955362964116237412L };
byte[] inputBytes = new byte[longs.length * 8];
ByteBuffer bbuf = ByteBuffer.wrap(inputBytes);
for (long l : longs)
bbuf.putLong(l);
System.out.println(new String(inputBytes));
Note that you probably want to store an extra integer telling how many bytes the long-array actually stores, since the number of bytes may not be a multiple of 8.