转换 SRTM 数据时的负高度

Negative heights when converting SRTM data

我想从 NASA SRTM 数据集 (http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/Eurasia/) 中获取特定 latitude/longitude 对的高度。

作为初学者,我想从 HGT 文件中获取所有高度:

int totalPx = 1201; //3 degree
char buffer[2];
for (int i=0; i<totalPx; i++)
{
    for (int j=0; j<totalPx; j++)
    {
        int pos = (i * totalPx + j) * 2;

        m_openedFile.seek(pos); //m_openedFile is a Qt QFile
        m_openedFile.read(buffer, 2);
        short h = 0 | (buffer[0] << 8) | (buffer[1] << 0);
        if (h < 0)
            printf("%d", h);
    }
}

这似乎适用于某些坐标(例如,高度值看起来合理),但也有许多负高度值。如何正确读取 SRTM/HGT 文件?

此致,

我想通了:问题是导致错误位移位的是有符号字符而不是无符号字符:

int totalPx = 1201; //3 degree
unsigned char buffer[2];
for (int i=0; i<totalPx; i++)
{
    for (int j=0; j<totalPx; j++)
    {
        int pos = (i * totalPx + j) * 2;

        m_openedFile.seek(pos); //m_openedFile is a Qt QFile
        m_openedFile.read(buffer, 2);
        short h = 0 | (buffer[0] << 8) | (buffer[1] << 0);
        if (h < 0)
            printf("%d", h);
    }
}

char转换为int

时注意符号扩展
// char buffer[2];
unsigned char buffer[2];
short h = buffer[0] << 8 | buffer[1];