Problem Translate c code into java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem Translate c code into java

  1. #1
    Join Date
    Mar 2005
    Posts
    1

    Problem Translate c code into java

    Hello I have to 'translate' a code into a java code.
    Now I have the some problems:

    In the c code memcpy is used en I found something about how to use memcpy.
    Can someone help me width this because I don' t know how I have to use it.
    http://www.garret.ru/~knizhnik/cons...s/AnyArray.html

    I tried this
    Code:
    public class memcpy 
    { 
        Obj String1; 
        Obj String2; 
     
    public memcpy() 
        { 
        String1 = new Obj(); 
        String2 = new Obj(); 
             
        String1.value="Hallo wat is het toch weer mooi weer vandaag"; 
        String2.value="Hallo wat is het stom weer vandaag"; 
         
        //System.out.println(String1.value); 
    //    System.out.println(String2.value); 
        memcpy(String1.value,3,String2.value,5,13); 
        System.out.println(String1.value); 
        }     
     
    class Obj 
        { 
        public String value; 
        } 
     
    public void memcpy(java.lang.Object dst, int dstPos,java.lang.Object src, int srcPos, int n) 
        { 
      //  System.out.println(String1); 
        } 
          
        public static void main(String[] args) 
        { 
        new memcpy(); 
        } 
    }public class memcpy 
    { 
        Obj String1; 
        Obj String2; 
     
    public memcpy() 
        { 
        String1 = new Obj(); 
        String2 = new Obj(); 
             
        String1.value="Hallo wat is het toch weer mooi weer vandaag"; 
        String2.value="Hallo wat is het stom weer vandaag"; 
         
        //System.out.println(String1.value); 
    //    System.out.println(String2.value); 
        memcpy(String1.value,3,String2.value,5,13); 
        System.out.println(String1.value); 
        }     
     
    class Obj 
        { 
        public String value; 
        } 
     
    public void memcpy(java.lang.Object dst, int dstPos,java.lang.Object src, int srcPos, int n) 
        { 
      //  System.out.println(String1); 
        } 
          
        public static void main(String[] args) 
        { 
        new memcpy(); 
        } 
    }
    My other problem is that the c code contains pointers but I don't know how I have to translate the code and it does the same in my java code as in the c code.

    Here's a little part

    Code:
    unsigned long random(unsigned long v)
     {
     unsigned char *ptr;
     ptr = (unsigned char *)&v
     etc.
     }
     
     unsigned long composiet;
     unsigned char *ptr;
     ptr = (unsigned char *)&composiet
     random(composiet);

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    there is no equivalent to memcpy in Java and nor should there be.

    memcpy is used in C/C++ to quickly copy areas of memory from one place to another.

    You have a similar problem with pointers - they directly access memory ready for maniplulation.

    You need to understand what the C code is trying to do and rewrite this logic in Java - do not attempt to port it line for line as it generally won't work and you'll go mad in the process.

    C and Java may look similar, but are very different languages to code in, primary due to the java sandbox which does not allow direct memory manipulations.
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center