我正在尝试使用 NDK 实现 Rot13 算法,并且对于操作字符串缓冲区的所有不同方法,我似乎无法让原始字符串缓冲区在算法进行时获取新值。每次在 linux 上使用 gcc 时,相同的代码都有效。我认为它可能是优化器,但是将 APP_OPTIM := debug 添加到我的 Android.mk 也不起作用。任何想法或帮助都会很棒!
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "FOO", __VA_ARGS__))
jbyteArray Java_NativeUtils_foo(JNIEnv* env, jobject obj, jstring item)
{
int upper, i, a, c;
char p[9];
const char *d;
//char *p = item;
const char *s = (*env)->GetStringUTFChars(env, item, 0);
//len = (*env)->GetStringUTFLength(env, item);
//** make a copy
strncpy(p, s, 8);
p[8] = '\0';
(*env)->ReleaseStringUTFChars(env, item, s);
LOGI("GG1");
LOGI(p);
for (i=0; p[i] != 0; i++) {
LOGI("GG2");
a =~ (int)p[i];
c = ~a-1 / ( ~(a|32) / 13*2-11 ) * 13;
p[i] = (char)c;
//** for log output
d = (const char *)&c;
LOGI(d);
}
LOGI("GG3");
LOGI(p);
//** convert back to a Java type
jbyteArray out = (*env)->NewByteArray(env, 8);
(*env)->SetByteArrayRegion(env, out, 0, 8, (jbyte *)p);
LOGI("GG4");
LOGI(out);
return out;
}
输出:
( 3521): GG1
( 3521): 12345693
( 3521): GG2
( 3521): 1
( 3521): GG2
( 3521): 2
( 3521): GG2
( 3521): 3
( 3521): GG2
( 3521): 4
( 3521): GG2
( 3521): 5
( 3521): GG2
( 3521): 6
( 3521): GG2
( 3521): 9
( 3521): GG2
( 3521): 3
( 3521): GG3
( 3521): 12345693
( 3521): GG4
( 3521): ??
-( 3521): Rot13 in:12345693... out:12345693...
the input is the same as the output and not shifted by 13 ! =============== GCC version
#include <stdio.h>
#include <string.h>
main(int argc, char** argv) {
int upper, i, a, c;
char p[9];
char *in;
in = argv[1];
strncpy(p, in, 8);
p[8] = '\0';
printf("in ...%s... p ...%s...", in, p);
for (i=0; p[i] != 0; i++) {
a =~ (int)p[i];
c = (~a-1 / ( ~(a|32) / 13*2-11 ) * 13);
p[i] = (char)c;
}
printf("out ...%s...", p);
//while(a=~getchar())putchar(~a-1/(~(a|32)/13*2-11)*13);}
}
输出:$./a.out Freddy in ...Freddy... p ...Freddy...out ...Serqql...
$./a.out Serqql in ...Serqql... p ...Serqql...out ...Freddy...