hey i would like to know how you could cast an Int array in C++ to an byte array and what would be the declaration method. I would appreciate if it is simpler and no use of pointers. thanks for the comments
-
1Please, look at stackoverflow.com/help/how-to-ask and at stackoverflow.com/help/mcve first.Daniel Langr– Daniel Langr2018-03-29 09:33:18 +00:00Commented Mar 29, 2018 at 9:33
-
[Integer to byte Array conversion Code][1] [1]: stackoverflow.com/a/5585683/5250973Jay Dangar– Jay Dangar2018-03-29 09:35:13 +00:00Commented Mar 29, 2018 at 9:35
-
1You will need to be more specific about your requirements. What do you envision the end result looking like? What have you tried so far?Lightness Races in Orbit– Lightness Races in Orbit2018-03-29 09:44:16 +00:00Commented Mar 29, 2018 at 9:44
Add a comment
|
1 Answer
This solution is a bit less convenient but maybe a bit more understandable from your perspective:
std::array<int, 3> arr_ints = {1, 2, 3};
std::array<unsigned char, 3> arr_bytes;
for(unsigned i=0; i<arr_ints.size(); ++i)
arr_bytes[i] = static_cast<unsigned char>(arr_ints[i]);
2 Comments
user2328447
0 | arr_ints[i]does not make much sense, does it? Is it intended to suppress a cast warning? Then a real cast would be much better.Paul
Changed it to an explicit cast!