Results 1 to 18 of 18

Thread: Type casting

  1. #1
    kiros88 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default Type casting

    Hi im really new to understanding the conversion of C to java especially with objects and since java doesnt have objects but classes im trying to figure out.

    basically i have a char[1000] in java.
    so its just raw data btu i want to convert it into a class i have.

    Java Code:
    class data{
         char [] name = new char[50];
    
    }
    class arrayofdata{
          data [] d = new data(20)
    }
    os is there a way to get the char[1000] into it i keep thnking something liek this should be possible

    foo [1000];
    arrayofdata gg = new arrayofdata(foo);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Don't try 'converting C to Java'

    Learn Java and produce a program that has the same functionality as the original C program.

    db

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    And Java does have objects.

  4. #4
    kiros88 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Yeah i meant java doesnt do structs like C does. Im just trying hard to basically once i gather a big chunk of data how can i convert that data to a structure would i just have to do it the brute force where every byte.something = what i want it to be or is there a simple method like C

  5. #5
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    do you just want to convert the syntax of C to java? or ....


    Edit : thnx
    Last edited by [RaIdEn]; 08-25-2010 at 12:23 AM.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The opposite. He wants to convert the syntax of C for usage in his Java application.

    @OP: You will be best to brute-force the operation; simply loop through each object in the array and convert it. There is no easy method of conversion of this type as in C/C++.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    i have a char[1000] in java.
    so its just raw data btu i want to convert it into a class
    There is no easy way to "convert" bytes into a class object. It requires a class loader.

    What is you are trying to do? what is in the 1000 characters? Where are they? In a file?

    Java Code:
    something like this should be possible
    
    foo [1000];
    arrayofdata gg = new arrayofdata(foo);
    
    In java:
    char[] foo = new char[1000];
     .. fill the 1000 chars with data ???
    
    class ArrayOfData {
       char[] data;
      // constructor
      ArrayOfData(char[] x) {
        data = x;  // copy ref to array
      }
    }
    
    ArrayOfData gg = new ArrayOfData(foo);  // create arrayOfData object

  8. #8
    kiros88 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry for responding late this array of 1000 chars is basically just 1000 data bytes and were transfering them over with jni from a C applications but we want to put them in a java Class structure that follows the protocols of a wmv file. Like header and type and sound and data load then end of data load.....like thats the structure of a wmv file

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    What java Class structure follows the protocols of a wmv file?
    If you are able to create this object, what would you do with it?
    What methods/classes would use it?

  10. #10
    kiros88 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    okay so im kinda new to java then but couldn't i just create one my self like i would in C then or is there standards to making java classes if i was trying to implement structures. I basically trying to do this so when the C data processes the selection sends back a chunk of data in unsigned chars then I need java to descipher that data for a java application

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    There are 10,000 plus different ways you can make a java class.
    What do you want to do with the class that will hold your data?

    when the C data processes the selection sends back a chunk of data in unsigned chars then I need java to descipher that data for a java application
    What is the java application? What does "descipher" mean?

  12. #12
    kiros88 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    I just want a class to represent a C structure without the extra features java has like i dont want a function in the class just data types and my application is juts basically to have a union of a data structure for java even though java doesnt support union they use hierachy but i thought if i could typecast the bulk of data into a class i created it would work out

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    Why have a class? How about an array?
    What are you going to do with the data in a java program?

  14. #14
    kiros88 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    well the big chuck of data is basically an array of unsigned chars and in the data its giong to be a list of adapters . so thats y if we had a structure we could easily break up the data in parts for each adapter and reconize the first [50] char is one adapter the second [50] char is a different adapter and then list it out in a java gui for the user

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    Sounds like an array would be the way to store it.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I'd store it in an array as Norm says, but I'd stick that array in a class that has the methods that do the work you are describing, since that data and the procesing you describe are intertwined.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    like i dont want a function in the class
    The OP is confused about what s/he wants.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I know.
    The idea of a basic struct-substitute in Java seems to occur a lot, but it's not really the Java Way...:)

    From what I read of the OPs requirements, there's some data coming in that then needs some work done on it to display on a GUI. I say that work code should be part of the same class that holds this data as an attribute. That's what I'd expect to see.

    From their posts, though, I can see they're thinking in terms of having a lump of data that they then pass around...

Similar Threads

  1. Type Casting
    By Shaheen Mohamed in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 08:56 PM
  2. Casting Object to another type
    By green_river48 in forum New To Java
    Replies: 12
    Last Post: 04-03-2010, 11:52 AM
  3. help with type casting.
    By ramsrocker in forum Java Applets
    Replies: 15
    Last Post: 02-27-2009, 12:28 AM
  4. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 08:07 PM
  5. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 01:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •