CString或const char数组转byte数组(unsigned char数组)

#include "StdAfx.h"
#include 
#include 
#include 
#include 
using namespace std;

#pragma comment(lib,"ws2_32.lib")

//int转byte数组
void  intToByte(int i,byte *bytes,int size = 4)
{
	//byte[] bytes = new byte[4];
	memset(bytes,0,sizeof(byte) *  size);
	bytes[3] = (byte) (0xff & i);
	bytes[2] = (byte) ((0xff00 & i) >> 8);
	bytes[1] = (byte) ((0xff0000 & i) >> 16);
	bytes[0] = (byte) ((0xff000000 & i) >> 24);
	return ;
}

int main(void)
{
	int iValue=0;
	CString strTemp="ABCDEFAB";
	//const char* str=(LPCTSTR)strTemp;
	byte* bBuffer=new byte[4];
	//sscanf(strTemp.GetBuffer(0),"%x",bBuffer);//直接转换会有小尾端问题
	//sscanf((LPCTSTR)strTemp,"%x",&iValue);
	//sscanf(strTemp.GetBuffer(0),"%x",&iValue);
	sscanf(strTemp,"%x",&iValue);//强制转换为const char*
	//sscanf("ABCDEFAB","%x",&iValue);
	//iValue=htonl(iValue);//小尾端转大尾端
	intToByte(iValue,bBuffer,4);
	system("pause");
	return 0;
}


 

你可能感兴趣的:(CString或const char数组转byte数组(unsigned char数组))