Convert char[] variable to uintptr_t variable C++ [duplicate] - c++

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.

Related

should the value of pointer always be an address? [duplicate]

This question already has answers here:
initializing char pointer as string vs other type pointers as arrays
(5 answers)
Initializing a char pointer C++ [duplicate]
(3 answers)
What is the type of string literals in C and C++?
(4 answers)
Closed 5 months ago.
so ive just begun learning about pointer basics and ive come across something im stuck on.
as the title says, should the value of the pointer must always be an address?
because i saw a line of code, which says otherwise:
char *text = "text";
this here is being used for the creation of a string, the other method is:
char text[] = "text";
which is pretty understandable.
could you guys explain to me what this line does exactly?
char *text = "text";
a pointer is being used but what does it do and point to? how can you use it to then access
the string created.
thanks.
"text" is a string literal. It is stored somewhere in memory and its address is used to initialise the pointer. You access the string as you would with any other pointer.
And as stated above
char *text = "text";
is not legal C++ (it is legal C) the correct C++ is
const char *text = "text";

convert std::string to unsigned char [duplicate]

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.

Does `std::string` Store its Characters Internally as Signed Chars? [duplicate]

This question already has answers here:
Difference between char and signed char in c++?
(4 answers)
Closed 4 years ago.
I'm writing an algorithm requires me to insert values into the std::string via an unsigned 1 byte integer (0 - 255) and calls for me to print the values of the individual characters of the string as integers, however, I keep getting negative values. My wild guess is that the characters are stored in the std::string as signed 1 byte characters (-128 to 127), hence, why I'm getting negative values for output. The negative values are the equivalent negative forms of the positive values I'm inserting. I did a bit of research, but couldn't seem to find a way to word my question in a way that produced the answer I was looking for.
No, this is implementation-dependent.
std::string is an alias for std::basic_string<char>, so the question boils down to the signedness of char on your plarform/implementation.
If you want it unsigned, explicitly convert it:
std::cout << static_cast<int>(static_cast<unsigned char>(ch));
Or alternatively, as suggested in comments, use vector<uint8_t> instead of string.
This has nothing really to do with string, it's just that char may be a signed type. All you need to do is cast your char to unsigned char. E.g.
char some_char = ...;
cout << (int)(unsigned char)some_char;
or
string some_string = ...;
cout << (int)(unsigned char)some_string[0];

Generating a char using input from user in C++ [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 7 years ago.
I wish to make a char with digits between 0-9. The user decides how many digits to use.
For example, if the user inputs 4, the char should be 01234.
Please note I cannot use the string data type. I have to use char.
I know how to generate a string for the same logic but not a char.
So if there is a way to convert string to char, that will work well. I tried
string randomString; //this contains the set of numbers 0-9 on the basis of the users input
char charString = randomString;
This however does not work.
So if there is a way to convert string to char
Yes, it's called a character array and you can easily convert a string type to a character array like so:
const char* charString = randomString.c_str();
You can find more information about c_str() method here and you should review this material regarding character arrays.
If you require a non-const (can be modified) character array, refer to the above links which will explain it and actually give examples about how to accomplish that.

How to convert char array to uintmax_t? [duplicate]

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 .