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 3 years ago.
Improve this question
How could I convert the following string:
std::string str = "x89x30x50";
to this following unsigned char array/byte array with escape sequences:
unsigned char char_arr[1024] = "\x89\x30\x50";
If I understand your question correctly you want to split a string of hexadecimal values (prefixed by 'x') into byte values.
You could achieve this by tokenizing the string (with a delimiter of 'x') and converting each token to the byte value:
Something like this:
#include <string>
#include <sstream>
int main()
{
std::string str = "x89x30x50";
unsigned char char_arr[1024] = "";
std::istringstream iss(str);
std::string token;
int i = 0;
while (std::getline(iss, token, 'x'))
{
if (token.empty()) continue;
char_arr[i++] = static_cast<unsigned char>(std::stoi(token, nullptr, 16));
}
//char_arr = {0x89,0x30,0x50,0,0,...}
}
Related
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 months ago.
Improve this question
Let’s say I have 128 and I broke it up into 1, 2 and 8.
Can anyone give me a logic to build the number again from its broken digits.
If you have a vector of ints, you can do:
#include <vector>
#include <cstdio>
#include <cstdlib>
int to_integer( const std::vector<int>& v ) {
int number = 0;
for ( int value : v ) {
number = 10*number + value;
}
return number;
}
int main() {
std::vector<int> vec {1,2,8};
int number = to_integer( vec );
printf( "Number:%d\n", number );
return 0;
}
If it was a string, you could simply use the C library
#include <cstdlib>
...
const char* str = "128";
int number = ::atoi( str );
But that's likely not what you are asking.
You can do the hands-on approach
#include <cstdio>
int to_integer( const char* str ) {
int number = 0;
for ( ; *str != '\0'; str++ ) {
char ch = *str;
number = 10*number + (ch-'0');
}
return number;
}
int main() {
const char* str = "128";
int number = to_integer( str );
printf( "Number:%d\n", number );
return 0;
}
Please note that this routine above is just a minimal, simplistic and it does not check for cases that the C library does as eg: non-numeric charaters, white spaces, null pointer.
However many times we can guarantee all above as a precondition so the above becomes valid production code. I actually use something like that for high speed trading.
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 1 year ago.
Improve this question
I have a wchar_t* in my C code, that contains URL in the following format:
https://example.com/test/...../abcde12345
I want to split it by the slashes, and get only the last token (in that example, I want to get a new wchar_t* that contains "abcde12345").
How can I do it?
Thank you?
In C, you can use wcsrchr to find the last occurence of /:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
int main(void){
wchar_t *some_string = L"abc/def/ghi";
wchar_t *last_token = NULL;
wchar_t *temp = wcsrchr(some_string, L'/');
if(temp){
temp++;
last_token = malloc((wcslen(temp) + 1) * sizeof(wchar_t));
wcscpy(last_token, temp);
}
if(last_token){
wprintf(L"Last token: %s", last_token);
}else{
wprintf(L"No / found");
}
}
Just use std::wstring.
int main()
{
std::wstring input = L"https://example.com/test/...../abcde12345";
auto separatorPos = input.rfind(L'/');
if (separatorPos == std::wstring::npos)
{
std::cout << "separator not found\n";
}
else
{
auto suffix = input.substr(separatorPos + 1);
std::wcout << suffix << L'\n';
}
return 0;
}
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 1 year ago.
Improve this question
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ifstream fin;
char ch[30];
int count = 0;
fin.open("story.txt");
while (!fin.eof())
{
fin>>ch;
if(ch == "the" || ch == "The")
{
count++;
cout<<" "<<ch;
}
}
fin.close();
cout<<" Number of the's in the file : "<<count;
}
Not supposed to use strcompi() function.(or any other functions that could help compare the "ch" with "the")
The count gives zero output because the if condition doesn't work
This is my code, what could be the problem over here.**
You can't compare char arrays with ==, but you can with std::string.
Change
char ch[30];
to
std::string ch;
If you replace char ch[30]; with std::string ch, it looks like it would work. Maybe with some minor adjustments.
The reason why it doesn't work is that ch == "the" tests whether char ch[30], decayed as a pointer, points to the same memory location as the "the" literal. And it doesn't.
If you use a std::string, the operator== for the std::string will be called and it will do the right thing: compare the two strings.
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 new to C++ and I am creating a small program. Part of it I have created a way of converting a char array to set of int's. But i was just wondering if there is a better way (more efficient and/or uses better C++ standards). Like for example using atoi on spliced part of the array for each number for etc.
So I start of reading a set of numbers e.g. "11 2 123 44" from a file into a char * array and now want to convert them into there respective values currently doing it as follows:
//char * char_cipher; //{'1', '1', ' ', '2', ... , '4'}
//int length; //length of char_cipher
string s = "";
vector<int> cipher_int;
for (int i = 0; i <= length; i++) {
if (char_cipher[i] == ' ') {
//create num
cipher_int.push_back(stoi(s));
s = ""; //empty num
}
else {
//add to string
s += char_cipher[i];
}
}
Any help would be much appreciated thanks :)
Your code is pretty close. The problem is that you never push the last number in char_cipher onto cipher_int, because there's no space after it. You need to do an extra check after the loop is done.
for (int i = 0; i <= length; i++) {
if (char_cipher[i] == ' ') {
//create num
cipher_int.push_back(stoi(s));
s = ""; //empty num
}
else {
//add to string
s += char_cipher[i];
}
}
if (s != "") {
cipher_int.push(back(stoi(s));
}
Let the STL do the parsing for you. You can use a std::istringstream for that, eg:
#include <string>
#include <sstream>
#include <vector>
std::string str_cipher; //"11 2 123 44"
std::vector<int> cipher_int;
std::istringstream iss(str_cipher);
int num;
while (iss >> num) {
cipher_int.push_back(num);
}
Alternatively:
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
std::string str_cipher; //"11 2 123 44"
std::vector<int> cipher_int;
std::istringstream iss(str_cipher);
std::copy(
std::istream_iterator<int>(iss),
std::istream_iterator<int>(),
std::back_inserter(cipher_int)
);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this function:
void map()
{
map<char, string> change;
string usrstr = "A APPLE AND BANANA";
change['A'] = "00011";
change['B'] = "11001";
change['C'] = "01110";
change[' '] = "$$";
}
How would I go about changing all occurrences of 'A' in my string to "00011" and the same for B, C and space. All help is much appreciated
P.S The string wont always be the same
Not sure to understand: how about:
std::string str = "A APPLE AND BANANA";
std::replace(str.begin(), str.end(), "A", "00011" );
std::replace(str.begin(), str.end(), "B", "11001" );
...
You can and should probably use string::replace.
Even with your most recent comment.
But this might have been what you were trying to do:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <string>
using namespace std;
int main()
{
char temp;
map<char, char*> change;
string lol = "A APPLE AND BANANA";
change['A'] = "00011";
change['B'] = "11001";
change['C'] = "01110";
change[' '] = "$$";
for (int i = 0; i < lol.length(); i++)
{
temp = lol[i];
if (change[temp])
cout << change[temp];
else
cout << lol[i];
}
cout << endl;
cin.get();
return 0;
}