C++ reverse memcpy
- 前言
- 基于QT的测试样例
- 结果
前言
C++ 实现翻转memcpy
基于QT的测试样例
代码如下:
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QDebug>using namespace std;void *reversememcpy(void *out, const void *in, int char_num);int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);int in[4] = {1,3,5,7};qDebug()<<in[0]<<""<<in[1]<<""<<in[2]<<""<<in[3];char *buf = new char[16];memcpy(buf, in, 16);int *out = new int[4];reversememcpy(out, buf, 16);qDebug()<<*out<<""<<*(out+1)<<""<<*(out+2)<<""<<*(out+3);int *out2 = new int[4];reversememcpy(out2, in, 16);qDebug()<<*out2<<""<<*(out2+1)<<""<<*(out2+2)<<""<<*(out2+3);return a.exec();
}void *reversememcpy(void *out, const void *in, int char_num)
{int typeSize = sizeof(out); char *o = (char *)out;char *i = (char *)in;for (int n = 0; n < char_num/typeSize; n++){for (int m = 0; m < typeSize; m++){o[n*typeSize+m] = i[char_num-(n+1)*typeSize+m];}}return (void *)o;
}