Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C和Python中的数据编码_Python_C_Encoding - Fatal编程技术网

C和Python中的数据编码

C和Python中的数据编码,python,c,encoding,Python,C,Encoding,我遇到了以下问题。我已经使用C将一些双类型数据写入二进制文件,现在我想使用Python读取它们。当我使用python函数时 with open("test.dat","rb") as dfile: data = dfile.read() 它给了我 b'\x00\x00\x00\x00\x00\x00\x00\x08?\x00\x00\x00\x00\x00\x04@\x00\x00\x00\x00\x00\x00\n@\x00\x00\x00\x00\x00\x00\x00\x11@'

我遇到了以下问题。我已经使用C将一些双类型数据写入二进制文件,现在我想使用Python读取它们。当我使用python函数时

with open("test.dat","rb") as dfile:
    data = dfile.read()
它给了我

b'\x00\x00\x00\x00\x00\x00\x00\x08?\x00\x00\x00\x00\x00\x04@\x00\x00\x00\x00\x00\x00\n@\x00\x00\x00\x00\x00\x00\x00\x11@'

所以我尝试使用
data.decode()
进行解码,然后它给了我解码错误。我想这是因为我使用了错误的编码类型。但是我尝试了
ascii
utf-8
,但都没有成功。因此,我的问题有两个方面:

  • 如何在不知道编码类型的情况下读取二进制文件

  • 由于我在用c编写二进制文件时没有给出编码类型,c是否对数据进行编码?如果是,那将是什么样的编码类型

  • 仅供参考,我最初用来编写二进制文件的代码是

    #include <stdio.h>
    
    int main(){
      double buffer[4]= {1.5, 2.5, 3.25, 4.25};
      FILE *ptr;
    
      ptr = fopen("test.dat", "wb");
      fwrite(buffer,sizeof(buffer),1,ptr);
      printf("%ld\n",sizeof(buffer));
    
      return 0;
    }
    
    #包括
    int main(){
    双缓冲区[4]={1.5,2.5,3.25,4.25};
    文件*ptr;
    ptr=fopen(“测试数据”、“wb”);
    fwrite(缓冲区,sizeof(缓冲区),1,ptr);
    printf(“%ld\n”,大小为(缓冲区));
    返回0;
    }
    
    您需要将C类型转换为Python类型。在标准库中使用
    struct.unpack

    在本例中,格式字符串是
    dddd
    ,表示4个双精度。当在不同的编译器和机器之间移动C类型时,困难就来了

    import struct
    
    with open('test.dat', 'rb') as dfile:
        data = dfile.read()
    
    result = struct.unpack("dddd", data)
    print(result)
    
    给出一个元组:

    (1.5, 2.5, 3.25, 4.25)
    

    您需要将C类型转换为Python类型。在标准库中使用
    struct.unpack

    在本例中,格式字符串是
    dddd
    ,表示4个双精度。当在不同的编译器和机器之间移动C类型时,困难就来了

    import struct
    
    with open('test.dat', 'rb') as dfile:
        data = dfile.read()
    
    result = struct.unpack("dddd", data)
    print(result)
    
    给出一个元组:

    (1.5, 2.5, 3.25, 4.25)
    

    您可以使用python标准模块:

    输出:

    array('d', [1.5, 2.5, 3.25, 4.25])
    [1.5, 2.5, 3.25, 4.25]
    

    您可以使用python标准模块:

    输出:

    array('d', [1.5, 2.5, 3.25, 4.25])
    [1.5, 2.5, 3.25, 4.25]
    

    如果您愿意使用numpy,请使用:


    您可能会发现numpy数组比普通python类型更易于操作,因为在它们周围形成了庞大的代码生态系统。

    如果您愿意使用numpy,请使用:


    您可能会发现numpy数组比普通python类型更容易操作,因为它们周围有大量的代码生态系统。

    从这个意义上讲,“解码”就是将二进制数据转换为文本。但是,保存在C程序中的数据表示双精度浮点数,而不是文本。您需要将C类型转换为Python类型。在标准库中使用
    struct.unpack
    :除此之外,在C中打印的缓冲区大小应为
    printf(“%zu\n”,sizeof(buffer))“解码”就是将二进制数据转换成文本。但是,保存在C程序中的数据表示双精度浮点数,而不是文本。您需要将C类型转换为Python类型。在标准库中使用
    struct.unpack
    :除此之外,在C中打印的缓冲区大小应为
    printf(“%zu\n”,sizeof(buffer))这假定相同的端点(这往往是硬件的一个特性)。如果查看文档,您会发现可以在7.1.2.1中指定endianness。字节顺序、大小和对齐这就是为什么我在帖子中说,在不同的编译器和机器之间移动C类型时会遇到困难。如您所知,例如,
    sizeof(int)
    未在该语言中指定。这假定相同的endianness(这往往是硬件的一个特性)。如果查看文档,您会发现可以在7.1.2.1中指定endianness。字节顺序、大小和对齐这就是为什么我在帖子中说,在不同的编译器和机器之间移动C类型时会遇到困难。如您所知,例如,
    sizeof(int)
    没有在语言中指定。