简体   繁体   English

Dlang字符串转换为char *转换

[英]Dlang string to char * conversion

I've got this code reading from a bin file. 我已经从bin文件中读取了此代码。 the data is held in an array of structs and null terminated My goal is to read data, written in c-code with write (filehandle, (char *)arrayOfCELLs, sizeof(arrayOfCELLs); to a file, into the same structure arrayOfCELLs using D. 数据保存在结构数组中,并且以null终止。我的目标是将数据以c代码write (filehandle, (char *)arrayOfCELLs, sizeof(arrayOfCELLs);并使用write (filehandle, (char *)arrayOfCELLs, sizeof(arrayOfCELLs);到文件,使用相同的结构arrayOfCELLs D.

My problem is that in c I read the file into a struct arr[] via a cast to (char *) 我的问题是在c中,我通过强制转换为(char *)将文件读入struct arr []

read(filehandle, (char *)arrayOfStructs, sizeof(arrayOfStructs)

In DI found out through rcorre that led me to rawRead 在DI中通过rcorre发现了导致我进入rawRead的过程

auto buffer = FileHandle.rawRead(arrayOfStructs)

where the buffer gets the data to the sizeof arrayOfStructs. 缓冲区将数据获取到sizeof arrayOfStructs的位置。

However, here comes my problem, the buffer seems to get the rawdata including nulltermination but all positioned in [0] of the receiving Array, also, I don't understand/know what the datatype it is(CELL[] or string[] or ..... 但是,这里出现了我的问题,缓冲区似乎获取了包括空终止符在内的原始数据,但所有数据都位于接收数组的[0]中,而且,我也不了解/不知道它是什么数据类型(CELL []或string []要么 .....

struct CELL{ char [20] name, int sect, int type ...};
struct CELL [50] arrayOfCELLs;
auto buffer = ..rawRead(arrayOfCELLs)

received data in the buffer is (part of it) this 缓冲区中接收到的数据是(部分)

[CELL("TEST\0feed\0\0\0\0\0\0\0\0\0\0\0", 1, 1,

The first is the name, "TEST" followed by null-termination, then sect = 1, ptyp = 1 . 第一个是名称“ TEST”,后跟零终止,然后是sect = 1,ptyp = 1。 Now the second CELL holds this 现在第二个细胞持有

CELL("\x01\0\0\0\x02\0\0\0N\0\0\0\x04\0\0\0\x02\0\0\0", 65656, 655375,

And that is all wrong, Should be "test" and 2,1, If I do a cat in the terminal I get 这是完全错误的,应该是“测试”和2,1,如果我在终端机上养猫,我会得到

anders$ cat cellspec
TESTfeedOx
atestTailingNx
aOF2 cycl primO?

Question, any ideas why this is and what can I do to get it right? 问题,这是为什么的任何想法,我该怎么做才能正确解决?

What you probably need are calls to toStringz() and fromStringz(). 您可能需要的是对toStringz()和fromStringz()的调用。 (Documentation page: http://dlang.org/phobos/std_string.html ) (文档页面: http : //dlang.org/phobos/std_string.html

Example (from the abovementioned page): 示例(来自上述页面):

import core.stdc.string : strlen;
import std.conv : to;

auto p = toStringz("foo");
assert(strlen(p) == 3);
const(char)[] foo = "abbzxyzzy";
p = toStringz(foo[3..5]);
assert(strlen(p) == 2);

assert(fromStringz(null) == null);
assert(fromStringz("foo") == "foo");

Also, rawRead() returns the same type as the type of the buffer. 另外,rawRead()返回的类型与缓冲区的类型相同。 So, since you have 所以,既然你有

auto buffer = f.rawRead(cellTab);

, then the buffer is already an array of CELL's... You should be able to use it with, say buffer[0].name to grab the name of the first cell... ,则buffer已经是CELL的数组...您应该可以将其与buffer[0].name一起使用,以获取第一个单元格的名称...

Keep in mind that .name is actually char[20]. 请记住,.name实际上是char [20]。 You really want to convert it to a string if you want to do some operations with it. 如果您要对它进行一些操作,则确实希望将其转换为字符串。

I have found out what the problem is with the conversions. 我发现转换存在什么问题。 The long and unsigned long in C is not the same size in Dlang long and ulong C中的long和unsigned long与Dlang long和ulong中的大小不同

Solution/conclusion is when coding in D-lang and using C typed data in files, in order to get the reading correct one needs to beware that D-lang type size might not be equal to C size. 解决方案/结论是在使用D-lang进行编码并在文件中使用C类型的数据时,为了获得正确的读数,需要注意D-lang类型的大小可能不等于C大小。 In my case the code changed to use c_long and c_ulong to get the reading correct. 在我的情况下,代码更改为使用c_long和c_ulong来使读数正确。 After that all was well reading data. 之后,一切都很好地读取了数据。 Happy days 快乐的时光

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

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