JNI:将矩阵打印到 logcat 不起作用
JNI: Printing Matrix to logcat doesn't work
我一直在尝试通过logcat给出一个矩阵,而使用c++和JNI。我对这些东西完全陌生,所以经过一些研究,我用以下代码尝试了它:
for(int i = 0; i<4; i++){
for (int j = 0;j<4; j++){
float v = Matrix[i][j];
__android_log_write(ANDROID_LOG_INFO , "Matrix: ", (char*)&v);
}
}
但是这种方法只会给我:
07-22 09:21:56.517 14487-14582/name.example I/Matrix:﹕ [ 07-22 09:21:56.517 14487:14582 I/Matrix: ]
我怎样才能显示矩阵里面是什么?
您的问题在以下代码行:
__android_log_write(ANDROID_LOG_INFO , "Matrix: ", (char*)&v);
(char*)&v
将float
(v
)的字节模式重新解释为C字符串,这不起作用(并且也只是偶尔允许)。相反,要将float
转换为字符串,请使用sprintf
或snprintf
,或者如果可能的话,使用std::to_string
(这需要c++ 11):
char str[buffer_size];
snprintf(str, buffer_size, "%f", Matrix[i][j]);
__android_log_write(ANDROID_LOG_INFO, "Matrix: ", str);
或
__android_log_write(ANDROID_LOG_INFO, "Matrix: ", std::to_string(Matrix[i][j]).c_str());
正如在评论中指出的,还有__android_log_print
,它已经集成了printf
的语法:
__android_log_print(ANDROID_LOG_INFO, "Matrix: ", "%f", Matrix[i][j]);
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- JNI:将矩阵打印到 logcat 不起作用