首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libimobiledevice返回奇怪的字符

libimobiledevice返回奇怪的字符
EN

Stack Overflow用户
提问于 2019-02-12 14:03:47
回答 1查看 109关注 0票数 0

我正在尝试与一个连接的iOS设备配对,并使用libimobiledevice和JNA获取UDID。下面是我声明本机函数的方式:

代码语言:javascript
复制
static native int idevice_new(PointerByReference device, Pointer udid);
static native int lockdownd_client_new(Pointer device, PointerByReference client, String label);
static native int idevice_get_udid(Pointer idevice, StringByReference udid);
static native int lockdownd_query_type(Pointer lockdownd_client, StringByReference type);

为了进行测试,我尝试执行运行命令idevicepair pair所做的事情。

这是我的主要方法:

代码语言:javascript
复制
PointerByReference device = new PointerByReference();
System.out.println("idevice_new error code: " + idevice_new(device, Pointer.NULL));
PointerByReference client = new PointerByReference();
System.out.println("lockdownd_client_new error code: " + lockdownd_client_new(device.getValue(), client, "java"));
StringByReference udid = new StringByReference();
System.out.println("idevice_get_udid error code: " + idevice_get_udid(device.getValue(), udid));
System.out.println("udid: " + udid.getValue());
StringByReference type = new StringByReference();
System.out.println("lockdownd_query_type error code: " + lockdownd_query_type(client.getValue(), type));
System.out.println("lockdownd_query_type: " + type.getValue());
System.out.println("lockdownd_pair error code: " + lockdownd_pair(client.getValue(), Pointer.NULL));

每当我尝试获取任何字符串值时,它都会输出这些奇怪的问号字符:

代码语言:javascript
复制
idevice_new error code: 0
lockdownd_client_new error code: 0
idevice_get_udid error code: 0
udid: ��AZ�
lockdownd_query_type error code: 0
lockdownd_query_type: �HbZ�
lockdownd_pair error code: 0

每次的角色都不同。

以防你看不到它:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-13 11:18:10

UUID每次都会改变,因为它是唯一的!生成的每一个新的都是不同的。

至于奇怪的字符,uuid (以及type)到StringByReference的映射是这里的罪魁祸首,因为您没有以本机存储的格式获取数据。

C中的方法签名(您应该已经将其与问题一起发布)指出,uuid的类型是**char,这是一个指向8位C值字符串的指针。深入研究源代码,它们似乎是字符串表示中的数字0-9和A-F,以及32个字节(不带连字符)或36个字节(带)加上空终止符。(注意,这并不总是显而易见的;它们可以存储在16字节中作为完整的字节值,这实际上是API应该记录的内容。)

在内部,StringByReference类使用Pointer.getString()方法:

代码语言:javascript
复制
public String getValue() {
    return getPointer().getString(0);
}

只有一个偏移量的getString() method使用您的平台的缺省编码,它可能是多字节字符集。这可能与UUID的8位编码不匹配(在您的例子中显然也不匹配)。

您应该将UUID映射为PointerByReference,并使用uuid.getValue().getString(0, "UTF-8")uuid.getValue().getString(0, "US-ASCII")将字符串提取为它们所表示的8位字符。

(或者,您也可以获取一个字节数组并从中创建一个字符串,尽管我不确定您是否会得到32字节或36字节的结果,因此,如果您这样做,请尽情享受。为了获得真正的乐趣,您可以迭代偏移量并逐字节读取,直到得到0。但我离题了。)

type字段执行相同的操作留给读者作为练习。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54643725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档