【发布时间】:2016-10-07 20:05:55
【问题描述】:
我正在使用 Java NDK 以使用 C 结构从“bin”文件中提取一些信息。我必须在 for 循环中提取有关网关的信息。我能够提取信息,但我需要连接结果并添加点 . 以打印最终 IP 并将其转换为 char* 或 string。
我使用了reinterpret_cast,但它不起作用。
如何将 IP 地址转换为字符串?
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
//Gateway.info[4] is unsigned char type
int config_len = sizeof(ptr->Gateway.info);
for (int i = 0; i < config_len; i++) {
int number = ptr->Gateway.info[i];
}
LOGD("GATEWAY %d",number);
【问题讨论】:
-
LOGD使用的是printf(or one of its siblings) 吗?那么将char打印为小整数的格式是"%hhd"。或"%hhu"为unsigned char。 -
是的,它使用 printf 格式。
-
问题在于
reinterpret_cast的使用,因为C语言没有这个特性。也许你应该正确更新你的语言标签。 -
哪种语言 C 或 C++?在 C++ 中,您可以使用
std::ostringstream,这不是 C 语言中的。 -
@ThomasMatthews
c++。我使用了这两种方法,但是当我打印字符串为空时。 - 方法 1std::string sName(reinterpret_cast<char*>(ptr->Gateway.info));- 方法 2int number; string Result; ostringstream convert; convert << Number; Result = convert.str();
标签: c++ android-ndk java-native-interface