Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Convert C/C++ code to Java

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Talking Convert C/C++ code to Java

    Hi guys, I have to convert C/C++ code to Java, I did something:

    *C CODE:

    #include <stdio.h> 
     
    int Bit() { 
       char c; 
       scanf(" %c", &c); 
       return c-'0'; 
    } 
     
    int Len() { 
       int len; 
     
       len = 4*Bit(); 
       len += 2*Bit(); 
       len += Bit(); 
     
       return len; 
    } 
     
    int Code(int len) { 
       int val=0, i, pow2=2, base=0; 
     
          /* read in the bits */ 
       for (i=0; i<len; i++) { 
          val = val*2 + Bit(); 
       } 
          /* val now contains the decimal representation of the bit code */ 
          /* now figure out that code's place int the sequence */ 
       for (i=0; i<len-1; i++) { 
          base += pow2-1; 
          pow2 *= 2; 
       } 
     
          /* if val is all 1's ( (2^n)-1 ), end here, return -1 */ 
       if (val == pow2-1) { 
          return -1; 
       } else { 
          return val + base; 
       } 
    } 
     
     
    int main() { 
       char header[300]; 
       int len; 
       int code; 
     
       while (scanf(" "), 1==scanf("%[^\n]", header)) { 
          while (len = Len(), len) { 
      while (code = Code(len), code != -1) { 
         putchar(header[[color=black]code[/color]]); 
      } 
          } 
          putchar('\n'); 
       } 
     
       return 0; 
    }
    *JAVA CODE:
    package messagedecoding;
    import java.io.DataInputStream;
    import java.io.IOException;
     
    public class Codigo {
        char code;
        public int len;
        public int val = 0;
        public int i;
        public int pow2 = 2;
        public int base = 0;
     
        public int Bit() throws IOException {
            DataInputStream ecode =new DataInputStream(System.in);
              try {
              code = ecode.readChar();
          }
            catch (Exception e) {}
            return code - '0';
        }
     
        public int Len() throws IOException {
            len += 2*Bit();
            len += Bit();
            return len;
        }
     
        public int Code(int len) throws IOException {
            for(i=0; i<len; i++){
                val = val * 2 + Bit();
            }
            for(i=0; i<len-1; i++){
                base += pow2 - 1;
                pow2 *= 2;
                }
            if (val == pow2 - 1){
                return -1;
            } else {
                    return val + base;
                }
            }
        }
     
    package messagedecoding;
     
    import java.io.DataInputStream;
    import java.io.IOException;
     
    public class Main {
        public static void main(String[] args) throws IOException
        {
            Codigo code1 = new Codigo();
            char header[] = new char[300];
            int len;
            DataInputStream input =new DataInputStream(System.in);
              try {
                  char c;
                  int i;
                  for(i=0; i<300; i++){
                      c=input.readChar();
                      header[i]=c;
                  }
     
            int u=-1;
            i=0;
            while(header[i]!='\n'){
                while(code1.Code(code1.Len())!=u){
                    System.out.println(header[code1.Code(code1.Len())]);
                }
                i=i+1;
            }
            System.out.println('\n');
        }
              catch (Exception e){}
        }
    }
    But, it doesn't work, I think the problem is that the code doesn't use the 2nd input 'ecode' ,but I dunno how fix it, if anyone can help me pls? Thks so much.
    Sry for my bad english
    Last edited by helloworld922; November 5th, 2009 at 09:20 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert C/C++ code to Java

    hmm, the one thing I can see is wrong is using DataInputStream to get user input from System.in. This is causing Java to hang because technically, there's no end to the System.in stream. Instead, use the Scanner.

    Also, Java has a "native" String class, I'd strongly recommend that for reading something in. Here's how to read in an entire line of text using the Scanner and String's:

    Scanner reader = new Scanner(System.in);
    String header = reader.nextLine();

    If you want to limit the header's length, add this:

    if (header.length() > MAX_HEADER_SIZE)
    {
         // you'll need to define MAX_HEADER_SIZE, or replace it with an int
         header = header.substring(0,MAX_HEADER_SIZE);
    }
    Last edited by helloworld922; November 5th, 2009 at 09:30 PM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    cristianll (November 5th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Convert C/C++ code to Java

    Thanks!, I didn't know about Scanner, I have a question, I want use char by char of the read line ,i.e.

    ...while(header[i]!='\n'){
                while(code1.Code(code1.Len())!=u){
                    System.out.println(header[code1.Code(code1.Len())]);
                }
                i=i+1;...
            }

    how do that with header.substring? using header.substring(i)?

    Thks.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert C/C++ code to Java

    If you want to extract each character, use the .charAt() method
    // get the first character
    header.charAt(0);

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    cristianll (November 5th, 2009)

  7. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Convert C/C++ code to Java

    Oh yes!, you are right, sry all the questions,Im new in Java and don't remember all functions, so the class Main now is

    package messagedecoding;
     
    import java.util.Scanner;
    import java.io.IOException;
     
    public class Main {
        public static void main(String[] args) throws IOException
        {
            Codigo code1 = new Codigo();
            Scanner reader = new Scanner(System.in); 
            String header = reader.nextLine(); 
              try {
                  String c;
                  int i;
                  {
                      if (header.length() > 300)
    {
     
         header = header.substring(0,300);
     
    }
                  }
     
            int u=-1;
            i=0;
            while(header.charAt(i)!='\n'){
                while(code1.Code(code1.Len())!=u){
                    System.out.println(header.charAt(code1.Code(code1.Len())));
                }
                i=i+1;
            }
            System.out.println('\n');
        }
              catch (Exception e){}
        }
    }

    and in class Codigo

    public class Codigo {
      ...
     
        public int Bit() throws IOException {
     
            Scanner reader = new Scanner(System.in);
            String codigo = reader.nextLine();
     
            -->return codigo.charAt(i) - '0';<--
     
        }...
    here reads a binary code like that 00101000 , I think will be the same declaration like in class Main but I dunno how iterate i for use with Len() function too. Thks for all!

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert C/C++ code to Java

    hmm. i'm going to assume that the method bit() gets a input of 0/1, then converts it to a number, right?

    If so, you can use the .nextInt() method of the Scanner class, and then just see if value inputted is 0 or 1. I'd recommend not having the bit() method read input from System.in, though. I'd just pass it a parameter (a string), and then return the appropriate value. You can parse a string into an int by using the Integer wrapper class:

    int a = Integer.parseInt("123");
    // a = 123

  9. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Convert C/C++ code to Java

    Method Bit() gets an input of 0/1 and,i.e. 10101000, the first call to Bit () will return 1, the 2nd call to Bit() will return 0, the 3rd call to Bit() will return 1, each time that use method Bit() will return the next binary digit.

  10. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Convert C/C++ code to Java

    Specify i as a global variable within the codigo class

  11. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert C/C++ code to Java

    there are no such things as "global" variables in Java, only object/class fields.

  12. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: Convert C/C++ code to Java

    there are no such things as "global" variables in Java, only object/class fields.
    i would like to add.... only objects/class/fields or data members.


    really? is in it!? tnx for that helloworld!! i've learned a new thing from that!
    so any way.. where did that term came from? "global"?

    and about this topic .. whats the purpose of DataInputStream?

  13. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert C/C++ code to Java

    Fields = data members

    Global is a concept when programming in C/C++ (and quite a few other languages) that meant everything had access to that variable. The reason it doesn't show up in Java is because Java is completely object oriented (aka. everything must be inside a class). This is not the case with C/C++.

    You can "mimic" global variables in Java by declaring public static variables in Java (can you see why?)

  14. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Convert C/C++ code to Java

    ahh field is equivalent to data members , but it is mostly called as fields.. ahh ok i got it..


    anyway
    You can "mimic" global variables in Java by declaring public static variables in Java (can you see why?)
    because in some cases , where variables(constant ones) are used by other programs?

  15. #13
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Convert C/C++ code to Java

    I'm aware of that helloworld *blush* lol! No i was being a muppet, I did actually mean a data member

    Chris

  16. #14
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Convert C/C++ code to Java

    Hey, I was out last days and now I come back to this Code, and I have problems, I use prints to control:

    package messagedecoding;
     
    import java.io.IOException;
     
    public class Main {
     
        public static void main(String[] args) throws IOException {
            Header header = new Header();
            header.putCharAndShowMessage();
        }
    }

    package messagedecoding;
     
    import java.io.IOException;
    import java.util.Scanner;
     
    public class Header {
        int i=0,u=-1,len,code1;
        Code code = new Code();
     
        public void putCharAndShowMessage() throws IOException{
            System.out.println("Write characters");
            Scanner reader = new Scanner(System.in);
            System.out.println("1");
            String header = reader.nextLine();
            System.out.println("2");
            if(header.length()>300){
                System.out.println("3");
                header=header.substring(0,300);
                System.out.println("4");
            }
            System.out.println("Write binary code");
            while(header.charAt(i)!='\n'){
            len=(int)code.Len();
            System.out.println("len initialized");
                while(len!=0){
                    System.out.println("len checked");
                    code1=(int)code.Code(len);
                    System.out.println("code1 initialized");
                    while(code1!=u){
                        System.out.println("1's String checked");
                        System.out.println(header.charAt(code1));
                    }
                }
                i+=1;
                System.out.println('\n');
        }
    }
    }

    package messagedecoding;
     
    import java.io.*;
    import java.util.Scanner;
     
    public class Code {
        int len,i,val = 0,base = 0,pow2 = 2,d;
        char c;
     
        public int Bit() throws IOException{
            Scanner reader = new Scanner(System.in);
            c=(char)reader.nextByte();
            System.out.println("a");
             return c-'0';
        }
     
        public int Len() throws IOException{
            len = 4*Bit();
            System.out.println("b");
            len += 2*Bit();
            System.out.println("c");
            len += Bit();
            System.out.println("d");
            return len;
        }
     
        public int Code(int len) throws IOException{
            for(i=0;i<len;i++){
                val = val*2+Bit();
                System.out.println("e");
            }
            for(i=0;i<len;i++){
                base += pow2-1;
                System.out.println("f");
                pow2 *= 2;
                System.out.println("g");
            }
            if(val==pow2-1){
                return -1;
            }else{
                return val+base;
            }
        }
    }

    When run with NetBeans show me this:

    run:
    Write characters
    1
    TAN
    2
    Write binary code
    0
    a
    b
    0
    a
    c
    1
    a
    d
    len initialized
    len checked
    code1 initialized
    1's String checked
    T
    1's String checked
    T
    1's String checked
    T

    I dunno why enter in infinite boucle, never show me the Code() method prints, and need to write the binary code in 1 line, how can do that? and ever the result is t, I dunno what thing is wrong... Thks!
    Last edited by cristianll; November 14th, 2009 at 02:01 PM.

Similar Threads

  1. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  2. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM
  3. Replies: 5
    Last Post: September 6th, 2009, 04:39 AM