3

I need to put my Java information in a String so I use the String(byte[] arrB) constructor. Now this information is sent to the C program as a char* type. Now I need to get back original bytes since, from my understanding, they were encoded in the process of creating a Java String.

How can I do that in the C program?

So, on the C side I have these bytes:

7e 53 e9 94 d4 46 f5 7c 66 cf 85 34 18 5a ff 6 2d a3 89 48 d2 e4 46 b8 6b 43 ec 64 3a 67 f9 2 6d 12 ac e7 0 c4 99 52 68 76 76 77 12 2 de 7d 5b e7 4e 5 6 73 f4 fc 91 54 12 71 64 7a 25 3d

They are in a char* but the reach Java as a String and the String is:

7E 53 EF BF BD EF BF BD 46 EF BF BD 7C 66 CF 85 34 18 5A EF BF BD 06 2D EF BF BD EF BF BD 48 EF BF BD EF BF BD 46 EF BF BD 6B 43 EF BF BD 64 3A 67 EF BF BD 02 6D 12 EF BF BD EF BF BD

As you can see there many similarities...

João Rodrigues
  • 191
  • 2
  • 11
  • if you read and write as bytes there is no encoding – Scary Wombat Dec 19 '14 at 02:56
  • 1
    How did you call Java from C? Show us the code. If you obtained a byte[] from the String, you probably specified the encoding (or took the platform's default); you'll need to use that encoding on the C side. – Jeffrey Bosboom Dec 19 '14 at 02:56
  • @ScaryWombat I need to use a String to carry the information from Java to C. @JeffreyBosboom I am developing a program which communicates with Linux `libpam`. I used the `libpam4j` wrapper in order to do that communication. We can pass information from the application to the PAM module by using `pam_conv` (conversations) and the only way is by putting it in a String from the Java side and call a `libpam4j` function. I didn't specify the encoding... – João Rodrigues Dec 19 '14 at 03:01
  • You don't need to do any such thing. You need to send the bytes, as they are. – user207421 Dec 19 '14 at 03:26
  • But the thing is that when I print the sizes in each side, they are different, and the output of the String in Java and the char* in C sometimes isn't "equal" with minor differences. – João Rodrigues Dec 19 '14 at 12:48

1 Answers1

0

As you donesn't provide details about the transmission between java and C I cannot provide you a full response only suppositions. (please provide code)

First points : As Scary Wombat say, if you want to send data to C don't use encoding, juste send bytes.

Supposition 1 : Encoding is not the same

When you are encoding your bytes to String, you will use the default encoding. As the javadoc says :

Constructs a new {@code String} by decoding the specified array of bytes using the platform's default charset

public String(byte bytes[])

If you need to send String to char*, define the encoding to the both sides :

String.getBytes("UTF-8)". 

If your java and c part use different encoding you will get difference in result.

Supposition 2 : Null byte added If the size is different (1 byte), it's probably the null byte at end of your char*.

Supposition 3 : You read some char who are out of your array

How did you do the init of your char* ? Did you any malloc ? A fixed size buffer ? Maybe this is wrong and you read another data, owned by another buffer/variable/....

Supposition 3' : Miss init the char*, you just read initial buffer value.

Don't forget to allocated memory for your struct : Two example to how to allocated struct memory and when

Community
  • 1
  • 1
Manticore
  • 441
  • 5
  • 24
  • I cannot send you the code since I am using a library. All I do is call `r.setResp(new String(respons)); r.write();` where r is `pam_response` type. Then on C I have the same `pam_response` struct where I do: `pam_response *r; char *text; text=r->resp;` The characters seem to be printing the same thing on each side but sometimes in Java the size of the string is lower. – João Rodrigues Dec 19 '14 at 17:27
  • Is difficilt to help you without more detail. We need informations about the struct you use, the string you display and how (maybe the error is here ?) Give two samples code and console print. – Manticore Dec 21 '14 at 08:16
  • I have no time for that right now due to deadlines, I opted to do it in another way by using a database and not passing strings. Thank you. Do you know what should I do with this thread (close it or something)? – João Rodrigues Dec 21 '14 at 17:20