728x90
외부 DLL을 사용하면 문자열을 주로 char* 타입으로 전달한다.
이때 인코딩 타입을 알 수 없기에 Visual studio에서 사용되는 CP949로 바로 받으면 한글이 깨지는 현상이 발생하며
프로젝트 타입에 따라 모든 문자가 깨질 수 있다.
이때 우선 데이터를 정상적으로 수신하였는지 확인하기 위해서는 char*으로 넘어온 값을 hex로 변환해보는것이다.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring> // For CString
#ifdef _MSC_VER // Include only if compiling with Microsoft Visual C++
#include <atlstr.h>
#else
typedef std::string CString; // Define CString as std::string for other compilers
#endif
void convertToHex(const char* input, CString& output) {
std::ostringstream hexStream;
```
// Iterate through each character in the input array
for (size_t i = 0; input[i] != '\\0'; ++i) {
hexStream << std::hex << std::uppercase << std::setfill('0') << std::setw(2)
<< static_cast<int>(static_cast<unsigned char>(input[i]));
}
// Assign the result to the CString
output = CString(hexStream.str().c_str());
```
}
int main() {
// Input char array
const char input[] = "Hello, World!";
```
// Output CString to hold the hexadecimal representation
CString hexOutput;
// Convert to hex
convertToHex(input, hexOutput);
// Print the result
std::cout << "Hexadecimal representation: " << hexOutput << std::endl;
return 0;
```
}
그냥 사내 레거시 호환성을 위해 CString을 쓰긴했는데 일반적으론 std::string로 처리하는것이 좋을것이다.
출력된 값을 로그로 찍으면 끝!!
* 주의할점은 visual studio에선 Little Endian 방식으로 값이 보이게 될 것이다
보낸쪽이 서버거나 서버 데이터를 바로 by pass하는 식의 코드라면 Big Endian으로 전송하고 있을 수 있으니 주의한다.
728x90
'운동하는 개발자 > c++' 카테고리의 다른 글
MFC] 프로세스에 연결할 수 없습니다. 지정된 파일을 찾을 수 없습니다. (0) | 2024.10.11 |
---|---|
C++로 Windows 환경에서 코드 동작 시간 체크 (feat. GetTickCount64의 오류) (0) | 2024.03.27 |
Visual studio Stack overflow (Stack Reserve Size) (0) | 2023.11.29 |
Beakjoon] 절댓값 힙 구현하기 (백준 11286 코테) = 우선순위 (0) | 2023.11.18 |
LNK2038: mismatch detected for ‘_ITERATOR_DEBUG_LEVEL': value ‘0’ doesn’t match value ‘2’ (0) | 2023.08.13 |