Row size ratio between java and C ++

I am working on a specific application created in Java. The java level talks about the C ++ layer, which performs the logic of generating sql queries from the database and returns the result back to the Java level.

With a simpler example:

On the java side

nameField = new JTextField(20) //20 chars max length
name = t.getText() // name is sent to CPP layer

At the CPP level, the name from the java level is accepted and stored in the local variable cppName. I am confused by the declaration of variables used in the CPP layer. Most of them are declared as follows:

char cppName[20*4+1]

I want to know the value 20 * 4 + 1 here. The reason for declaring all the variables on the cpp side with a size like javaSize * 4 + 1.

+3
source share
3 answers

java- UNICODE? , char UNICODE, 4:1. (+1) .

, 4 , 4 char s, ++ Java, char - ++ ( '\0'), 20*4+1.

+2

UTF-8, 4 . CPP , , , , .

BTW Java String UTF-16, , 65535 .

http://java.sun.com/developer/technicalArticles/Intl/Supplementary/

Java 4 UTF-8.

StringBuilder sb = new StringBuilder();
sb.appendCodePoint(Character.MAX_CODE_POINT);
System.out.println(sb.toString().getBytes("UTF-8").length); // prints 4

char . , 3. 4 ( 4 )

StringBuilder sb = new StringBuilder();
sb.appendCodePoint(Character.MAX_VALUE);
System.out.println(sb.toString().getBytes("UTF-8").length); // prints 3
+2

Java characters are Unicode, so if you want to pass them to C as ascii, you have to use string.getBytes (charset ()) - will give you an array of bytes of the desired encoding. On the C side, you will need to add a terminating null byte - hence +1

+1
source

All Articles