Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. C to Java translation

C to Java translation

Scheduled Pinned Locked Moved Java
java
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kujtim Hyseni
    wrote on last edited by
    #1

    Hi, is there a reliable tool which can convert completely a C code to Java. C code contains only bit operations such AND, OR ... XOR, addition, subtraction and like and is not dependent on particular library. The tool should convert the code completely i.e. without my intervention after translation.

    L K T J 4 Replies Last reply
    0
    • K Kujtim Hyseni

      Hi, is there a reliable tool which can convert completely a C code to Java. C code contains only bit operations such AND, OR ... XOR, addition, subtraction and like and is not dependent on particular library. The tool should convert the code completely i.e. without my intervention after translation.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Kujtim Hyseni wrote:

      The tool should convert the code completely i.e. without my intervention after translation.

      It is unlikely that any tool is capable of doing that since there are enough subtle differences in the languages, and coding style to confuse it. If you want a selection of tools that you can try for yourself then Google is your friend.

      Veni, vidi, abiit domum

      1 Reply Last reply
      0
      • K Kujtim Hyseni

        Hi, is there a reliable tool which can convert completely a C code to Java. C code contains only bit operations such AND, OR ... XOR, addition, subtraction and like and is not dependent on particular library. The tool should convert the code completely i.e. without my intervention after translation.

        K Offline
        K Offline
        Kujtim Hyseni
        wrote on last edited by
        #3

        When I translate the C code to C# it works fine because in both C and C# there is unsigned 8 bit integer type "byte" (values from 0 to 255) while in Java "byte" is signed 8 bit integer (values from -128 to 127). What operations might have effect when signed used instead of unsigned?

        L 1 Reply Last reply
        0
        • K Kujtim Hyseni

          Hi, is there a reliable tool which can convert completely a C code to Java. C code contains only bit operations such AND, OR ... XOR, addition, subtraction and like and is not dependent on particular library. The tool should convert the code completely i.e. without my intervention after translation.

          T Offline
          T Offline
          thatraja
          wrote on last edited by
          #4

          You can't expect 100% automatic conversion. Even commercial products offers only 95-99% conversion. In Google you could find few free converters. Ex. C To Java Converter[^] There's a better way if you have more time & resources. Code Rewrite[^]

          thatraja

          Code converters | Education Needed No thanks, I am all stocked up. - Luc Pattyn When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute

          1 Reply Last reply
          0
          • K Kujtim Hyseni

            When I translate the C code to C# it works fine because in both C and C# there is unsigned 8 bit integer type "byte" (values from 0 to 255) while in Java "byte" is signed 8 bit integer (values from -128 to 127). What operations might have effect when signed used instead of unsigned?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            If you use arithmetic operations on them then you will get incorrect results.

            Veni, vidi, abiit domum

            K 1 Reply Last reply
            0
            • L Lost User

              If you use arithmetic operations on them then you will get incorrect results.

              Veni, vidi, abiit domum

              K Offline
              K Offline
              Kujtim Hyseni
              wrote on last edited by
              #6

              How do you mean, can you explain me more ?

              L 1 Reply Last reply
              0
              • K Kujtim Hyseni

                How do you mean, can you explain me more ?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Try this code:

                public static void main(String\[\] args) throws Exception {
                    byte b1 = (byte)129;
                    byte b2 = 4;
                    byte b3 = (byte)(b1 + b2);
                    System.out.println("b1 is: " + b1);
                    System.out.println("b2 is: " + b2);
                    System.out.println("b3 is: " + b3);
                

                If the byte values were unsigned you would expect the answer to be 133, but in fact you will get -123, since the 'real' value of b1 is, as you see, -127.

                Veni, vidi, abiit domum

                K 1 Reply Last reply
                0
                • L Lost User

                  Try this code:

                  public static void main(String\[\] args) throws Exception {
                      byte b1 = (byte)129;
                      byte b2 = 4;
                      byte b3 = (byte)(b1 + b2);
                      System.out.println("b1 is: " + b1);
                      System.out.println("b2 is: " + b2);
                      System.out.println("b3 is: " + b3);
                  

                  If the byte values were unsigned you would expect the answer to be 133, but in fact you will get -123, since the 'real' value of b1 is, as you see, -127.

                  Veni, vidi, abiit domum

                  K Offline
                  K Offline
                  Kujtim Hyseni
                  wrote on last edited by
                  #8

                  Yes, but I am receiving un-correlated values, how to get 133 instead of -123 - I mean what modifications or things I have to do at the end?

                  L 1 Reply Last reply
                  0
                  • K Kujtim Hyseni

                    Hi, is there a reliable tool which can convert completely a C code to Java. C code contains only bit operations such AND, OR ... XOR, addition, subtraction and like and is not dependent on particular library. The tool should convert the code completely i.e. without my intervention after translation.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    I wouldn't think that would work at all for any non-trivial business application. And once translated it would be so mucked up that it would be significantly more costly to maintain.

                    1 Reply Last reply
                    0
                    • K Kujtim Hyseni

                      Yes, but I am receiving un-correlated values, how to get 133 instead of -123 - I mean what modifications or things I have to do at the end?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      You have to get the value as an integer and then mask off the low order 8 bits, to make it into an unsigned value, like this:

                      public static void main(String\[\] args) throws Exception {
                          byte b1 = (byte)129;   // b1 is the original unsigned value, which takes the full 8 bits
                          byte b2 = 4;           // b2 is a normal value
                          int i1 = b1;           // make b1 an integer
                          int i2 = i1 & 0xFF;    // mask off all but the low order 8 bits, i.e. the original byte value
                          int i3 = i2 + b2;      // add the two original values together
                          System.out.println("i1 is: " + i1);  // the negative byte value, same as b1
                          System.out.println("i2 is: " + i2);  // the unsigned value
                          System.out.println("i3 is: " + i3);  // the sum of the two numbers
                      }
                      

                      [edit]Fixed typo; I meant low order 8 bits.[/edit]

                      Veni, vidi, abiit domum

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups