I am trying to just add print statements to my code to find errors, but cannot seem to get integers or bytes or jints or anything other than a String to show in LogCat.
#define LOG_INFO( info ) __android_log_print( ANDROID_LOG_WARN, "NATIVE CODE", info );
LOG_INFO( "c = " + c );
The result for this is just
c =
Does anyone know what I am doing wrong, or if this is possible? I have tried jints, ints, chars, jchars, and bytes ( as the type for c ).
The language is C. C does not have a builtin type for a string; strings are really arrays of
charor pointers tochar. You cannot add neither arrays nor pointers. So there’s no string concatenation by+operator, or by any other operator for that matter. C++ is different, but let’s not go there for now.The way __android_log_print() works is similar to Java’s String.format(). To output a character to LogCat, you use the following call:
If you want to output several variables – say, a char and an int, the call would be:
__android_log_printis a variadic function – it takes a variable number of arguments (but no less than three), with unknown types beyond the first three. The types of the extra arguments are deduced from the format specifiers in the format string – the %-sequences. %c means char, %d means int, %s is char string and so forth, look it up.This is the
printfpattern, used throughout the C world. It does not easily lend itself to macro-izing, unless you’re willing to introduce separate macros for outputting 0, 1, 2, etc. parameters. That way, it might look like this:I’m deliberately avoiding the thorny issue of character type conversion here; for the %c format specifier, the function expects a parameter of type
charwhich is not the same asjchar. For jchar’s that are non-ASCII characters, this snippet will print garbage.Just FYI: the correspondence between Java types and C types is not straightforward, and it’s actually somewhat platform-dependent. For the purposes of printing, you need to carefully watch. So:
jcharis not char; it’sunsigned short. Shortening tocharloses data.jlongis notlong, it’slong long(64-bit integer)jbooleanis signed char (nobooleandatatype in C)jbyteis signed char (nobytedatatype in C)jintisint(on Android)jfloatisfloatjshortisshortjdoubleisdouble