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...
As you donesn't provide details about the transmission between java and CI 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.