本文介绍了如何将HEXADECIMAL LPCTSTR转换为BYTE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下功能 void StrToByte2(LPCTSTR str,BYTE * dest) { UINT count = _ttoi(str); BYTE buf [ 4 ] = { 0 }; char string [ 10 ] = { 0 }; sprintf_s(字符串, 10 , % 04d,伯爵); for ( int i = 0 ; i< 4 ; ++ i) { if ((string [i]> = ' 0')&&(string [i] < = ' 9')) buf [i] = string [i] - ' 0'; } dest [ 0 ] =(BYTE)(buf [ 0 ] << 4 )| buf [ 1 ]; dest [ 1 ] =(BYTE)(buf [ 2 ]<< 4 )| buf [ 3 ]; } 如果我在1234(任何数字)上调用此函数,则输出一些12814! struct st { byte btID [ 2 ]; int nID; }; PTR ptr( new st); StrToByte2(strCode,ptr-> btID); 但是当我在任何十六进制前A123上调用此函数时,它总是输出0000. 以下函数用于将dest代码转换回str CString Byte2ToStr( const byte * pbuf) { CString str; str.Format(_T( %02X%02X),pbuf [ 0 ],pbuf [ 1 ]); return str; } 如何将A123转换为字节而不是返回str以显示A123? 请帮忙!! 我的尝试: 尝试下面的功能,但转换回str,有些我看不到A123,但是输了一些随机数,比如1348 void StrToByte2(LPCTSTR str,BYTE * dest) { BYTE buf [ 4 ] = { 0 }; char string [ 10 ] = { 0 }; sprintf_s(字符串, 10 , % 04d,str); for ( int i = 0 ; i< 4 ; ++ i) { if ((string [i]> = ' 0')&&(string [i] < = ' F')) buf [i] = string [i] - ' 0'; } dest [ 0 ] =(BYTE)(buf [ 0 ] << 4 )| buf [ 1 ]; dest [ 1 ] =(BYTE)(buf [ 2 ]<< 4 )| buf [ 3 ]; } 解决方案 根据建议,使用strtol的(通用文本变体): void StrToByte2(LPCTSTR str,BYTE * dest) { LONG u16 = _tcstol(str,NULL , 16 ); dest [ 0 ] =(BYTE)(u16>> 8 ); dest [ 1 ] =(BYTE)(u16); } 或至少,写一个明智的手工制作的。试试 void StrToByte2Alt(LPCTSTR str,BYTE dest []) { for ( size_t b = 0 ; b< 2; ++ b) { dest [b] = 0 ; for ( size_t nb = 0 ; nb< 2; nb ++) { dest [b]<< = 4 ; TCHAR tc = str [ 2 * b + nb]; if (tc> = _T(' 0')&& tc< = _T(' 9') ) dest [b] | = tc - _T(' 0'); else if (tc> = _T(' A')&& tc< = _T(' F')) dest [b] | = tc - _T(' A')+ 10 ; else if (tc> = _T(' a')&& tc< = _T(' f')) dest [b] | = tc - _T(' a')+ 10 ; else { // 扔掉所有扔掉的东西...... } } } } 或者,使用 C ++ 流: #ifdef UNICODE typedef std :: wistringstream InputStringStream; #else typedef std :: istringstream InputStringStream; #endif // UNICODE BOOL strtobyte2(LPCTSTR str,std :: array< BYTE,2>& b) { UINT16 u16; InputStringStream iss(str); iss>> hex>> U16; if (!iss) return FALSE; b [ 0 ] = static_cast< BYTE>(u16>> 8 ); b [ 1 ] = static_cast< BYTE>(u16); return TRUE; } #include< cstdlib> int main() { char * p = NULL; long n = strtol(abcd,& p,16); printf(%ld \\ n,n); } I have below functionvoid StrToByte2(LPCTSTR str, BYTE *dest){UINT count = _ttoi(str);BYTE buf[4] = { 0 };char string[10] = { 0 };sprintf_s(string, 10, "%04d", count);for (int i = 0; i < 4; ++i){if ((string[i] >= '0') && (string[i] <= '9'))buf[i] = string[i] - '0';}dest[0] = (BYTE)(buf[0] << 4) | buf[1];dest[1] = (BYTE)(buf[2] << 4) | buf[3];}If i call this function on "1234" ( any digits) , dest output some 12814!struct st{ byte btID[2]; int nID;};PTR ptr(new st);StrToByte2(strCode, ptr->btID);but when i call this function on any hexadecimal ex A123 , it outputs 0000 always.Below function is used to convert back the dest code to strCString Byte2ToStr(const byte* pbuf){ CString str; str.Format(_T("%02X%02X"), pbuf[0], pbuf[1]); return str;}How can i get A123 to converted to bytes and than back to str to display A123??Please help!!What I have tried:Tried below function but on convert back to str ,some i cannot see A123, but insted some random number like 1348void StrToByte2(LPCTSTR str, BYTE *dest){BYTE buf[4] = { 0 };char string[10] = { 0 };sprintf_s(string, 10, "%04d", str);for (int i = 0; i < 4; ++i){if ((string[i] >= '0') && (string[i] <= 'F'))buf[i] = string[i] - '0';}dest[0] = (BYTE)(buf[0] << 4) | buf[1];dest[1] = (BYTE)(buf[2] << 4) | buf[3];} 解决方案 As suggested, use the (generic-text variant of) strtol:void StrToByte2(LPCTSTR str, BYTE * dest){LONG u16 = _tcstol( str, NULL, 16);dest[0] = (BYTE) (u16 >> 8);dest[1] = (BYTE) (u16);}or at least, write a sensible hand-crafted one. Tryvoid StrToByte2Alt(LPCTSTR str, BYTE dest[]){for (size_t b=0; b<2; ++b){dest[b] = 0;for (size_t nb=0; nb<2; nb++){dest[b] <<= 4;TCHAR tc = str[2*b+nb];if ( tc >= _T('0') && tc <= _T('9') )dest[b] |= tc - _T('0');else if ( tc >= _T('A') && tc <= _T('F') )dest[b] |= tc - _T('A') + 10;else if ( tc >= _T('a') && tc <= _T('f') )dest[b] |= tc - _T('a') + 10;else{// throw all the throwable here...}}}}or, using C++ streams:#ifdef UNICODE typedef std::wistringstream InputStringStream;#else typedef std::istringstream InputStringStream;#endif // UNICODEBOOL strtobyte2(LPCTSTR str, std::array<BYTE, 2> & b){UINT16 u16;InputStringStream iss(str);iss >> hex >> u16;if ( ! iss) return FALSE;b[0] = static_cast<BYTE>(u16 >> 8);b[1] = static_cast<BYTE>(u16);return TRUE;}#include <cstdlib>int main(){ char * p = NULL; long n = strtol( "abcd", & p, 16 ); printf("%ld\r\n", n);} 这篇关于如何将HEXADECIMAL LPCTSTR转换为BYTE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:01