C++ conversion from char* to byte array [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would appreciate if someone could give me an example of how to convert char *variable to byte array and vice-versa in C++.
Thanks

If std::vector<char> is what you're after, then it's simply:
std::vector<char> byteArry(charPtr, charPtr + sizeOfCharPtr);
and the other way:
const char* charPtr = byteArry.data();

Have you tried anything you self, maybe google?
Anyways a char *variable is a char pointer, so you will need to know the size of the data and create a byte array in the same size.
After that you can do a memcpy

Related

How to fill a std::vector<int64_t> with text [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a std::vector<int64_t> myVec in which I want to store a string (text). My understanding is that i cannot reserve space and write to myVec.data(), as this would be undefined behavior. What would be the non-hackish way to do this? I'm assuming the last int64_t will have to be filled with padding zeroes.
I'm using C++14.
If you are constrained to vector the only solution is resize() to required size and strcpy/memcpy into data()
Why would you store text in int?
Is your intention to store digits made from string?
If so, you shall:
std::vector<int64_t> myVec;
myVec.push_back(atoll("100"))

Appending bits to increase the size of a char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way I can append the size of a particular variable? For instance, if I have:
static_cast<char>(0x0147)
an 8 bit char and I want to increase it to say 16 bits without changing the data type, is that possible?
No.
The size of the object is not only related to its type; the size is defined by the type.
You cannot change one and not the other.
Just initialize a new int16_t from this char if that's what you want.
Or, you could have a vector<char> and add new elements to this collection as needed.
(Shifting has nothing to do with it; that's about transforming data.)

Convert const char* to std::string without constructors [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I always wondered why one can't just convert null-terminated C string into std::string(without constructors, writing helper functions and other workarounds)? It appears to be a simple and overly commonplace problem, where you have, let's say argv[1] null-terminated string as an argument passed to your program(or any other naturally occurred C string) and you need to pass it's value to a function directly.
I.e
How do I wrap the cstr in-place without allocating new string object?
This is the answer as best I can tell.
const char * cstr = "Hello World.";
...
..myfunc(cstr);

TXT file to char array and char array to TXT file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new to C++ and wanted to do the following thing: consider a txt file that looks like this for example:
+-+-+-+-+
+-+-o-+-o
+-o-+-+-+
+-+-+-+-+
Now I want to take the content of this file and copy it into an array of the same size and do the other thing around aswell. Thank you for your help, explaining your answer would be extra amazing :)
Using build-in arrays for variable sized things is a bit tricky. The easy way is to use a suitable std::vector<char> instead:
std::ifstream in(from_filename);
std::vector<char> array{std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>()};
// ...
std::ofstream out(to_filename);
std::copy(array.begin(), array.end(),
std::ostreambuf_iterator<char>(out));

How to allocate 100 bytes of memory [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I come across this question in an interview. I answered
char *p = new char[100];
which is wrong according to interviewer.
Can anyone tell me what is the correct way, and what exactly interviewer was expecting?
Your answer is correct. However, it requires manual memory management and is thus error-prone. A more C++-y way would be
std::vector<char> buffer(100);
Or indeed, if the number 100 is a compile-time constant:
std::array<char, 100> buffer;
// or
char buffer[100];
Finally, if we are really interested in low-level memory management, here is another way:
std::allocator<char> alloc;
char* buffer = alloc.allocate(100);
Without knowing what sits in his head, he might not know that char is guaranteed to have size equal to 1 in C++ (ยง5.3.3/1), and was expecting something like:
void* mem = malloc(100);
Still, in C++ vector would probably be preferred.
Or maybe he didn't want you to use dynamic allocation at all?