GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ByteConvert_cpp/examples/string_to_bytes.cpp at master · SloCompTech/ByteConvert_cpp Expand file tree/ Copy pathstring_to_bytes.cppMore file actions 56 lines (46 loc) · 1.45 KB / Copy pathstring_to_bytes.cppFile metadata and controls56 lines (46 loc) · 1.45 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <cstdint> #include <cstring> #include <string> #include <memory> #include <ByteConvert/ByteConvert.hpp> int main() { // String that we will convert to bytes std::string example_string = "facaceca"; // Array of bytes we expect to get uint8_t expected[4] = {0xfa, 0xca, 0xce, 0xca}; // Convert string to bytes size_t result_size = 0; uint8_t* result = ByteConvert::hex_string_to_block(example_string, &result_size); // Check result if (result_size != 4) { // Error, unexpected size of result delete[] result; return -1; } for (size_t i = 0; i < result_size; i++) { if (expected[i] != result[i]) { // Error, result not expected delete[] result; return -2; } } // Clear result !!! It was allocated to heap so we need to manualy free it !!!!!! delete[] result; // Same as above, just implemented with smart pointers, which is safer std::unique_ptr<uint8_t[]> result1(ByteConvert::hex_string_to_block(example_string, &result_size)); // Check result if (result_size != 4) { // Error, unexpected size of result return -1; } for (size_t i = 0; i < result_size; i++) { if (expected[i] != result1[i]) { // Error, result not expected return -2; } } // No need to free result1, because it clears itself when it goes out of scope // Success return 0; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |