H
Hendrik Maryns
Hi group,
I am working on a JNI project. However, since things didn’t work the
way I wanted them, I wrote some utility functions. One of them is a
function to print the following struct to stdout:
typedef unsigned mgState;
typedef unsigned mgId;
typedef char *mA;
typedef struct mgTreeNode { /* tree node */
mA a; /* alphabet element */
struct mgTreeNode *left, *right; /* successors */
mgId id; /* state space id */
mgState state; /* automaton state */
} mgTreeNode;
Important to notice here is that, although mA is typedef’ed to char*, it
is not a string, but really an array of char, containing 0 and 1 (so
some primitive form of bit vector). However, when printing it, I want
to see '0' and '1', of course.
My first try was the following:
void printTreeNode(mgTreeNode * node, int labelLength) {
int i;
char *label = malloc(labelLength);
for (i = 0; i < labelLength; ++i) {
label = node->a + '0';
}
printf("a: %s, id: %d, state: %d, left: [", label, node->id, node->state);
if (node->left) {
printTreeNode(node->left, labelLength);
} else {
printf("nil");
}
printf("], right: [");
if (node->right) {
printTreeNode(node->right, labelLength);
} else {
printf("nil");
}
printf("]");
fflush(stdout);
free(label);
}
There definitely are more elegant ways to do it, but hey, I’m a real
noob in C.
Now, when trying this out in a C program, it gave the expected results:
a: 000000, id: 0, state: 0, left: [a: 100000, id: 0, state: 0, left: [a:
010001, id: 0, state: 0, … (continued recursively)
However, I wrapped this function with SWIG (http://www.swig.org/), and
when calling it from Java, I get to see the following stuff:
a: 0�\Ӫ*, id: 0, state: 0, left: [a: 0�\Ӫ*, id: 0, state: 0, left: [a:
0�\Ӫ*, …
So some encoding problem is getting in the way. Maybe this is the time
to say that I am on 64-bit Linux, where UTF-8 is the system standard.
Note that those characters are being produced on the C side, the string
is not passed to Java (I tried that as well, with the same result).
I then messed around a bit and came up with the following:
void printTreeNode(mgTreeNode * node, int labelLength) {
int i;
printf("a: ");
for (i = 0; i < labelLength; ++i) {
printf("%c", node->a + '0');
}
printf(", id: %d, state: %d, left: [", node->id, node->state);
if (node->left) {
printTreeNode(node->left, labelLength);
} else {
printf("nil");
}
printf("], right: [");
if (node->right) {
printTreeNode(node->right, labelLength);
} else {
printf("nil");
}
printf("]");
fflush(stdout);
}
You’ll notice a little difference in that the ‘label’ variable is no
longer used and the chars are printed directly.
My question: what the hell causes this strange stuff?
Grateful for any clarifications, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iD8DBQFHmLl1e+7xMGD3itQRAu/kAJ9MEd80nzicyq8r/g96lBcNHB+ctACeKC8K
J5jK2Uy3OYV7OmqOm2Qx8xM=
=UfwX
-----END PGP SIGNATURE-----
I am working on a JNI project. However, since things didn’t work the
way I wanted them, I wrote some utility functions. One of them is a
function to print the following struct to stdout:
typedef unsigned mgState;
typedef unsigned mgId;
typedef char *mA;
typedef struct mgTreeNode { /* tree node */
mA a; /* alphabet element */
struct mgTreeNode *left, *right; /* successors */
mgId id; /* state space id */
mgState state; /* automaton state */
} mgTreeNode;
Important to notice here is that, although mA is typedef’ed to char*, it
is not a string, but really an array of char, containing 0 and 1 (so
some primitive form of bit vector). However, when printing it, I want
to see '0' and '1', of course.
My first try was the following:
void printTreeNode(mgTreeNode * node, int labelLength) {
int i;
char *label = malloc(labelLength);
for (i = 0; i < labelLength; ++i) {
label = node->a + '0';
}
printf("a: %s, id: %d, state: %d, left: [", label, node->id, node->state);
if (node->left) {
printTreeNode(node->left, labelLength);
} else {
printf("nil");
}
printf("], right: [");
if (node->right) {
printTreeNode(node->right, labelLength);
} else {
printf("nil");
}
printf("]");
fflush(stdout);
free(label);
}
There definitely are more elegant ways to do it, but hey, I’m a real
noob in C.
Now, when trying this out in a C program, it gave the expected results:
a: 000000, id: 0, state: 0, left: [a: 100000, id: 0, state: 0, left: [a:
010001, id: 0, state: 0, … (continued recursively)
However, I wrapped this function with SWIG (http://www.swig.org/), and
when calling it from Java, I get to see the following stuff:
a: 0�\Ӫ*, id: 0, state: 0, left: [a: 0�\Ӫ*, id: 0, state: 0, left: [a:
0�\Ӫ*, …
So some encoding problem is getting in the way. Maybe this is the time
to say that I am on 64-bit Linux, where UTF-8 is the system standard.
Note that those characters are being produced on the C side, the string
is not passed to Java (I tried that as well, with the same result).
I then messed around a bit and came up with the following:
void printTreeNode(mgTreeNode * node, int labelLength) {
int i;
printf("a: ");
for (i = 0; i < labelLength; ++i) {
printf("%c", node->a + '0');
}
printf(", id: %d, state: %d, left: [", node->id, node->state);
if (node->left) {
printTreeNode(node->left, labelLength);
} else {
printf("nil");
}
printf("], right: [");
if (node->right) {
printTreeNode(node->right, labelLength);
} else {
printf("nil");
}
printf("]");
fflush(stdout);
}
You’ll notice a little difference in that the ‘label’ variable is no
longer used and the chars are printed directly.
My question: what the hell causes this strange stuff?
Grateful for any clarifications, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iD8DBQFHmLl1e+7xMGD3itQRAu/kAJ9MEd80nzicyq8r/g96lBcNHB+ctACeKC8K
J5jK2Uy3OYV7OmqOm2Qx8xM=
=UfwX
-----END PGP SIGNATURE-----