简体   繁体   English

如何将字符串从C传递到swift?

[英]How to pass a string from C to swift?

I am using OpenSSL as a library in swift. 我正在使用OpenSSL作为Swift库。 Since it's in C, I need to pass some data from C language back to swift. 由于它在C语言中,因此我需要将一些数据从C语言传递回swift。 The data was an array, but after some research I have learned that I cannot pass an array from C to swift. 数据是一个数组,但是经过一些研究,我知道我无法将数组从C传递到swift。 One way I can think of it is to change the array into a string, an then pass the string back to swift. 我可以想到的一种方法是将数组更改为字符串,然后将字符串传递回swift。 In CI wrote: 在CI中写道:

char teststring()
{
    char c[]={"H","E","L","L","O"};
    printf(c);
    return c;
}

In Swift I wrote: 在Swift中,我写道:

teststring()

The result is that it only prints H . 结果是它只打印H What did I do wrong with it? 我做错了什么? It there a easier way to pass an array from C back to Swift? 有没有更简单的方法将数组从C传递回Swift?

You might want to change 您可能要更改

char c[]={"H","E","L","L","O"};

to

char c[]={'H','E','L','L','O','\0'};

Or 要么

char c[]="HELLO"; 

By char c[]={"H","E","L","L","O"}; 通过char c[]={"H","E","L","L","O"}; an array of strings is getting declared, rather than an array of characters. 声明了一个字符串数组,而不是一个字符数组。 There is a difference between a string and a char. 字符串和字符之间有区别。

A string literal enclosed within a double quotes represents a set of characters. 用双引号引起来的字符串文字代表一组字符。 So, "H" seems to represent a single character to you, but internally it contains an additional character with 'H' and the very necessary '\\0' which is terminating character in C. That was possibly the reason that you were getting a H printed on your console as output. 因此,“ H”似乎代表您一个字符,但在内部它包含一个附加字符'H'和非常必要的'\\0' ,后者在C中终止该字符。这可能是您得到一个“ H作为输出打印在您的控制台上。

This is because as per C11 standard - Section: 6.4.5 String literals which states, 这是因为按照C11标准-第6.4.5节的字符串文字说明,

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz". 字符串文字是由双引号引起的零个或多个多字节字符的序列,例如“ xyz”。 A UTF-8 string literal is the same, except prefixed by u8. UTF-8字符串文字相同,只是前缀为u8。 A wide string literal is the same, except prefixed by the letter L, u, or U. 宽字符串文字相同,除了以字母L,u或U为前缀。

Additionally, section: 6.4.4.4 Character constants states that, 此外,第6.4.4.4节的字符常量指出,

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. 整数字符常量是由单引号引起的一个或多个多字节字符的序列,例如'x'。 A wide character constant is the same, except prefixed by the letter L, u, or U. With a few exceptions detailed later, the elements of the sequence are any members of the source character set; 除了以字母L,u或U为前缀之外,宽字符常量是相同的。除了后面要详细说明的一些例外情况之外,序列的元素是源字符集的任何成员。 they are mapped in an implementation-defined manner to members of the execution character set. 它们以实现定义的方式映射到执行字符集的成员。

Also, 也,

char teststring()

means that teststring is supposed to return a char , and you are returning c which is now a string. 意味着teststring应该返回一个char ,并且您正在返回c现在是一个字符串。 So either change it to return a pointer to char ie 因此,要么更改它以返回指向char的指针,即

char* teststring()

Or, return a char like return c[0]; 或者,返回一个char例如return c[0];

there's a couple things... 有几件事...

First, it'd be a lot easier to format your string this way: 首先,用这种方式格式化字符串要容易得多:

char* c="HELLO";

But, if you want to do it your way still, you need to null-terminate your string, otherwise the computer will not know where the string ends and new memory begins: 但是,如果您仍然想按自己的方式进行操作,则需要对字符串进行空终止,否则计算机将不知道字符串在何处结束,并且新的内存将开始:

char c[]={"H","E","L","L","O"};

becomes: 变成:

char c[]={'H','E','L','L','O',0};

See the single quotes vs the double quotes? 看到单引号还是双引号? The double quotes indicate a whole string (null terminated with zero)... the single quotes indicate a single char. 双引号指示整个字符串(以零结尾的空字符串)...单引号指示单个字符。

So when you did it with double quotes, in characters in memory for the whole thing looked something like this: 因此,当您用双引号引起来时,整个内存中的字符看起来像这样:

H0E0L0L0O0 H0E0L0L0O0

With single quotes, the characters in memory looks like this: 用单引号引起来,内存中的字符如下所示:

HELLO0 HELLO0

The printf function will print until it hits a zero and then quit. printf函数将一直打印到零为止,然后退出。 That's why you were getting just the 'H' printing out. 这就是为什么只打印“ H”的原因。

Yes you have to declare array of char s. 是的,您必须声明char的数组。

So the it will be 所以它将是

Case-1 情况1

char c[]={'H','E','L','L','O','\0');

Here you have created something like array of array of chars. 在这里,您创建了类似字符数组的内容。 Coorect way of doing it would be. Coorect可以做到这一点。

Case-2 案例2

char c[][2]={"H","E","L","L","O"};

But in this case you have to pass c[0] or c[1] in printf . 但是在这种情况下,您必须在printf传递c[0]c[1]

Also note that the first thing can alternatively done using 另请注意,第一件事可以选择使用

Case-3 案例3

   char c[]="HELLO";

This is an array of 6 characters, \\0 included. 这是6个字符组成的数组,包括\\0

And once again you could have also done 再一次,你也可以做

Case-4 案例4

   char *c[]={"H","E","L","L","O"};

Here you have to pass the c[0] or c[1] which will print the string "H" or "E" . 在这里,您必须传递c[0]c[1] ,这将打印字符串"H""E"

Also note that 另请注意

char c[]={'H','E','L','L','O'};

is a valid initialization but this is not null terminated. 是有效的初始化,但不以null终止。 So you can't pass it where a null terminated char array is expected. 因此,您不能在期望以null结尾的char数组的地方传递它。 (like strlen() etc). (如strlen()等)。

Now the case-2, here the strings are modifiable. 现在是case-2,这里的字符串是可修改的。 So you can change the character in them where as the strings in case-4 are not. 因此,您可以更改其中的字符,而case-4中的字符串则不然。 Those are string literals which are non-modifiable. 这些是不可修改的字符串文字。

Simply put if you wanted to have a string which contains those as characters then Case-1 and case-3 are the way for you. 简而言之,如果您想要一个包含那些作为字符的字符串,那么Case-1和case-3是您的理想选择。

c is an array and when you return c you return first element. c是一个数组,当您返回c时,您将返回第一个元素。 You need to return entire array. 您需要返回整个数组。 Check 校验

printf("%s", c);
return *c;

And remember to change signature of your function 并记住要更改功能的签名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM