FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ByteConvert_cpp/examples/string_to_bytes.cpp at master · fcccode/ByteConvert_cpp · GitHub
fcccode
/
ByteConvert_cpp
Public
forked from
SloCompTech/ByteConvert_cpp
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ByteConvert_cpp
/
examples
/
string_to_bytes.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (46 loc) · 1.45 KB
Breadcrumbs
ByteConvert_cpp
/
examples
/
string_to_bytes.cpp
Copy path
File metadata and controls
56 lines (46 loc) · 1.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
;
}
Back
|
FazBrowse Home
|
New Git URL