3 Replies - 898 Views - Last Post: 14 June 2011 - 09:04 PM Rate Topic: -----

#1 litedrive   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Converting a C Coded App to Java - Proper Methods

Posted 14 June 2011 - 07:58 PM

FYI I've been programming for, like, six months.

And I've been wanting to convert a Ragnarok Sprite Viewer application (which has been coded in C/C++) to Java. At first I decided to give it a go in C sharp. Then I decided to try Java, since both languages are relatively easy to switch to and from.

On my second try, I made more progress, of course, but this time I've created myself a procedural programmed mindfuck of a cluster and have realized that the only real way to do this properly in Java is by converting it via OOP methods.

There is a problem, though: it's hard to distinguish the differences between arrays and pointers in C/C++, because even though the array bracket symbols ("[]") are typically used, I'll often find arrays declared like so, in addition to the former method:

array*;
//as opposed to
array[];



Other issues exist too, such as c methods/functions with strict, primitive return types (such as unsigned chars) being able to return as explicitly casted objects.

for example:

//example of casted reverse_palette function, which is supposed to return
//unsigned char...
sprite->palette =(SpritePalette *) reverse_palette (palette, 1024, &palette_size);
sprite->palette_size = palette_size;



//structs for individual reference:
typedef struct {
	unsigned char *data;
	int len;
	int width;
	int height;
} SpriteImage;


typedef struct {
	unsigned char b;
	unsigned char g;
	unsigned char r;
	unsigned char unused;
} SpritePalette;


typedef struct {
	char *filename;
	unsigned int nimages;
	SpriteImage *images;
	unsigned int palette_size;
	SpritePalette *palette;
} Sprite;



//the logic behind the casted method. 
static unsigned char * //<--as you can see...
reverse_palette (unsigned char *palette, int palettelen, int *returnsize)
{
	StrBuf *retbuf;
	unsigned char *ret;
	int x;

	retbuf = strbuf_new ();
	for (x = 0; x < palettelen; x += 4) {
		unsigned char tmp[4];

		tmp[0] = palette[x + 2];
		tmp[1] = palette[x + 1];
		tmp[2] = palette[x];
		tmp[3] = '\0';
		strbuf_append (retbuf, tmp, 4);
	}

	ret = retbuf->str;
	if (returnsize) *returnsize = retbuf->len;
	strbuf_free (retbuf, 0);
	return ret;
}



I'm not looking for someone to tell me how to code this with object-oriented design methodology; I'm just looking for a good understanding between the differences of the two languages (besides the fact that C isn't object-oriented and Java is), as well as a guideline to follow when encountering specific problems such as these.

Is This A Good Question/Topic? 0
  • +

Replies To: Converting a C Coded App to Java - Proper Methods

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Converting a C Coded App to Java - Proper Methods

Posted 14 June 2011 - 08:33 PM

A lot of questions, a few hints:
- Java does not have unsigned data type
- if you work with plain ISO-Latin Ascii Java char can be considered as byte else they are 2 bytes (UTF-8)
- Java hides pointer from the user but all Java objects are actually * to that object (but you cannot perform arithmetic on those pointers)
- Java does not have struct just build a class containing as instance variables the elements of your C/C++ struct
Was This Post Helpful? 0
  • +
  • -

#3 litedrive   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Converting a C Coded App to Java - Proper Methods

Posted 14 June 2011 - 09:00 PM

Thanks for the reply.

Still, I'm actually well aware of all that, except for what you stated about the ISO-Latin ASCII set.

I'm talking about hardcore differences, like the ridiculously unfathomable cast I just showed you with the method example I posted.

Are method returns able to be commonly explicitly cast in C, even with data types which have no implicit relativity to each other? This just doesn't make sense to me, for some reason. Seriously, if someone could explain this, I would really appreciate it.

I'll even go so far as to say that my main focus is really regarding that issue, along with Java's answer unsigned chars in C...which, I always thought was a byte. Still, I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Converting a C Coded App to Java - Proper Methods

Posted 14 June 2011 - 09:04 PM

Yes Java has a byte datatype which C/C++ does not have.
You can most of of the times cast basic datatype to another datatype.
For Objects, they have to be son/father of the other ones and Object is the father of all classes in Java
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1