char数组转成dword
一般来说,直接移位拼装即可。但是我理解,按照你的描述,貌似你的DWORD长度超过了cpu的寄存器的长度。如果是后者的话,不妨这样做:
struct _dword{
word lo; // 假设cpu寄存器长度16位,word长度16位
word hi;
};
struct _dword a;
char b[4] = {0x12, 0x34, 0x56, 0x78};
a.lo = b[0] & 0x00FF + (b[1] & 0x00FF)
char *转换Variant
看下边这个吧,刚才那个似乎不对,先声明一个_variant_t类型的对象,然后条用如下函数。
就是这样:_variant a; a.SetString("source string");这是函数的说明_variant_t::SetStringMicrosoft Specific void SetString( const char* pSrc ) throw( _com_error );Parameter pSrc pointer to the character stringRemarksConverts an ANSI character string to a Unicode BSTR string and assigns it to this _variant_t object._variant_tMicrosoft Specific A _variant_t object encapsulates theVARIANT data type. The class manages resource allocation and deallocation, and makes function calls to VariantInit and VariantClear as appropriate.#include Compiler COM Support Class OverviewConstruction_variant_t Constructs a _variant_t object. OperationsAttach Attaches a VARIANT object into the _variant_t object. Clear Clears the encapsulated VARIANT object. ChangeType Changes the type of the _variant_t object to the indicated VARTYPE. Detach Detaches the encapsulated VARIANT object from this _variant_t object. SetString Assigns a string to this _variant_t object. Operatorsoperator = Assigns a new value to an existing _variant_t object. operator ==, != Compare two _variant_t objects for equality or inequality. Extractors Extract data from the encapsulated VARIANT object. 不要用哪个空间了,直接用下边的代码进行串口操作吧。你要是非得用,我可以给你发过去,需要找下具体是怎么写的。
** 打开端口名称 ** */ string it_comm("\\\\.\\"); try { it_comm.append("COM1"); hCom = CreateFile(it_comm.c_str() , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , NULL ); if (hCom == INVALID_HANDLE_VALUE) { throw bad_exception((it_comm + ":打开串口失败").c_str()); } //设置波特率、校验位等 commcfg.dcb.BaudRate = uiBaudRate; commcfg.dcb.ByteSize = 8; commcfg.dcb.Parity = NOPARITY; commcfg.dcb.StopBits = ONESTOPBIT; commcfg.dcb.fBinary = TRUE; commcfg.dcb.fDtrControl = DTR_CONTROL_DISABLE; commcfg.dcb.fRtsControl = DTR_CONTROL_DISABLE; //CommConfigDialog(it_comm->c_str(), NULL, &commcfg); /* * * 应用新的串口配置 */ fSucc = SetCommConfig(hCom, &commcfg, (DWORD)sizeof(commcfg));/* * * 发送数据 */ fSucc = WriteFile(hCom , buf , (DWORD)buf_length , &lenw , NULL ); if (!fSucc) { throw bad_exception((szCommPortName + ":发送失败").c_str()); }/* * * 读取数据 */ fSucc = ReadFile(hCom , buf , (DWORD)buf_length , &lenr , NULL ); if (!fSucc) { throw bad_exception((szCommPortName + ":读取失败").c_str()); }。
【100分求解】 “char *”转换为“TCHAR * (提供所有代码 帮整理下
/ 2::MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, NULL );/ 1; int iTextLen; //:delete [] psz, 0, sizeof( char ) * ( iTextLen + 1 ) ); wide char to multi char iTextLen = WideCharToMultiByte( CP_ACP, NULL, NULL ), 0;/delete[] pElementText; return pElementText;}#elsechar* ANSIToUnicode( const char* str ) /,(unicodeLen+1)*sizeof(wchar_t)); ; pUnicode = new wchar_t[unicodeLen+1]; memset(pUnicode;/rt = ( wchar_t* )pUnicode; // pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, str, (LPWSTR)pUnicode, unicodeLen ):#ifdef UNICODEwchar_t* ANSIToUnicode( const char* str ) ///string strText;}char* UnicodeToANSI( const wchar_t* str ) /, -1, NULL; /. ANSI to Unicode{ int len = 0; 这里改成; len = strlen(str); char *pUnicode; pUnicode = new char[len+1]; memset(pUnicode,0,len+1); pUnicode = new char[len+1];/SetDlgItemText(IDC_EDIT_RECV声明, NULL, 0 ); wchar_t * pUnicode, pElementText, iTextLen; return pUnicode; strcpy_s(pUnicode,len+1,str). Unicode to ANSI{ char* pElementText; 1. ANSI to Unicode{ int len = 0; len = strlen(str);free(psz),比如你给的例子; char *pUnicode,0;// /char* UnicodeToANSI( const char* str );#endif定义, psz); / / ::WideCharToMultiByte( CP_ACP, 0, str, -1;/strText = pElementText;}#endifPS:TCHAR* psz = ANSIToUnicode(pBuf);/::MultiByteToWideChar( CP_ACP, 0, str, -1,你自己试,有错自己改吧 补充一下,你给的函数里申请内存用的new,所以释放不能用free,要用delete; int unicodeLen = ;char* UnicodeToANSI( const wchar_t* str );}char* UnicodeToANSI( const char* str ) { int len = 0; len = strlen(str):随便写的,手头没工具,我没测试; strcpy_s(pUnicode,len+1,str); memset(pUnicode,0,len+1);#elsechar* ANSIToUnicode( const char* str ); return pUnicode, 0;delete pUnicode; return pUnicode:#ifdef UNICODEwchar_t* ANSIToUnicode( const char* str )。
char *转换Variant
看下边这个吧,刚才那个似乎不对,先声明一个_variant_t类型的对象,然后条用如下函数。
就是这样:_variant a; a.SetString("source string");这是函数的说明_variant_t::SetStringMicrosoft Specific void SetString( const char* pSrc ) throw( _com_error );Parameter pSrc pointer to the character stringRemarksConverts an ANSI character string to a Unicode BSTR string and assigns it to this _variant_t object._variant_tMicrosoft Specific A _variant_t object encapsulates theVARIANT data type. The class manages resource allocation and deallocation, and makes function calls to VariantInit and VariantClear as appropriate.#include ** 打开端口名称 ** */ string it_comm("\\\\.\\"); try { it_comm.append("COM1"); hCom = CreateFile(it_comm.c_str() , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , NULL ); if (hCom == INVALID_HANDLE_VALUE) { throw bad_exception((it_comm + ":打开串口失败").c_str()); } //设置波特率、校验位等 commcfg.dcb.BaudRate = uiBaudRate; commcfg.dcb.ByteSize = 8; commcfg.dcb.Parity = NOPARITY; commcfg.dcb.StopBits = ONESTOPBIT; commcfg.dcb.fBinary = TRUE; commcfg.dcb.fDtrControl = DTR_CONTROL_DISABLE; commcfg.dcb.fRtsControl = DTR_CONTROL_DISABLE; //CommConfigDialog(it_comm->c_str(), NULL, &commcfg); /* * * 应用新的串口配置 */ fSucc = SetCommConfig(hCom, &commcfg, (DWORD)sizeof(commcfg));/* * * 发送数据 */ fSucc = WriteFile(hCom , buf , (DWORD)buf_length , &lenw , NULL ); if (!fSucc) { throw bad_exception((szCommPortName + ":发送失败").c_str()); }/* * * 读取数据 */ fSucc = ReadFile(hCom , buf , (DWORD)buf_length , &lenr , NULL ); if (!fSucc) { throw bad_exception((szCommPortName + ":读取失败").c_str()); }。 转载请注明出处51数据库 » char**转dword