How to print very long (100000 items) array of integers to stdout - java

I'm trying to output some very long (100000 items) array of integers to stdout.
public int myPrint(int[] A) {
System.out.println("A " + Arrays.toString(A));
}
It fails because stdout on the machine the code runs cuts the string from some point like (this is whole output, as you can see there is not 100000 items but much less):
A [-1, -1, 0, -1, 1, -1, 0, -1, 1, -1, 0, 0, -1, -1, 0, -1, 0, -1, -1, 0, 1, -1, -1, 1, -1, 1, 0, -1, 1, 1, 0, 1, -1, 0, 0, 1, -1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, -1, 1, -1, 1, 1, 0, -1, 0, 0, -1, 0, 0, 0, -1, -1, 0, -1, 0, -1, 0, 1, 1, 0, 1, 1, 0, -1, 0, -1, 1, 0, 1, -1, -1, -1, -1, 0, -1, 0, 0, -1, -1, 0, -1, 0, -1, 0, -1, 1, 0, 1, -1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, -1, -1, 1, -1, 0, 0, -1, 0, 1, 1, 0, -1, 1, 0, 0, 1, 0, -1, -1, 1, 0, 1, 1, 1, 1, -1, 1, -1, 0, 1, 0, -1, 0, 0, 0, 1, 0, -1, 0, -1, -1, 0, 1, -1, -1, -1, 1, 1, -1, 0, -1, -1, 0, 0, 1, 1, 0, 1, 0, 1, 1, -1, 1, 0, 0, 0, -1, 0, -1, 0, 1, 1, 0, 0, 1, -1, -1, 1, 0, 1, 0, 0, 1, 0, 1, 1, -1, 1, -1, 0, 0, 1, -1, 0, 0, 1, -1, -1, 1, -1, 1, -1, 0, -1, 0, 1, -1, 0, -1, 0, 0, -1, 1, 0, 1, -1, 1, -1, -1, 0, 1, 0, -1, 1, -1, 1, 1, -1, 1, 0, 1, 1, 1, -1, 0, 1, -1, 0, 1, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 1, 1, 1, -1, 1, 0, -1, -1, 0, 0, -1, -1, 1, -1, 1, 0, 1, -1, 0, 1, 0, 0, 0, -1, -1, -1, -1, 1, -1, 1, -1, 0, 0, -1, 1, 1, 1, 0, -1, -1, -1, 1, 1, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 1, 0, -1, -1, 1, 1, -1, 0, 1, 0, -1, 0, -1, -1, 1, -1, 1, 1, 0, -1, -1, 1, 1, -1, -1, 0, 1, -1, 1, -1, -1, -1, 0, 1, 0, -1, -1, 0, -1, 1, 0, 1, 1, 1, -1, 0, 1, -1, -1, -1, 0, 1, 1, -1, -1, 1, -1, -1, 1, 1, 0, 1, 1, 1, -1, 0, 1, 0, 0, 1, 1, 0, 0, 1, -1, 0, 1, 0, -1, 1, 0, 0, -1, 0, 1, -1, 0, -1, -1, 0, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 0, -1, 1, 0, -1, 1, -1, -1, -1, -1, 0, 0, 0, 0, -1, 0, 1, -1, 1, 0, 1, 0, 0, -1, -1, -1, -1, 1, 1, 1, 1, 0, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 0, 0, 0, 0, -1, -1, 0, 1, 0, -1, -1, -1, 1, 0, 0, 0, 1, 1, -1, 0, 0, 1, -1, 0, -1, 0, 1, 0, 0, 0, 1, 1, -1, -1, 0, -1, -1, 1, -1, 0, -1, 1, 1, 1, 1, 0, 0, -1, 0, 1, 0, -1, -1, -1, 1, -1, 1, 0, 0, 0, -1, 0, -1, 0, 0, 0, 1, 1, 1, 0, -1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, -1, 0, -1, 0, 1, 0, 1, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 0, -1, 1, -1, 1, 0, -1, -1, 0, 1, 0, -1, 0, 1, 0, -1
The code is remotely executed and I don't have option to change stdout formatting/buffer size etc. on the machine so it must be done inside the method.
Maybe I should split the string into sub strings?

Do not use Arrays.toString here. But output immediate values.
Maybe with extra flushing, as done with a println.
public int myPrint(int[] A) {
System.out.print("A [");
for (int i = 0; i < A.length; ++i) {
if (i % 25 == 0) {
System.out.println(); // Or flush()
}
if (i != 0) {
System.out.print(", ");
}
System.out.print(A[i];
}
System.out.println("]");
}

int width = 80;
int c = 0;
for(int i: A){
String item = i + " ";
c += item.length();
System.out.print(c);
if(c>width){
System.out.println();
c=0;
}
}
Only prints 80 or so characters to the terminal before adding a new line.

Related

why is my board not being rendered in the same orientation as the 2D array

With this code I'm getting the board in the picture. can any please help me figure out why this is happening and how I fix it.
I've tried calling super.paintComponent but nothing is changing. if i just needed the board I would have probably left it but I implemented a search and the search kept going to the wrong position because the array doesnt match the board which I dont understand because the array is defining the board. This problem is beyond my abilities and help is greatly appreciated.
protected int[][] board = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1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, 1, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length ; col++) {
Color color;
switch (board[row][col]) {
case 0:
color = new Color(149, 153, 146);
break;
default:
color = new Color(38, 252, 0);
}
g2.setColor(color);
g2.fillRect(row * pixelSize, col * pixelSize, pixelSize, pixelSize);
}
}
}
Change this:
g2.fillRect(row * pixelSize, col * pixelSize, pixelSize, pixelSize);
to this:
g2.fillRect(col * pixelSize, row * pixelSize, pixelSize, pixelSize);
X coordinate comes before Y.
X is defined by column.
Y is defined by row.

