Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++ C+中的字节翻转数据+;只返回零_C++_Gcc_Endianness - Fatal编程技术网

C++ C+中的字节翻转数据+;只返回零

C++ C+中的字节翻转数据+;只返回零,c++,gcc,endianness,C++,Gcc,Endianness,我在windows上使用GCC编译器(代码块),我得到的数据来自UNIX机器,所以都是用big-endian编写的。我必须先把它换给小恩迪安,然后才能给我们。我会被封死的,但我不能让它工作。使用 temp = ntohl(fileBuf[N*i+j]); 或 只返回零。我知道这是不正确的 传入的数据只是一个32位整数的字符串(美国部分地区的高程数据)。非常感谢您的帮助 编辑:这里很新,我不确定代码是否有用 typedef unsigned char BYTE int main() {

我在windows上使用GCC编译器(代码块),我得到的数据来自UNIX机器,所以都是用big-endian编写的。我必须先把它换给小恩迪安,然后才能给我们。我会被封死的,但我不能让它工作。使用

temp = ntohl(fileBuf[N*i+j]);

只返回零。我知道这是不正确的

传入的数据只是一个32位整数的字符串(美国部分地区的高程数据)。非常感谢您的帮助

编辑:这里很新,我不确定代码是否有用

typedef unsigned char BYTE
int main()
{
    long int temp;
    int i,j;
    ofstream myFile;
    myFile.open("fixed4.txt");
    const char *filePath = "input2";
    BYTE *fileBuf;
    FILE *file = NULL;
    if ((file = fopen(filePath, "rb"))==NULL)
       cout<< "File could not be opened successfully" << endl;
    else
        cout << "File Opened successfully" << endl;

    long fileSize = getFileSize(file);
    fileBuf = new BYTE[fileSize];
    //BYTE *flipped;
    //flipped = new BYTE[fileSize];

    fread(fileBuf, fileSize, 4, file);

    for (i=0; i<N; i+=1){
        for (j=0; j<N; j+=1){
            temp = _byteswap_ulong(fileBuf[N*i+j]);
            grid[i][j]=binaryToBase10(temp);

// more code here but it isn't important....
typedef无符号字符字节
int main()
{
长内温;
int i,j;
流文件;
myFile.open(“fixed4.txt”);
const char*filePath=“input2”;
字节*fileBuf;
FILE*FILE=NULL;
if((file=fopen(filePath,“rb”))==NULL)

不能你没有给我们太多的东西,所以这是一个水晶球猜测

fileBuf
的类型是
char*
unsigned char*
。因此,传递给
ntohl
的值是位置(i,j)处的单字节,而不是(i,j)处的32位整数

解决该问题的一种方法(有更好的方法)是:

ntohl( *(uint32_t*)&fileBuf[N*i+j] )

ntohl
经过了很好的测试,所以很可能问题出在您提供给它的数据中。您确定
filebuf[N*i+j]
真的是正确的数据吗?
printf(“%x%x\N”,filebuf[N*i+j],ntohl(filebuf[N*i+j]))你是对的,问题是在FielBuf中。这对于16位交换来说是完美的,但是它并没有正确地读取32位的数据。例如,它是全零读的。这也很可能是未定义的行为。最好是把一个整数向量读入右AWA。Y
ntohl( *(uint32_t*)&fileBuf[N*i+j] )