4

I am trying to re-write a program in C to java. I have no experience in C, but some in C++, so I understand some of the pointer/array things. I am slightly confused though...I am given the following code in C:

void ProcessStatus(Char *Stat){
    DWORD relayDate;
    DWORD APIDate;

    version3=false;
    version4=false;
    version9=false;
    tiltAngle = false;
    ver4features=0;
    tooNew=false;
    ProgRev=0;

    switch(Stat[14]){

From what I understand, the function ProcessStatus is passed a pointer to a char; and I'm assuming since in the last line of the code provided Stat[14] is called its within in array.

So what I'm confused about is how I would pass a pointer to a char within an array in Java.

Any help would be appreciated, even if its helping with my understanding of the C code. Thanks.

4
  • the Char* in C is String. So String should work for you in java. Commented Jul 16, 2012 at 13:23
  • 1
    Is the capital 'C' on 'Char' a typo or deliberate - i.e. is it the C built-in-type char or something else? What? Commented Jul 16, 2012 at 13:26
  • 1
    @mihail Not quite. A char * in C can be a string, but can also be a generic array to store binary data. Since char in C is 8 bits, and in java char is 16 bits, the correct conversion is a byte array in java 'byte[]'. Commented Jul 16, 2012 at 13:34
  • Sorry, I wasn't quite descriptive enough. The code actually uses Palm Pilot API/libraries, and the capital C is their definition I'm pretty sure. Either way, its true that the correct conversion is byte[] Commented Jul 16, 2012 at 13:37

4 Answers 4

4

Hard to say whether it's a string or a raw data that has been passed.

In case of string, use Java's built-in String class.

void ProcessStatus( String stat)
{
  ...

  switch ( stat.charAt( 14 ) )
  {
  } 
}

In case of raw data array, use byte array

void ProcessStatus( byte[] stat)
{
  ...

  switch ( stat[ 14 ] )
  {
  } 
}

BTW, C's char data type is translated to byte type in Java. char type in Java denotes a UTF-16 character which is 2 bytes long. byte is exactly what it is (8-bits, signed).

Sign up to request clarification or add additional context in comments.

3 Comments

You mean stat[14] instead byte[14] in the second ProcessStatus() function, right?
Why would it be byte[14]? Isn't byte the type? I think he's correct calling stat[14]. But I'm the OP and really don't know haha, let me know.
@JuiCe: He had "switch ( byte[ 14 ] )" instead of "switch ( stat[ 14 ] )", but already corrected the typo. Now is everything ok! :)
2

Since C does not have a built-in String type, a String in C is represented as an array of chars. Passing a pointer to the zeroth element in an array is equivalent, in C, to passing a reference to the array, which is what I think this code is doing. Essentially, replace char* Stat with String Stat and switch(Stat[14]) with switch(Stat.charAt(14))

Comments

2

First, just to clarify, it's not a char, but a Char; Char with a capital C is some user defined type. It could just be a typedef for char, or it could be something else; we don't know.

Secondly, what's being passed to the method is apparently a pointer to the first element of a block of Char objects. The closest equivalent is a Java array, but depending on how you do this, you may leave the data structure entirely in C, pass the pointer as a long to Java, and let Java manipulate it by calling native functions you provide. A Java array is quite different from an "array" or sequence of contiguously-allocated objects in C.

Comments

2

Most of the time, when you see char * (I don't know what your initially-capped Char is) in C or C++, the Java equivalent would be String or char[]. The former is the string class, the latter is a character array.

The equivalent function signature using String would look like this; we use charAt to access the 15th character:

void ProcessStatus(String Stat){
    // ...
    switch (Stat.charAt(14))
    // ...
}

The equivalent with char[] would look like this (and as it's an array, we index into it as you do with char * or char[] in C):

void ProcessStatus(char[] Stat){
    // ...
    switch (Stat[14])
    // ...
}

4 Comments

char in C is 8 bits wide and char in java is 16 bits wide. If char * is a string in C then you can use String in java, but if char * was used to store raw binary data, byte[] should be used instead of char[].
@FilipePalrinhas: It depends on how the function is being called. If the entire codebase is being translated from C to Java, then you'd use char[]. If the function were going to be called from C, or using 8-bit data for any other reason, then of course you're right that it should be byte[]. From the accepted answer, looks like it's not actually string data, so byte[] would be correct for the OP.
"If the entire codebase is being translated from C to Java, then you'd use char[]". If the data is a string you can use either char[] or String, but the correct and most natural way to do it in java is using String directly.
@FilipePalrinhas: Er, yes, quite. I sort of meant char[] or String as the case may be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.