[Java] Question on arrays

Ritsuki

ORAORAORAORA
OP
Member
Joined
Mar 15, 2008
Messages
1,618
Trophies
1
Age
34
XP
2,596
Country
Switzerland
Hi,

Here's my problem. I've an array of bytes, and I want to separate each value in this array in 4 parts (ex. if array[0] = 01100011, I should get dividedIndex0[4] = {01,10,00,11})

How can I do this ?
Thanks.
 
Solution
You would have to use bit wise operators to do this.

ex:

div Array[0] = array[0] & 0x3;
div Array[1] = (array[0] >> 3) & 0x3;
div Array[2] = (array[0] >> 5) & 0x3;
div Array[3] = (array[0] >> 7) & 0x3;

However, I'm not sure this will work unless you first cast the byte to an int, as Java bit shifts are designed for use on integers.

kiafazool

Well-Known Member
Member
Joined
Apr 21, 2010
Messages
1,221
Trophies
0
Age
29
Location
Canada
Website
www.gbatemp.net
XP
343
Country
Canada
i used google

http://www.google.ca/search?hl=en&q=java+divide+arrays+into+small+parts&aq=f&aqi=&aql=&oq=

http://stackoverflow.com/questions/2895342/java-how-can-i-split-an-arraylist-in-multiple-small-arraylists

http://stackoverflow.com/questions/3405195/divide-array-into-smaller-parts
 

Aeter

A walking contradiction
Member
Joined
Apr 1, 2009
Messages
942
Trophies
1
Age
36
Location
The lands of nether
XP
485
Country
Netherlands
You have to be a bit more clear.
What do you exactly want to do?
Just saying I should get this "dividedIndex0[4] = {01,10,00,11}"
is kind of weird to ask.
You need to explain at what part you are stuck.
If this is a school question perhaps you should show us the full question.
 

Ritsuki

ORAORAORAORA
OP
Member
Joined
Mar 15, 2008
Messages
1,618
Trophies
1
Age
34
XP
2,596
Country
Switzerland
It's not a school question, that's just for fun.

Imagine I have a Byte array with a length of 2 :
array[0] = 01010101
array[1] = 11110000

I want to take array[0] and transform it into another array of length 4 :
divArray[] = {01000000, 00010000, 00000100, 00000001}

To be more precise, I have a .bmp file that I read like a DataStream, and I want to "hide" a message in it, To do it I need to transform each character of my message in bytes (according to the ASCII table) then split them into 4 pieces of 2 bits. Then I'll replace the two last bits of the bytes in the picture with the ones from the message.

ex : My picture is '00110011 11001100 10101010 01010101'
My message is "a", which is 97 in ASCII table and '01 10 00 01' in binary.

The modified picture with the message will be : '00110001 11001110 10101000 01010101

I know how to read the file, how to replace the two last bits, how to rewrite the file, but my only problem is how to split the message into pieces of 2 bits.
 

ronfar23x

Member
Newcomer
Joined
May 13, 2010
Messages
17
Trophies
0
XP
35
Country
United States
You would have to use bit wise operators to do this.

ex:

div Array[0] = array[0] & 0x3;
div Array[1] = (array[0] >> 3) & 0x3;
div Array[2] = (array[0] >> 5) & 0x3;
div Array[3] = (array[0] >> 7) & 0x3;

However, I'm not sure this will work unless you first cast the byte to an int, as Java bit shifts are designed for use on integers.
 
Solution

Ritsuki

ORAORAORAORA
OP
Member
Joined
Mar 15, 2008
Messages
1,618
Trophies
1
Age
34
XP
2,596
Country
Switzerland
Thanks, I'll try your solution ronfar. I used a similar solution, but instead of using logical operation, I transformes the bytes into integers then used modulo. The only problem is that my code is really ugly now, and I have a LOT of repetitions.
 

ronfar23x

Member
Newcomer
Joined
May 13, 2010
Messages
17
Trophies
0
XP
35
Country
United States
I'm pretty sure you will have to change the bytes into integers. Or, you could work directly on the bytes themselves without having to change them.

(int) byte = (((((int) byte = val3) << 3) | val2) << 5) | val1;

