This commit is contained in:
Yu SuiXian 2023-11-17 01:04:03 +08:00
commit 3edd93faef
No known key found for this signature in database
GPG Key ID: AD62D3EEA62C94B9
5 changed files with 83 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

7
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"files.associations": {
"iostream": "cpp",
"ostream": "cpp",
"xstring": "cpp"
}
}

7
CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project(cmake_template)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(source)

3
source/CMakeLists.txt Normal file
View File

@ -0,0 +1,3 @@
add_executable(cmake_template main.cpp)

65
source/main.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <string>
auto to_hex = [](unsigned char c) -> char
{
if (c < 10)
{
return c + 48;
}
else
{
return c + 55;
}
};
int main()
{
unsigned char mac[6] = {0xa0, 0xa0, 0xb0, 0x03, 0xa0, 0xd0};
unsigned short sum = 0;
for (int i = 0; i < 6; i++)
{
sum += mac[i];
}
// print sum
std::cout << "Sum: " << sum << std::endl;
// to string
std::string macString = "";
// loop through all bytes
for (int i = 0; i < 6; i++)
{
// to HEX
macString += to_hex(mac[i] / 16);
macString += to_hex(mac[i] % 16);
// add colon
macString += ":";
}
// remove last colon
macString.pop_back();
// print mac address
std::cout << "MAC: " << macString << std::endl;
auto sum_to_hex = [](unsigned short s) -> std::string
{
std::string hex = "";
// loop through all bytes
for (int i = 0; i < 2; i++)
{
// to HEX
unsigned char c = s % 256;
hex += to_hex(c % 16);
hex += to_hex(c / 16);
// shift right
s /= 256;
}
// reverse string
std::reverse(hex.begin(), hex.end());
// return hex
return hex;
};
// print sum as hex
std::cout << "Sum as HEX: " << sum_to_hex(sum) << std::endl;
}