Swig 创建Java实用程序函数以将无符号字符数组转换为字符串

Swig 创建Java实用程序函数以将无符号字符数组转换为字符串,swig,Swig,SWIG将下面的无符号字符数组(“id”)转换为短[]。在C端,sender_id_t_结构的id属性是指向包含字母数字数据(如“TEST123-E”)的数组的指针。我想您可以循环使用short[](sender_id_t_.getId())并将每个元素转换为一个字符,然后连接起来构建一个Java字符串。我想知道他们是否是处理这种情况的更好更简单的方法 C头文件: #define SAMPLE_ID_SIZE_V1 32 #define SAMPLE_ID_SIZE SAMPLE_ID_SIZE

SWIG将下面的无符号字符数组(“id”)转换为短[]。在C端,sender_id_t_结构的id属性是指向包含字母数字数据(如“TEST123-E”)的数组的指针。我想您可以循环使用short[](sender_id_t_.getId())并将每个元素转换为一个字符,然后连接起来构建一个Java字符串。我想知道他们是否是处理这种情况的更好更简单的方法

C头文件:

#define SAMPLE_ID_SIZE_V1 32
#define SAMPLE_ID_SIZE SAMPLE_ID_SIZE_V1

typedef unsigned char sample_id_v1_t[SAMPLE_ID_SIZE];
typedef sample_id_v1_t sample_id_t;

struct sample_sender_id_t_ {
    sample_id_t           id;
    uint32_t              idx;
};
typedef struct sample_sender_id_t_ sample_sender_id_t;
%rename (Sample) sender_id_t_;
struct sender_id_t_ {
    unsigned char id_v1_t[32]      id;
    uint32_t   phy_idx;
};
 [exec] test_wrap.c: In function `TestJNI_Sample_1id_1set':
 [exec] test_wrap.c:826: error: cast specifies array type
 [exec] test_wrap.c:829: error: incompatible types in assignment
 [exec] test_wrap.c:832: error: `jarr2' undeclared (first use in this function)
SWIG.i:

#define SAMPLE_ID_SIZE_V1 32
#define SAMPLE_ID_SIZE SAMPLE_ID_SIZE_V1

typedef unsigned char sample_id_v1_t[SAMPLE_ID_SIZE];
typedef sample_id_v1_t sample_id_t;

struct sample_sender_id_t_ {
    sample_id_t           id;
    uint32_t              idx;
};
typedef struct sample_sender_id_t_ sample_sender_id_t;
%rename (Sample) sender_id_t_;
struct sender_id_t_ {
    unsigned char id_v1_t[32]      id;
    uint32_t   phy_idx;
};
 [exec] test_wrap.c: In function `TestJNI_Sample_1id_1set':
 [exec] test_wrap.c:826: error: cast specifies array type
 [exec] test_wrap.c:829: error: incompatible types in assignment
 [exec] test_wrap.c:832: error: `jarr2' undeclared (first use in this function)
例外情况:

#define SAMPLE_ID_SIZE_V1 32
#define SAMPLE_ID_SIZE SAMPLE_ID_SIZE_V1

typedef unsigned char sample_id_v1_t[SAMPLE_ID_SIZE];
typedef sample_id_v1_t sample_id_t;

struct sample_sender_id_t_ {
    sample_id_t           id;
    uint32_t              idx;
};
typedef struct sample_sender_id_t_ sample_sender_id_t;
%rename (Sample) sender_id_t_;
struct sender_id_t_ {
    unsigned char id_v1_t[32]      id;
    uint32_t   phy_idx;
};
 [exec] test_wrap.c: In function `TestJNI_Sample_1id_1set':
 [exec] test_wrap.c:826: error: cast specifies array type
 [exec] test_wrap.c:829: error: incompatible types in assignment
 [exec] test_wrap.c:832: error: `jarr2' undeclared (first use in this function)

它被视为一个
short
数组,因为该类型的
无符号
,这使得最大值太大,无法放入(有符号的)
字节
字符
。如果要强制将其视为
字符串
,可以使用
%apply
来使用正常的
字符串
类型映射,例如:

%module test

%rename (Sample) sender_id_t_;
%apply char * { unsigned char id[32] };

struct sender_id_t_ {
    unsigned char id[32];
    uint32_t   phy_idx;
};

(我不得不在你的
结构中修改一些语法,这有点奇怪)。

在C头文件中,结构对我来说有点奇怪(包括上面的)。我可能没有正确的SWIG.I发送方id结构定义。当我按照您的建议尝试%apply char*时,我得到了一个类型兼容性错误,完整堆栈也在上面。根据我提供的C头文件,您知道正确的SWIG.i结构吗?@c12-使SWIG接口中的
struct
的内容与headerfile匹配-该错误是因为SWIG.i中的
id
与我怀疑的headerfile非常不同。