C++中的字节翻转数据只返回零
byte flipping data in C++ returns only zeroes
我在windows上使用GCC编译器(代码块),我得到的数据来自UNIX机器,所以都是big-endian。我必须先把它换成小endian,然后才能给我们。我会被诅咒的,但我无法让它发挥作用。使用
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....
您没有给我们太多内容,所以这是一个水晶球猜测。
fileBuf
的类型是char*
或unsigned char*
。因此,传递给ntohl
的值是位置(i,j)处的单字节,而不是(i,j)处的32位int。
解决这个问题的一种方法(有更好的方法)是:
ntohl( *(uint32_t*)&fileBuf[N*i+j] )
相关文章:
- 为什么我们使用在C++中返回数据结构的函数?
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- FFmpeg av_read_frame从音频流返回数据包
- 扩展 QStandardItem 不会返回数据
- C++ 根据给定的类型名从变量返回数据
- YAML-CPP的顺序返回数据
- 从全局 CUDA 函数返回数据
- C++ ostream 从对象数组返回数据
- 现代C++.从继续执行的工作线程返回数据结构
- 使用 JNI 通过从C++到 Java 的指针返回数据
- 为什么返回数据结构而不是指针弄乱了我的数据的完整性
- C++Curl,如何读取返回数据
- WSARecv()能否立即返回数据
- 处理 char * 的正确方法在 swig/python 中返回数据
- 如何使用 cppunit 断言宏来检查返回数据类型的预处理器值
- C++函数,不返回返回数据
- 通过.NET中的指针从本机方法返回数据
- 从if语句返回数据
- 更快地以数组形式返回数据的c++接口
- 模板,在派生类中返回数据的通用方法