Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Formatting binary output from an array in Java
New Posts  All Forums:Forum Nav:

Formatting binary output from an array in Java

post #1 of 11
Thread Starter 
Here's my code: http://pastebin.com/x8XKSufP

I need to format the output into proper binary format. For example, if you enter a decimal "64" I need it to output "0100 0000", NOT 1000000. I've been trying to figure this out for over an hour. Please help.
über pwnage v2
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4.4GHz @ 1.36v Asus P8Z68-V LX EVGA GTX 970 SuperClocked ACX2.0 4GB G.SKILL Ripjaws X 2x8GB DDR3 @ 1648MHz 
Hard DriveOptical DriveCoolingOS
840EVO 250GB / 3TB+2TB+320GB Bar / 750GB Cav Blk Samsung DVDRW Hyper 212+ Windows 10 Pro x64 
MonitorKeyboardPowerCase
U2412M + X191W Azio MGK1 Corsair RM650X Centurion 5 with acoustic foam 
MouseMouse PadAudio
MX518 SteelSeries Xonar DG + Koss PortaPro + Logitech X-540 + Bey... 
  hide details  
Reply
über pwnage v2
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4.4GHz @ 1.36v Asus P8Z68-V LX EVGA GTX 970 SuperClocked ACX2.0 4GB G.SKILL Ripjaws X 2x8GB DDR3 @ 1648MHz 
Hard DriveOptical DriveCoolingOS
840EVO 250GB / 3TB+2TB+320GB Bar / 750GB Cav Blk Samsung DVDRW Hyper 212+ Windows 10 Pro x64 
MonitorKeyboardPowerCase
U2412M + X191W Azio MGK1 Corsair RM650X Centurion 5 with acoustic foam 
MouseMouse PadAudio
MX518 SteelSeries Xonar DG + Koss PortaPro + Logitech X-540 + Bey... 
  hide details  
Reply
post #2 of 11
this is not going to beat me.
Edited by andrewmchugh - 11/5/11 at 8:18pm
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #3 of 11
Quote:
Originally Posted by Boris4ka View Post
Here's my code: http://pastebin.com/x8XKSufP

I need to format the output into proper binary format. For example, if you enter a decimal "64" I need it to output "0100 0000", NOT 1000000. I've been trying to figure this out for over an hour. Please help.
You can make several char arrays...and then in between each char array just add a space in your print statement.

0-128 in decimal will go from......0 to 1000 0000


129-32768 in decimal will go from 1000 0001 to 1000 0000 0000 0000

Code:

if(input >= 0 && input < 128){
       char[] chars1 = output_string.toCharArray(); //converts string to char array
       char[] chars2 = new char[4]; //char array for first 4 bits
       char[] chars3 = new char[4]; //char array for last 4 bits
        int j = 4;
       for(int i = 0; i < 4; ++i){
             chars2[i] = chars1[i];
             chars3[j] = chars1[j];
             ++j; 
        }
        System.out.println(chars2 + " " + chars3);
}

Edited by surfbumb - 11/5/11 at 8:25pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #4 of 11
Code:
public static int[] getBinaryArray(int decimal){
        
    System.out.println("decimal "+decimal);
    
    int result = decimal;
   
    
    int length=0;
    while(result != 0){        
        result /= 2;
        length++;
    }
    
    
    if(decimal%2==0)
    {
    length++;
    }

   
    return new int[length];

    } //close getBinaryArray method
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #5 of 11
Thread Starter 
Quote:
Originally Posted by andrewmchugh View Post
Code:
public static int[] getBinaryArray(int decimal){
        
    System.out.println("decimal "+decimal);
    
    int result = decimal;
   
    
    int length=0;
    while(result != 0){        
        result /= 2;
        length++;
    }
    
    
    if(decimal%2==0)
    {
    length++;
    }

   
    return new int[length];

    } //close getBinaryArray method
That still doesn't work. If you enter 10 it gives 01010 instead of 1010
über pwnage v2
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4.4GHz @ 1.36v Asus P8Z68-V LX EVGA GTX 970 SuperClocked ACX2.0 4GB G.SKILL Ripjaws X 2x8GB DDR3 @ 1648MHz 
Hard DriveOptical DriveCoolingOS
840EVO 250GB / 3TB+2TB+320GB Bar / 750GB Cav Blk Samsung DVDRW Hyper 212+ Windows 10 Pro x64 
MonitorKeyboardPowerCase
U2412M + X191W Azio MGK1 Corsair RM650X Centurion 5 with acoustic foam 
MouseMouse PadAudio
MX518 SteelSeries Xonar DG + Koss PortaPro + Logitech X-540 + Bey... 
  hide details  
Reply
über pwnage v2
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4.4GHz @ 1.36v Asus P8Z68-V LX EVGA GTX 970 SuperClocked ACX2.0 4GB G.SKILL Ripjaws X 2x8GB DDR3 @ 1648MHz 
Hard DriveOptical DriveCoolingOS
840EVO 250GB / 3TB+2TB+320GB Bar / 750GB Cav Blk Samsung DVDRW Hyper 212+ Windows 10 Pro x64 
MonitorKeyboardPowerCase
U2412M + X191W Azio MGK1 Corsair RM650X Centurion 5 with acoustic foam 
MouseMouse PadAudio
MX518 SteelSeries Xonar DG + Koss PortaPro + Logitech X-540 + Bey... 
  hide details  
Reply
post #6 of 11
Quote:
Originally Posted by Boris4ka View Post
That still doesn't work. If you enter 10 it gives 01010 instead of 1010
Yeah, that code will only ensure that you have an even number of binary digits.