How to read the TypedArray data in Android?

I'm new to Android and still following some tutorials. One tut is about TypedArray for storing the image information.
MainActivity.java
package com.example.materialme;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TypedArray sportsImageResources = getResources().obtainTypedArray(R.array.sports_images);
Log.d(TAG, "sportImageResources: " + sportsImageResources);
sportsImageResources.recycle();
}
}
strings.xml
<resources>
<array name="sports_images">
<item>#drawable/img_baseball</item>
<item>#drawable/img_badminton</item>
<item>#drawable/img_basketball</item>
<item>#drawable/img_bowling</item>
<item>#drawable/img_cycling</item>
<item>#drawable/img_golf</item>
<item>#drawable/img_running</item>
<item>#drawable/img_soccer</item>
<item>#drawable/img_swimming</item>
<item>#drawable/img_tabletennis</item>
<item>#drawable/img_tennis</item>
</array>
</resources>
When I see in logcat, the values of TypedArray sportsImageResources are
[3, 179, 6, 2131165272, 0, 0, 3, 178, 6, 2131165271, 0, 0, 3, 180, 6, 2131165273, 0, 0, 3, 181, 6, 2131165274, 0, 0, 3, 182, 6, 2131165275, 0, 0, 3, 183, 6, 2131165276, 0, 0, 3, 184, 6, 2131165277, 0, 0, 3, 185, 6, 2131165278, 0, 0, 3, 186, 6, 2131165279, 0, 0, 3, 187, 6, 2131165280, 0, 0, 3, 188, 6, 2131165281, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 17, 512, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 16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 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 2131558747, 6, 2131558747, 1073742848, 0, 1, 2131558746, 6, 2131558746, 1073742848, 0, 28, -1, 1, 17170443, 1073742848, 0, 3, 193, 6, 2131165294, 1073742848, 0, 3, 27, 6, 0, 1073742848, 0, 18, -1, 6, 0, 1073742848, 0, 18, 0, 6, 0, 1073742848, 0, 18, 0, 6, 0, 1073742848, 0, 0, 0, -1, 0, 1073742848, 0, 0, 0, -1, 0, 1073742848, 0, 0, 0, -1, 0, 1073742848, 0, 0, 0, -1, 0, 1073742848, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 18, 0, 6, 0, 1073742848, 0]
How to read this data?
May be this example will help you.
TypedArray sportsimgs = getResources().obtainTypedArray(R.array.random_imgs);
// get resource ID by index
sportsimgs .getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(sportsimgs .getResourceId(i, -1));
// recycle the array
sportsimgs .recycle();

Multiple data conversion with groovy

