How can I convert a string to a byte array? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I convert a string to a byte array in C++?
For example, "Hello" → 48 65 6c 6c 6f.
I'm trying to pattern search the memory with this byte array after.

std::string::c_str() yields the underlyng c string / byte array.
Also see std::string::c_str() for a list of occasions when the returned pointer might be invalidated (basically everytime you modify the string and of course when the std::string itself is destroyed).
You can create a copy of it using memcpy() if required.

Related

How to assign a string value to integer variable in c++? [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 very basic question how can I assign a string value to a variable that already has integer value. For example, int a = 10 then I need to assign a ='hey'. Is it possible in c++?
I think you are new to C++? In C++ you assign the variable a fixed type (int in your case). It is not possibile to change that type inside the scope. You have to convert a to a string and save them in a new variable. How to convert a string (char* or std::string) you can look here:
Easiest way to convert int to string in C++

C++ String individual characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to make a function that takes the first and last letters of a string, divides them for the modulus and stores them in an array spot related to the modulus number. However I don't know how I would get the first and last letters of the string.
Assuming std::string str, you can use either:
str.front() and str.back() (C++11 and later only).
str[0] and str[str.size()-1], if str is not empty.
You can have the first letter of your string by just using yourString[0] and to get the last latter you can use yourString[yourString.size()-1]
EDIT
As said in the comments, assuming you're using std::string

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.)

c++ binary value for indexed array [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 6 years ago.
Improve this question
I am extremely new to C++ so I'm probably asking a very trivial question, but if you could help that'd be great!
I have an array[n].
Indexed from 0 to some unknown value.
I need to access the index of the array, the n value but I need to do so in binary. I am intending to do a bit reversal on it.
So, if I have an array of 2048 points how do I represent the 1024 array in binary?
If you want to write a value in binary, you can do so in C++14 with
int my_binary_value = 0b01010101;
If you'd like to test a specific bit of an int, you can do that by masking it, i.e.
bool is_bit_4_set = my_binary_value & 0b00001000;

How to get variables to my program vrom text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a file like:
a [able%5:00:00:capable:00] [able] capable
a [abnormal%3:00:00::] [abnormal]
a [absent%3:00:00::] [absent]
a [absolute%3:00:00::] [absolute] perfect or complete
a [abstract%3:00:00::] [abstract] existing only in the mind
a [abundant%3:00:00::] [abundant] plentiful
I want to get first column "avle", "abnormal" etc to my object. How to crop them and how storage them?
Use the string tokenizer, the best/easiest way to split a line into different variables.
char* word = strtok(line," [%:]");
char* word2 = strtok(0," [%:]");
int value = strtoi(strtok(0," [%:]"));
to store there is a vector container, but there can be used any type of arrays, what is more convenient