0

我在 Windows 上使用 GCC 编译器(代码块),我得到的数据来自 UNIX 机器,所以它都是大端的。我必须先把它换成小端序,然后才能使用它。我会被拦住,但我无法让它发挥作用。使用

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

或者

_byteswap_ulong(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....
4

1 回答 1

2

你没有给我们太多继续,所以这是一个水晶球猜测。

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

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

ntohl( *(uint32_t*)&fileBuf[N*i+j] )
于 2012-07-17T17:39:56.127 回答