gpt4 book ai didi

iphone - 用memcpy反转字节序

转载 作者:行者123 更新时间:2023-12-01 18:33:06 26 4
gpt4 key购买 nike

我正在写一个创建内存WAV文件的方法。文件的前4个字节应包含字符“RIFF”,因此我正在编写以下字节:

Byte *bytes = (Byte *)malloc(len); // overall length of file
char *RIFF = (char *)'RIFF';
memcpy(&bytes[0], &RIFF, 4);

问题在于,由于little-endianness,这会将前4个字节写为“FFIR”。要纠正此问题,我正在这样做:
Byte *bytes = (Byte *)malloc(len); 
char *RIFF = (char *)'FFIR';
memcpy(&bytes[0], &RIFF, 4);

这行得通,但是有没有一种更好的方法让 memcpy反转其写入的字节顺序?

最佳答案

您正在使用指针做一些坏事情(以及一些奇怪但不是错误的事情)。尝试这个:

Byte *bytes = malloc(len); // overall length of file
char *RIFF = "RIFF";
memcpy(bytes, RIFF, 4);

会很好的。

关于iphone - 用memcpy反转字节序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5874311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com