This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert char array to single int?
How to convert char array to uintmax_t?
char array contains the uintMax_t value but in string format.
Thank you.
You need to use atoi or atol .
Related
This question already has answers here:
How to convert string to wstring in C++
(1 answer)
How to convert char* to wchar_t*?
(9 answers)
How to convert UTF-8 std::string to UTF-16 std::wstring?
(6 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
If I have this below code -
string str;
wstring wstr;
for (char x : str)
{
wstr += x;
}
Is this line wstr += x wrong? Do I need some conversion function to convert char x to wchar_t to be stored in wstr? If yes, which conversion function do I need?
Edit - I have gone through the linked answers, it mentions about converting the array of wchar_t and char -> for that for sure conversion functions are needed but my question specifically asks if a char -> wchar_t would need conversion function
This question already has answers here:
C++ convert hex string to signed integer
(10 answers)
Closed 1 year ago.
I have a variable of type char [] with an address. Example: char addr[11] = "0x01234567";. How do I convert this variable to type uintptr_t?
I want it to be equivalent to uintptr_t add = 0x01234567;.
"0x01234567" is a textual representation of an integer. std::uintptr_t is an integer type. You can use any standard formatted input functions to do the conversion. There are several options, but I recommend starting with a string stream.
This question already has answers here:
How to convert a string of hex values to a string?
(4 answers)
Converting a hex string to a byte array
(22 answers)
Closed 4 years ago.
I have a string like this:
std::string s="840D8E88B0AC";
and an array:
char MAC[6];
I want to produce this:
MAC={0x84,0x0D,0x8E,0x88,0xB0,0xAC};
I try with sscanf() but I can't make it.
sscanf(s.c_str(), "%02X%02X%02X%02X%02X%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
It should be (other errors notwithstanding)
sscanf(s.c_str(), "%02X%02X%02X%02X%02X%02X", &MAC[0], &MAC[1], &MAC[2],
&MAC[3], &MAC[4], &MAC[5]);
sscanf (and variants) require pointers in order to change the variables that are being read into.
Surprised your compiler didn't warn you about that error.
This question already has answers here:
How to convert a char array to a string?
(5 answers)
Closed 9 years ago.
I'm trying to find how to convert a char array into a std::string, but I can't find anything to do this.
If it is impossible, could you explain me how can I read an std::string but by scanf() and gets() I can't.
Best regards.
The simplest way of making a string from a character array is using a constructor that takes a pointer and the length, like this:
char charArray[] = {'w', 'x', 'y', 'z'};
std:string s(&charArray[1], 2); // s is "xy"
// ^ ^
// | |
// Pointer to the |
// starting position Length
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Modify a char* string in C
I have:
char* htmlstring = "htmldata"
How do I extract:
char* htmlstring1 = "htmldata before </body>tag"
char* htmlstring2 ="htmldata after and including</body> tag"
Can I use strstr? Whats the best approach?
strncpy(destination_string, source + start, count)
It's not really safe, but works. You should wrap it around.