请教关于char型转换成int和short的问题
我有一个存在数据的一个数据数组,以char的形式存放在一个数组中,现以2个字节和4个字节的形式进行查看,如下:
void displayInfo(int byte, int colNum , int count)
//参数说明:byte 以2个字节传入2,4个字节传入4,
colNum显示在talbe表中的每行的列数
count是显示的总的字节数
//内容如下:
{
int colBytes = byte*(colNum-1);//计算每行显示的总的字节数
int rowNum = count/colBytes;//计算行数,通常为2*20或4*10
//模型数据
QPointer model = new QStandardItemModel(/*rowNum, colNum*/);
//设置表头
QStringList header;
header << tr("Line Header") << tr("") << tr("") << tr("") << tr("") << tr("") ;
model->setHorizontalHeaderLabels(header);
//设置模型数据
QStandardItem* item;
QString tmp;
QList rowList;
//获取线头数据
char* rawData = (char*)(&((SegyLineHead*)segy->getLineHead())->sLineHead);//获取线头结构
char* itemData = new char[byte];//存储每一个数据的缓冲区
for(int row = 0; row < rowNum; row ++)
{
rowList.clear();//清除链表中的数据
tmp = QString("Bytes[%1-%2]").arg(row+1).arg(row+colBytes);
item = new QStandardItem(tmp);
rowList.push_back(item);
int length ;//计算每个字符的位置
for(int col = 0; col < colNum-1; col ++)
{
length = row*colBytes + col*byte;//计算每个字符的位置
for(int j = 0; j < byte; j++)
{
itemData[j] = rawData[length + j];//分割指定个字节的数据
}
//short value = *((short*)(itemData));//转换成整数
int value = *((int*)(itemData));//转换成整数
item = new QStandardItem(tmp.setNum(value));
rowList.push_back(item);//加入链表
}
model->appendRow(rowList);//加入一行数据到模型
}
delete []itemData;//释放内存
//关联到视图
ui.tableViewLineHeadInfo->setModel(model);
}
问题:我在转换时如果将其中
item = new QStandardItem(tmp.setNum(value));
rowList.push_back(item);//加入链表
中的value定义成short,将char进行转换在界面上能正确地显示,包括4个字节,但是short占2个字节的内存,为什么能显示4个字节的数据啊?
如果改成int,将char进行转换,能显示4个字节的数据,但是对于2个字节的数据却全显示成了-33751040这种无效数据。请大家看看,谢谢!
同时本人再请教一下我这个功能函数的参数命名,感觉不够准确。