Solved

How to combine two 8bits binary become 16bit binary

Posted on 2002-03-19
3
202 Views
Last Modified: 2010-03-31
import java.io.*;
import java.math.*;

class test {
   
    static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter screen = new PrintWriter(System.out, true);
   
    public static void main(String[] args) throws IOException {
       
        String str;
        char[] charArray;
        String[] subs;
        int[] q;
       
        screen.println("Try System");
        screen.println("Enter a sentences");
        screen.println();
        str = keyboard.readLine();
        //str = str.toLowerCase();        
        char[] array = new char [str.length()];
        StringBuffer buf = new StringBuffer();
        int dis = 1 << 7;
       
        for ( int i = 0; i < str.length(); i++ ) {
           
            array[i] = str.charAt(i);
           
            for (int sh = 1; sh <= 8; sh++) {
               
                buf.append(( array[i] & dis ) == 0 ? '0' : '1');
                array[i] <<= 1;
            }                                                                  
        }                                                                      
        String s = buf.toString();                                              
        System.out.println(s);
}

// what i want is somethin like this
01010011 01100101 01100011 01110010 01100101 01110100 00100001
and taking 8 bit numbers and representing then side by side as one 16 bit number. In this example, there are
7 original characters (Secret!) that from 4 blocks:
0101001101100101   0110001101110010   0110010101110100   0000000000100001
NOTE that the last character is padded with zeros
0
Comment
Question by:lskoong
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6879940
What is the question?
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 300 total points
ID: 6880019
import java.io.*;
import java.math.*;

class test {
   
   static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
   static PrintWriter screen = new PrintWriter(System.out, true);
   
   public static void main(String[] args) throws IOException {
       
       String str;
       char[] charArray;
       String[] subs;
       int[] q;
       
       screen.println("Try System");
       screen.println("Enter a sentences");
       screen.println();
       str = keyboard.readLine();
       //str = str.toLowerCase();        
       char[] array = new char [str.length()];
       StringBuffer buf = new StringBuffer();
       int dis = 1 << 7;
       
       for ( int i = 0; i < str.length(); i++ ) {

           if (str.length() % 2 == 1) {
              for (int sh1=1; sh1<=8; sh1++)
                 buf.append('0');
           }
           
           array[i] = str.charAt(i);
           
           for (int sh = 1; sh <= 8; sh++) {
               
               buf.append(( array[i] & dis ) == 0 ? '0' : '1');
               array[i] <<= 1;
           }

           i++;

           if (i < str.length()) {
             array[i] = str.charAt(i);
             
             for (int sh2 = 1; sh2 <= 8; sh2++) {
               
               buf.append(( array[i] & dis ) == 0 ? '0' : '1');
               array[i] <<= 1;
             }
           }
 
          buf.append(' '); // added for debug
       }                                                                      
       String s = buf.toString();                                              
       System.out.println(s);
   }
}
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6880030
if you have this method:
public static String getBinaryString(byte b) {
    int dis = 1 << 7;
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < 8; i++) {
        buf.append((b&dis)==0?'0':'1');
        b <<= 1;
    }
    return buf.toString();
}

you can extract the bites of the char in this way:
char ch = 'a';
System.out.println(
getBinaryString((byte)(ch & 0x00ff)) + " " + getBinaryString((byte)(ch >> 8)));
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
Core java. Create the class with method that do logic for all elements of collection. 1 68
word0 challenge 3 77
backtracking recursion  code 19 53
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now