commit 3edd93faef79e4e6c809d86b2ba3b1096cb279f2 Author: Yu SuiXian <47711102+GengGode@users.noreply.github.com> Date: Fri Nov 17 01:04:03 2023 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5ae3a82 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "iostream": "cpp", + "ostream": "cpp", + "xstring": "cpp" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b95ec72 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.15) + +project(cmake_template) + +set(CMAKE_CXX_STANDARD 17) + +add_subdirectory(source) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt new file mode 100644 index 0000000..9e0f5b7 --- /dev/null +++ b/source/CMakeLists.txt @@ -0,0 +1,3 @@ + +add_executable(cmake_template main.cpp) + diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..0ea2493 --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,65 @@ +#include +#include + +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; +} \ No newline at end of file