如何将一片memoryview转换为C字符串(无符号char *)?

wvxvw

下面是我遇到这个问题的代码:

cpdef object encode_file(object fin, str fout):
    if not PyObject_CheckBuffer(fin):
        raise TypeError("fin must follow the buffer protocol")


    cdef Py_buffer in_view
    cdef int ret_code = PyObject_GetBuffer(fin, &in_view, PyBUF_SIMPLE)
    if ret_code < 0:
        raise TypeError("Couldn't get buffer from fin")

    cdef bytes py_filename = fout.encode()
    cdef char* cy_filename = py_filename
    cdef bytes py_mode = "w".encode()
    cdef const char* mode = py_mode
    cdef FILE* fd = fopen(<const char*>py_filename, <const char*>mode)
    if <size_t>fd == 0:
        raise FileNotFoundError(fout)

    cdef unsigned char out_buff[256]
    cdef size_t written = 0
    cdef size_t total_written = 0
    cdef size_t used = 0
    cdef size_t total_used = 0
    cdef size_t pad_start = 80

    cdef unsigned char[:] char_view = fin
    cdef unsigned char* char_slice

    while total_used < <size_t>in_view.len:
        char_view = char_view[used:]
        # This is the place where I get the error
        char_slice = char_view.buf
        used = encode_buffer(
            char_slice,
            in_view.len - used,
            out_buff,
            256,
            pad_start,
            80,
            &written,
        )
        pad_start = 80 - used % 80
        total_written += written
        total_used += used
        if fwrite(out_buff, sizeof(char), used, fd) != used:
            fclose(fd)
            raise Exception(
                "Couldn't write to file: {}. Bytes written: {}".format(
                    fout, total_used,
                ),
            )

    fclose(fd)

    print "used: {}, written: {}".format(used, total_written)
    return total_written

对于一个简单的示例来说,这可能是太多的代码,但是如果您考虑一下,实际上并没有那么多。循环之前的部分用于过滤掉各种边缘情况-对于这个问题,它们不感兴趣。唯一重要的部分是第一个参数必须实现缓冲区协议,第二个参数是文件名。

因此,为了写入文件,我想获取一个内存视图切片,然后将其传递给需要指向的C函数unsigned char为了我的一生,我无法弄清楚如何使用Cython做到这一点...我尝试了上面代码的各种排列,但是,在大多数情况下,

存储临时Python参考的不安全C派生

没有任何暗示它试图产生什么。

上面的代码中也有一些重复,因为我不知道如何使用in_view.buf[x]并使其具有所需的类型。我把它留在这里只是为了表明我也尝试过。


由于Cython内存视图存在错误,因此无法回答类似问题。我将不胜感激。

戴维

由于数据似乎是只读的,因此您似乎无法使用memoryviews,因此可以使用该Py_Buffer对象。数据存储为void*in in_view.buf它转换为一个const char*<const char*>(in_view.buf)您可以n通过简单的指针算法获得th元素(即,只需将其添加n到该值)。

由于使用过,因此PyBuf_SIMPLE您知道项目大小为1并且数组是连续的,但是在更复杂的情况下,您可能需要为此担心。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将包含字符转义序列的字符串转换为char?

如何将字符串=“ \ t”转换为char

如何将2D字符串切片转换为[] byte?

如何将字符串切片转换为符文切片

如何将char转换为字符串..?

如何将整数切片的字符串转换为整数切片?

cgo-如何将字符串转换为C固定的char数组

如何将C#字符串转换为Span <char>?(跨度<T>)

如何将char数组转换为字符串?

如何将一个元素字符串转换为char?

如何将lua字符串转换为C char *?

如何将C ++ / CLI字符串转换为const char *

Python将MemoryView转换为字符串

Postgres:如何将一个bytea字段转换为C内部字符串char *

如何将C ++无符号char *转换为C#?

如何将字符串数组转换为char **

如何将一片无符号整数转换为相同大小的有符号整数?

将十六进制字符串转换为无符号char []

C ++:如何将字符串向量元素转换为无符号字符?

如何在C语言中将无符号char数组转换为字符串?

C ++如何将字符串转换为指针?

如何将int转换为char字符串

如何将XML转换为C ++字符串

C#如何将双精度型转换为带指数符号的字符串

C ++如何将char指针数组转换为字符串数组?

如何以优雅有效的方式将无符号/有符号整数/长整数转换为 C 字符串?

如何将 16 位无符号整数转换为 8 位无符号字符并最终返回无符号字符*?

如何将char字符串转换为int字符串?

如何将任意长度的无符号整数数组转换为以 10 为底的字符串表示形式?