That should work. I'll have to check some source code I wrote at home to be sure...
 

omatic

Well-Known Member
Member
Joined
Apr 25, 2007
Messages
408
Trophies
0
Age
36
Location
United States
Website
Visit site
XP
261
Country
United States
Global vars:
Make a temp binary var, temp1 and temp2. Temp 1 will contain the last two digits, temp2 will contain the remaining bits.

Also make a size 4 array, and initialize it with 0's, and an index int initialized at 0.
--
Main method: Call helper method.
--
Helper method:
Obtain temp with (original binary [bitwise AND] 00000011).
Original binary becomes the original with a bit shift to the right two times.

array[index] = temp1
index++
if original binary != 0, repeat method

*Optional: else if original binary = 01 or 10 or 11, array[index] = original binary
----
*Note: 0's to the left of the last 1 are ignored. Therefore, your example would look more like {1,10,0,11}.
 

ronfar23x

Member
Newcomer
Joined
May 13, 2010
Messages
17
Trophies
0
XP
35
Country
United States
I do have one question though. Since you're using it to operate on an image, have you considered using Java's PixelGrabber class? it will transform the image to a one dimensional array of integers, each integer representing the rgb of one pixel. You can ten perform bitwise tasks on the integers, and then write them back to an image file easily. :D
 

Ritsuki

ORAORAORAORA
OP
Member
Joined
Mar 15, 2008
Messages
1,618
Trophies
1
Age
34
XP
2,596
Country
Switzerland
@omatic : Thanks, but I need to keep the 0's on the left to avoid complication after.

@ronfar : I saw something like that on th API, but I had already finished ^^' Thanks again for your solution ! Finally I did the same thing, but I used modulo instead of the left shift (it's exactly the same thing, but less beautiful. The only advantage is that my code for extracting the message from the picture is easier to do now)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • K3Nv2 @ K3Nv2:
    Sweet I dont need a dozen pieces of other shit to hack it :)
  • BigOnYa @ BigOnYa:
    Sweet
  • BigOnYa @ BigOnYa:
    Hi
  • K3Nv2 @ K3Nv2:
    Thanks for signing up at LinusTechTips
  • QuarterCut @ QuarterCut:
    holey shmoley!
  • BigOnYa @ BigOnYa:
    Your credit card has been charged. Thank you.
  • K3Nv2 @ K3Nv2:
    Your screwdriverPlus will arrive in three weeks
    +1
  • QuarterCut @ QuarterCut:
    K64_Waddle_Dee_Artwork_1.jpg

    my reaction to such information
    +2
  • BigOnYa @ BigOnYa:
    Press 1 for English. Press 2 for Pig Latin. Or press 3 to speak to a representative.
  • BakerMan @ BakerMan:
    guys, i need help, i got into an argument about what genre radioactive is, and i forgot who made it
  • Sicklyboy @ Sicklyboy:
    @BakerMan, Imagine Dragons
  • Sicklyboy @ Sicklyboy:
    Dragon deez nuts across yo face GOTEEM
  • Sicklyboy @ Sicklyboy:
    lmao now I realize that was probably the joke in the first place
  • BakerMan @ BakerMan:
    IMAGINE DRAGON DEEZ NUTS ACROSS YO- FUCK HE BEAT ME TO IT
  • BigOnYa @ BigOnYa:
    You have selected 4 - Death by Snu Snu, please stand by...
    +1
  • BakerMan @ BakerMan:
    lucky bastard
    +1
  • Sicklyboy @ Sicklyboy:
    hahahaha I'm half way through a bag off my Volcano and my tolerance is way down because I haven't been smoking much lately, so I was a little slow to catch that that was what your angle was 🤣🤣
    +1
  • Sicklyboy @ Sicklyboy:
    Also I was just excited to know a music reference for once (I am the LAST person in the world that you want on your trivia team)
    +1
  • K3Nv2 @ K3Nv2:
    Bummer webos 7.4 isnt working with dejavuln-autoroot
    K3Nv2 @ K3Nv2: Bummer webos 7.4 isnt working with dejavuln-autoroot