반응형
------------------------------ 1. std::string -----> BYTE*, BYTE[] ------------------------------
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <string.h>
#include <list>
#include <vector>
#include <algorithm>
#include <cmath>
#include <Windows.h>
#include <numeric>
#include <memory>
int main(
std::string str = "Dynamite";
int size = str.length();
// Keypoint
BYTE* pName = (BYTE*)str.c_str();
for (int i = 0; i < size; i++)
{
printf("%02X ", pName[i]);
}
// OutPut
// Dynamite -> 44 79 6E 61 6D 69 74 65
return 0;
}
|
cs |
------------------------------ 2. 기존에 있던 배열 복사 ------------------------------
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <string.h>
#include <list>
#include <vector>
#include <algorithm>
#include <cmath>
#include <Windows.h>
#include <numeric>
#include <memory>
int main(
std::string str1 = "yamite";
int size1 = str1.length() + 1;
BYTE* pTemp = new BYTE[size1];
BYTE* pName1 = (BYTE*)str1.c_str();
pTemp[0] = 0x44; // D
memcpy(&pTemp[1], pName1, size1);
for (int i = 0; i < size1; i++)
{
printf("%02X ", pTemp[i]);
}
// OutPut
// Dynamite -> 44 79 6E 61 6D 69 74 65
return 0;
}
|
cs |
------------------------------ 3. BYTE[], BYTE* -> std::string ------------------------------
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <string.h>
#include <list>
#include <vector>
#include <algorithm>
#include <cmath>
#include <Windows.h>
#include <numeric>
#include <memory>
int main(
//BYTE buffer[8] = {0x44, 0x79, 0x6E, 0x61, 0x6D, 0x69, 0x74, 0x65};
BYTE* buffer = new BYTE[8];
buffer[0] = 0x44;
buffer[1] = 0x79;
buffer[2] = 0x6E;
buffer[3] = 0x61;
buffer[4] = 0x6D;
buffer[5] = 0x69;
buffer[6] = 0x74;
buffer[7] = 0x65;
std::string str3;
for (int i = 0; i < sizeof(buffer); i++)
{
str3 += buffer[i];
}
std::cout << str3;
// 0x44, 0x79, 0x6E, 0x61, 0x6D, 0x69, 0x74, 0x65 -> Dynamite
// BYTE* BYTE[] 둘다 Dynamite로 똑같은 값 나옴
return 0;
}
|
cs |
반응형
'개인공부' 카테고리의 다른 글
문자열 뒤집기 c++ (0) | 2022.01.14 |
---|---|
[BaeKJoon/Code up/C++] 별 찍기 응용 (0) | 2021.11.14 |
백준 2798(참고) 개인공부 ----c++ (0) | 2021.07.31 |
학생명단 (0) | 2021.07.13 |
c++ 대소문자 변환 (0) | 2021.06.30 |