I have an array of unsigned chars with hex values and I need to make a string of them. So when I have arr[0]=2b and arr[1]=fc I want to be able to make a string s="2bfc", not the characters from ascii codes. How can I do that?
You could change to long or int and loop through adding +arr[ArrayIndex]*0x10^ArrayIndex to get one int representation of it using itoa(), then convert the int to a literal string of it. Just to outline a manual process of doing it.
Related
Im using this atoi to remove all letters from the string. But my string uses special characters as seen below, because of this my atoi exits with an error. What should I do to solve this?
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string playerPickS = "Klöver 12"; // string with special characters
size_t i = 0;
for (; i < playerPickS.length(); i++) { if (isdigit(playerPickS[i])) break; }
playerPickS = playerPickS.substr(i, playerPickS.length() - i); // convert the remaining text to an integer
cout << atoi(playerPickS.c_str());
}
This is what I believe is the error. I only get this when using those special characters, thats why I think thats my problem.
char can be signed or unsigned, but isidigt without a locale overload expects a positive number (or EOF==-1). In your encoding 'ö' has a negative value. You can cast it to unsigned char first: is_digit(static_cast<unsigned char>(playerPickS[i])) or use the locale-aware variant.
atoi stops scanning when it finds something that's not a digit (roughly speaking). So, to get it to do what you want, you have to feed it something that at least starts with the string you want to convert.
From the documentation:
[atoi] Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer number representation and converts them to an integer value. The valid integer value consists of the following parts:
(optional) plus or minus sign
numeric digits
So, now you know how atoi works, you can pre-process your string appropriately before passing it in. Good luck!
Edit: If your call to isdigit is failing to yield the desired result, the clue lies here:
The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.
So you need to check for that yourself before you call it. Casting playerPickS[i] to an unsigned int will probably work.
Suppose I have a string that contains a necessary numeric character but it is not terminated by '/0', it has garbage characters instead. Actually, the string has garbage characters after the number. So how to deal with the garbage character while storing that numerical character in another string or variable?
So how to deal with the garbage character while storing that numerical character in another string or variable?
Only copy a substring. Example:
std::string example "garbage1garbage";
char numerical = example[7];
We got the numerical character excluding the garbage entirely.
If the text be converted is in a std::string, then you can extract a number from the front as follows:
#include <sstream>
...
std::string input = "128734garbage";
std::istringstream iss{input};
int num;
if (iss >> num)
...use_num...
else
std::cerr << "wasn't able to parse an int from input\n";
Just change int to double, uint64_t, ... - whatever suits your data.
If you have only a pointer to the text and know it's not null-terminated, just getting the text into a std::string is problematic. You could instead use a function that converts text to a number, but stops at the first invalid character. std::stol et al, and the other unsigned and floating point variants linked from the same reference page, are good candidates for that.
From your "another string or variable" - the above addresses storing into a numeric variable. You can then create a new std::string from the number using std::to_string, or a std::ostringstream, if that's what you want to do. This will standardise the output format though, so input like say "1E4" might end up looking like say 1000.0. Alternatively, with the stol-type functions you can use the pointer-to-the-end-of-the-number to work out the length of the numeric part, and use std::string::substr() to extract the leading number as a new std::string object.
You should also be aware that the distinction between number and garbage is not always what you might expect. For example "0XBEFHJQ" might be split by some of the above functions as 0xBEF hex and HJQ garbage.
Hi I have a string that looks like this:
std::string myString = "123456789";
Is there a way for me to turn that string of a decimal number to a binary number?
Firstly, you want to convert the string representing a number into an actual integer.
As stated by some others: std::stoi "string to int". Depending on the size of the numbers, you may want to use std::stoull. this is the same thing but for long long, a 64 bit integer.
Next you want to convert that number back into string. the easiest thing to use that provides this functionality is std::bitset and its to_string member function. The constructor of bitset requires an unsigned long long, so we'll use std::stoull, the unsigned variant.
This can all be combined into a single line of code:
myString = std::bitset<64>{ std::stoull(myString) }.to_string(); // 64 bits to fit a long long
After this operation, You'll be left with a string that looks like this: 0000000000000000000000000000000000000111010110111100110100010101
You may want to get rid of the zero's up front.
We can use the string's find and erase
myString.erase( 0, myString.find('1') );
This will erase all characters from position 0 until the first 1's position.
If you want to parse string to int, here is how you do it:
std::string strDec = "123456789";
int i = std::stoi(strDec);
If you then want to convert int to string but this time in binary representation, here is a way to do that:
std::string strBin = std::bitset<32>(i).to_string();
Demo
Or, you can use {fmt} library:
#include <fmt/core.h>
int main() {
std::string strDec = "123456789";
std::string strBin = fmt::format("{:032b}", std::stoi(strDec));
}
Demo
i have a big problem and i dont know how to fix it...
I want to decode a very long Base64 encoded string (980.000 Chars) but every time when i to debug it i get this error :
Error C2026: string too big, trailing characters truntraced
I tried this but i can only compare 2 strings throught this method
char* toHash1 = "LONG BASE 64 Code";
char* toHash2 = "LONG BASE 64 Code";
if (true) {
sprintf_s(output, outputSize, "%s", base64_decode(toHash1 =+ toHash2).c_str());
}
Anyone know how i can get it to work?
As documented here, you can only have about 2048 characters in a string literal when using MSVC. You can get up to 65535 characters by concatenation, but since this is still too short, you cannot use string literals here.
One solution would be reading the string from a file into some allocated char buffer. I do not know of any such limits for gcc and clang, so trying to use them instead of MSVC could solve this too.
You can first convert your string to hex and then can include it like this,
char data[] = {0xde,0xad,0xbe,0xef}; //example
And than can use it like a string, append null terminator if needed to.
I have a text file with 1000 hex values in it like this:
00 2f 3a 2e...
and I'm trying to store them in a const char* array with both values in each 'cell', like '00' '2f' '3a'.
Currently, each 'cell' is only containing one character '0' '0' '2' 'f' '3' 'a'.
The code I'm currently using is:
void Myfunction::Myfile(const char* fileName)
{
ifstream Myfile(fileName);
if (Myfile.is_open())
{
while (!Myfile.eof() && i < 1000)
{
Myfile >> Array[i];
i++;
}
}
Myfile.close();
}
I've looked at dynamic memory allocation, pairs, typecasting and just haven't found a solution that fits. Can anybody help? Thanks in advance.
A char array is an array of chars; by definition, each "cell" can only hold a single char.
You could, if you computed what number each hex value represents, store that value in a char (or, more appropriately, an unsigned char). Or, if you insist on storing 2 chars for each value, declare an array whose "cells" can each hold 2 chars.
Since your array is of 'char', you can only write a single character to each element. The first character is a '0', the second is a '0', the third is a ' ', and so on.
If you want to treat the words of your input as integers and store one of those integers in each element of your array, then you'll need to read them in as strings and convert them to integers. Boost's lexical_cast (http://www.boost.org/doc/libs/1_57_0/doc/html/boost_lexical_cast.html) might help you with that.
A sort of ANSI C solution would be to make a data structure:
typedef struct _hex_val{
char first;
char second;
}t_hex_val;
Then you would allocate a large pointer to this and fill it up. You would need a little bit of parsing code to find the ' ' values between the pairs of chars.
I realize that his is very against the grain of what C++ can do, but sometimes, simple C is the easiest (at least for me).