I have a problem doing several conversions on some data.
I get a response from a request that contains base64 encoded bytes and I'd like to get them in hexa.
This is a String type:
log.info "type data base 64 : " + dataBase64.getClass().getName()
INFO:type data base 64 : java.lang.String
Then I decode it:
data = dataBase64.decodeBase64()
log.info "type data : " + data.getClass().getName()
INFO:type data : [B => ?? there is something strange here
log.info "data = " + data
INFO:data = [0, 33, 0, 0, -74, 0, 16, 0, 0, 1, 0, 10, 3, 0, 0, 0, 28, 120, -45, 28, 90, 95, 25, -14, -119, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I want this data in bytes in order to recover an address in bytes 22 to 24
If I do
log.info "dataHex = " + data.encodeHex()
INFO:dataHex = 00210000b60010000001000a030000001c78d31c5a5f19f289ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01000000000000000000000000000000000000000000000000000000000000000000000000000000
I want to recover the 19f289 part. The problem is that I cannot convert data[22] to data[24]
data[22].encodeHex fails (the data[22] type is Byte, not Byte[])
If I try to use toHexString on each byte required, I encounter a problem when handling negative values
for (i=22;i<25;i++)
{
myInt = data[i]
log.info Integer.toHexString(myInt)
}
gives
INFO:19
INFO:fffffff2
INFO:ffffff89
Is there any clean way to do this ?
I could try to extract the substring from dataHex after conversion to String (I haven't tried it yet) but I don't understand why I can convert simply a byte to hexadecimal
if anyone knows ...
Well,
After improving my knowledge of Groovy thanks to a collegue, here is a cleaner way to recover the data I want :
dataHex = data.encodeHex().toString()
log.info "data hex in bytes = ${dataHex}"
// group data by bytes
def dataHexBy2 = dataHex.toList().collate(2)*.join()
log.info " group by 2 " + dataHexBy2
def address = ""
for (i=22;i<25;i++)
{
address = address + dataHexBy2[i]
}
log.info "address = ${address}"
and I finally get address = 19f289

2.5D Plane in Slick2D is curved for some reason

I'll readily admit that this code is weird and a pretty unorthodox way of creating anything 2.5D, however if anyone can see what's going on I would appreciate help.
The result of my code is as shown:
I do of course want a totally flat plane, however I'm unsure as to why it curves like so. The code is shown below if anyone has any answers:
public static int width = 640;
public static int height = 480;
private Image image;
private Graphics imageG;
private int mapWidth = 20, mapHeight = 15;
private int[] map;
private float x, y;
public Game(String title)
{
super(title);
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
imageG.setBackground(Color.black);
imageG.clear();
for (int y = 0; y < mapHeight; y++)
for (int x = 0; x < mapWidth; x++)
{
switch (map[x + y * (mapWidth)])
{
case 0:
imageG.setColor(Color.green);
break;
case 1:
imageG.setColor(Color.yellow);
break;
}
imageG.fillRect(x * 32, y * 32, 32, 32);
}
imageG.flush();
Image frustrum;
for (int i = 0; i < 240; i++)
{
frustrum = image.getSubImage((int) (x - i), (int) (y - i), 32 + 2 * i, 1);
frustrum.draw(0, height - 1 - i, width, 1);
//g.drawImage(frustrum, x - i, y - i);
}
//g.setColor(Color.blue);
//g.fillRect(x, y, 32, 32);
}
#Override
public void init(GameContainer gc) throws SlickException
{
try
{
image = new Image(width, height);
imageG = image.getGraphics();
} catch (SlickException e)
{
e.printStackTrace();
}
/*map = new int[]
{
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
};*/
map = new int[mapWidth * mapHeight];
Random random = new Random();
for (int i = 0; i < map.length; i++)
{
map[i] = random.nextInt(2);
}
x = 10 * 32;
y = 7 * 32;
}
#Override
public void update(GameContainer gc, int delta) throws SlickException
{
if (gc.getInput().isKeyDown(Input.KEY_W)) y--;
if (gc.getInput().isKeyDown(Input.KEY_S)) y++;
if (gc.getInput().isKeyDown(Input.KEY_A)) x--;
if (gc.getInput().isKeyDown(Input.KEY_D)) x++;
}
public static void main(String[] args)
{
try
{
AppGameContainer appgc = new AppGameContainer(new Game("2.5D Game"));
appgc.setDisplayMode(width, height, false);
appgc.start();
} catch (SlickException e)
{
e.printStackTrace();
}
}
This is my first post, so sorry if things look a bit messy. I'm sure I'll get used to it eventually...

Convert Binary Array to Char Java

I have this binary array:
int[] bitArray = {
0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,
0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,
0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,1,0,1,0,0,
0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,1,
0,1,1,1,1,0,0,1,};
It makes the phrase: The sun is in the sky
How would you convert the int binary array to a char?
Every eight bits make up a char. You could just loop over the bits and accumulate every eight together:
int[] bitArray = {0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1,};
char[] chars = new char[bitArray.length / 8];
for (int i = 0; i < chars.length; ++i) {
int c = 0;
for (int j = i * 8; j < (i + 1) * 8; ++j) {
c = c << 1;
c += bitArray[j];
}
chars[i] = (char)c;
}
String s = new String(chars);
System.out.println(s);
Loop over every byte (8 bits) of the array, create a string of those bits, and then convert those bits to an integer using Integer.parseInt(x,2) and then cast that to a character and added to the result.
public static void main (String[] args) throws java.lang.Exception
{
int[] bitArray = {0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,1,0,1,1,1,1,0,0,1,};
int CHAR_SIZE = 8;
String T = "";
String result ="";
for (int i=0; i<bitArray.length; i+= CHAR_SIZE)
{
for (int j=0; j<CHAR_SIZE; j++)
T += Integer.toString(bitArray[i+j]);
result += Character.toString((char)Integer.parseInt(T, 2));
T="";
}
System.out.println(result);
}
output
The sun is in the sky
ASCII chars are one byte. One byte is eight bits. Separate the array elements in segments of 8 (0-7), (8-15), ...
Store these segments in another array. Use the following constructor to finish.
public String(byte[] bytes,
Charset charset)
Remember to specify utf8 as your charset. This causes the chars to be treated as one-byte ASCII chars.

Categories