in Education by
I want to store the hex values into a string, but I don't know to do that when my string is not giving me the hex values when it is printed out. I'm pretty sure it has something to do with hex, but I don't know how to get those int values that print out the correct hex values to be stored into a string without it being changed. I tried different ways of manipulating this and searched on the web but have not found much of a solution in solving this. #include #include #include #include #include #include #include using std::cout; using std::endl; using std::string; using std::hex; using std::stringstream; using namespace std; int main(){ string s2 = "HelloWorld"; cout << "string: " << s2 << endl; cout << "hexval: "; vector<int> character; // converting each character to its ascii value string bytes; for(int i = 0; i < s2.size(); i++) { character.push_back(int(s2[i])); bytes = to_string(character.at(i)); cout << hex << character.at(i) << " "; cout << bytes << endl; } cout << endl; cout << bytes << endl; return 0; } Here is the output that 'bytes' my string is printing out: 48 72 65 101 6c 108 6c 108 6f 111 57 87 6f 111 72 114 6c 108 64 100 Left is the hexadecimals and right is the string. Two different values. How can I store these hexadecimals that is being converted from a string be stored into a string as a hexadecimal value? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I see 2 different ways: The first one uses a char array and writes to it with sprintf with %X. The second way uses a stringstream and streams the int values into it with the hex specifier. You can get the string with the .str() method of stringstream. #include #include #include #include #include #include #include using std::cout; using std::endl; using std::string; using std::hex; using std::stringstream; using namespace std; int main(){ string s2 = "HelloWorld"; cout << "string: " << s2 << endl; string result; for(int i = 0; i < s2.size(); i++) { char buffer[20]; sprintf(buffer, "%X ", s2[i]); result += buffer; } cout << "hexval1: " << result << endl; stringstream res; for (int val : s2) res << hex << val << " "; cout << "hexval2: " << res.str() << endl; return 0; }

Related questions

0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have written a piece of code to validate a keyword, it validates and makes sure that the word is 5 letters long and ... the problem, as it works fine without it. The code: cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to convert the bytes value back to string? I have used binascii.b2a_qp() method, ... doing it manually. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    y = " \{ Hi\} {0} " print y.format(43) it's showing an error: Key Error: Hi\\ I want to print the output from the above syntax: {Hi} 43 Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I need a way to remove the first character from a string which is a space. I am looking for a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I want an efficient way to append one string to another in Python. var1 = "foo" var2 = "bar" var3 = var1 + ... built-in method to use? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Can someone tell me any methods to read every line of a file in Python and store each line as an element in a ... till the end of list. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to delete some certain characters that used wrongly in a string. "........I.wanna.delete. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How do you import a plain text file as a single character string in R? I think that this will probably ... is probably unstable too. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I'm using Python to open a text document: text_file = open("Output.txt", "w") text_file.write("Purchase Amount: ... how to do this? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    What's the simplest way to count the number of occurrences of a character in a string? e.g. count the number of ... had a little lamb' Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm looking through the Apple's Vision API documentation and I see a couple of classes that relate to text ... overview of Vision. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript ... '\ 'def_id=' + def_id Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...