Convert C/C++ code to Java
Hi guys, I have to convert C/C++ code to Java, I did something:
*C CODE:
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:
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 :o
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:
Code :
Scanner reader = new Scanner(System.in);
String header = reader.nextLine();
If you want to limit the header's length, add this:
Code :
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);
}
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.
Code :
...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.
Re: Convert C/C++ code to Java
If you want to extract each character, use the .charAt() method
Code :
// get the first character
header.charAt(0);
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
Code :
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
Code :
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!
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:
Code :
int a = Integer.parseInt("123");
// a = 123
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.
Re: Convert C/C++ code to Java
Specify i as a global variable within the codigo class
Re: Convert C/C++ code to Java
:P there are no such things as "global" variables in Java, only object/class fields.
Re: Convert C/C++ code to Java
Quote:
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?
Re: Convert C/C++ code to Java
Fields = data members :P
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?)
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
Quote:
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? :confused:
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
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:
Code :
package messagedecoding;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Header header = new Header();
header.putCharAndShowMessage();
}
}
Code :
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');
}
}
}
Code :
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:
Code :
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!