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.
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:
C++ concat three char* strings togther [closed]
(4 answers)
Closed 2 years ago.
When I am trying to run this code. I'm getting this error "C2110: '+' : cannot add two pointers". Can anyone just tell me what is wrong in the code?
string Msg;
getline(cin, Msg);
string output;
output = "<Rvc>\n"+"<Msg>"+Msg+"< / Msg>\n";
C-style string literals are not std::strings. "<Rvc>\n" and "<Msg>" are of type const char[] and could decay to pointers (i.e. const char*). Adding on pointers doesn't make sense.
You can just
output = "<Rvc>\n<Msg>"+Msg+"< / Msg>\n";
Then the overloaded operator+ for std::string taking const char* and std::string will be used.
This question already has answers here:
C or C++. How to compare two strings given char * pointers?
(8 answers)
Closed 4 years ago.
If I have a char* that is the output of another function, and I know it is one of the 10 known words, what is the best way to find what is it?
converting the char* to string bystd::string(char*) , then using string.compare() ?
char* c = "hi";
string s = std::string(c);
if (s.compare("hello") )
Is this the best way? I can not directly write:
char* c ="hi";
if(c == "hello")
Since you already have a C string, just use strcmp. It will likely be faster than the s.compare method since you avoid the overhead of doing a conversion to std::string for both the original string and the string to compare to.
if (strcmp(c, "hello") == 0) {
...
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 .