edit: actually it doesn't ensure that there are an even number of digits, i'm not sure what it does...

If you want a number of binary digits always divisible by four try something like

Code:
if(length % 4 != 0)

length += (4 - (length % 4));

Edited by morethantoast - 11/5/11 at 8:46pm
eyefinity
(22 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K EVGA E685 Radeon HD 6970 Radeon HD 6970 
RAMRAMHard DriveCooling
G.Skill SNIPER G.Skill SNIPER Crucial M4 128GB 6970 EK Water Block Acetal + Nickel 
CoolingCoolingCoolingOS
Swiftech MCR320-QP Radiator Swiftech MCP655 Pump EK Supreme HF CPU Block Windows 7 Ultimate 
MonitorMonitorMonitorKeyboard
Dell E228WFP Dell E228WFP Dell P2210 G510 
PowerCaseMouseMouse Pad
TX850 HAF932 BE G500 XTRAC PADS Ripper 
Audio
G930 
  hide details  
Reply
eyefinity
(22 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K EVGA E685 Radeon HD 6970 Radeon HD 6970 
RAMRAMHard DriveCooling
G.Skill SNIPER G.Skill SNIPER Crucial M4 128GB 6970 EK Water Block Acetal + Nickel 
CoolingCoolingCoolingOS
Swiftech MCR320-QP Radiator Swiftech MCP655 Pump EK Supreme HF CPU Block Windows 7 Ultimate 
MonitorMonitorMonitorKeyboard
Dell E228WFP Dell E228WFP Dell P2210 G510 
PowerCaseMouseMouse Pad
TX850 HAF932 BE G500 XTRAC PADS Ripper 
Audio
G930 
  hide details  
Reply
post #7 of 11
I would think of calling a method a certain number of times that returns new char arrays....divisibility by four.

Code:
double t = string.length()/4;
int len = string.length();
int count = 0;
while(t > 0){

   System.out.print(split(len, count) + " ");  //if length is 1-4 returns 1 char array.....5-8....2 char arrays....9-12....3 char arrays...and so on.
   len = len - 4;
   count += 4;
   --t;
}
Code:
      public char[] split(int length, int count){
              
                char[] chars = original_string.toCharArray();
                char[] chars2 = new char[4];
             
                int i = 4;
                int v = 0;
                while(i != 0){
                      chars2[v] = chars[count];
                      ++count;  
                      ++v;
                      --i;
               }

      return chars;
}
that should do it.
Edited by surfbumb - 11/5/11 at 8:54pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #8 of 11
You cant really do anything about it, its because 0-64 1-32 2-16 3-8 4-4 5-2 6-1 (then the while conition is false.)

try..

Code:
public class wat {


public static int[] decimalToBinary (int decimal) {

int result = decimal;


ArrayList<Integer> binaryAr= new ArrayList<Integer>();

boolean flop = false;

while(result != 0){


if(result % 2 == 0){
binaryAr.add(0, 0);
flop = false;
}

else{
binaryAr.add(0, 1);
flop = true;
}

result /= 2;


} //close WHILE

if(flop&&(binaryAr.size()%2==1))
binaryAr.add(0,0);


int[] binary = new int[binaryAr.size()];

for(int i = 0; i < binaryAr.size(); i++)
binary[i]=binaryAr.get(i);


return binary;

} //close decimalToBinary method

//Main method
public static void main (String[]args){

//initialize the input scanner
Scanner input = new Scanner(System.in);

int getDecimal = input.nextInt();

int[] binary = decimalToBinary(getDecimal);


for(int i = 0; i < binary.length; i++){

System.out.println(i + " " + binary[i]);

} //close FOR

} //main method

}

Edited by andrewmchugh - 11/5/11 at 9:01pm
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
^3
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel Core i7 5930K  EVGA x99 Micro EVGA GTX 980 SC ACX 16GB Corsair DDR4 Vengeance LPX Black PC4-21300 
Hard DriveCoolingOSMonitor
500GB Samsung 840 EVO H105 Win 10 ASUS ROG PG279 
KeyboardPowerCase
Ducky Shine 1 MX-Red Corsair AX 860i  Fractal Design Node 804 Black 
CPUGraphicsRAMHard Drive
Core i7 @ 2.6  GT650 16GB of 1600 MHz DDR3L SDRAM 256GB SSD 
OSMonitorKeyboard
Mac OSX Lion Retina Display US layout 
  hide details  
Reply
post #9 of 11
If you're only trying to format the output, why not traverse the array and output each binary digit individually? You could probably put a: if i%4==0, output a space, then continue to traverse the array as you were.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
1100t Asus m4A78T-E HIS 5850 4G 
Hard DriveOS
60 SS, + 3T Windows 7 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
1100t Asus m4A78T-E HIS 5850 4G 
Hard DriveOS
60 SS, + 3T Windows 7 
  hide details  
Reply
post #10 of 11
Quote:
Originally Posted by Oaff View Post
If you're only trying to format the output, why not traverse the array and output each binary digit individually? You could probably put a: if i%4==0, output a space, then continue to traverse the array as you were.
This.

And if you need a fixed format (for example, u want exactly xxxx yyyy zzzz but instead you get xx yyyy zzzz), then you can create a padder as well. You can just see what the length of the array is and then pad the rest with 0s based on the difference.

You'd probably want it to only output a space if i > 4 and i%4 = 0 otherwise you'll get a space when i=0.
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Formatting binary output from an array in Java