This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ convert hex string to signed integer
I have the string line which is a hexadecimal number say like 12ab43c..(but I have read it as a string) and I would like to pass it to an unsigned char* linehex or directly to a hexadecimal so I can later use it in my program for further computations.
Which is the most efficient way to do this?
The easiest is probably to read it as a number to start with, instead of reading it as a string, then converting. For example:
some_stream >> std::hex >> your_number;
Quick demo code:
#include <iostream>
int main() {
int x;
std::cin >> std::hex >> x;
std::cout << x << "\n";
return 0;
}
Input: ff
Output: 255
Related
This question already has answers here:
How to output a character as an integer through cout?
(6 answers)
Convert an int to ASCII character
(11 answers)
Closed 3 years ago.
I have a program that takes a string. Using the check() function that calculate the sum of all the value in the string of integers, I make additional computations, aka ss. The issue comes when I try to convert ss, which is an int, into a char, c.
When I try to print out the newly converted value, nothing prints out on the console, not even an error message.
I have tried using static_cast<char>(ss), and it won't work. Yet when I try to print out the ss value, I get it to print it out.
Source Code
void sum(string input)
{
int s = check(input);
int ss = (s * 9) % 10;
char c = ss;
cout << "val is: " << c << endl;
}
int main()
{
string x = "7992739871";
sum(x);
return 0;
}
Can someone explain what I might be doing wrong and how it can be fixed?
You can use std::to_string() (C++11) but making sure that the value of c is something that can be printable is a better practice.
This question already has answers here:
Integer to hex string in C++
(27 answers)
Closed 5 years ago.
I have an int which I want to convert to a char array, but I want the char array to be formatted in hexadecimal and with every byte of the int taking up exactly 2 char variables (filled out with zeroes).
To clarify what I mean, I have an example:
I want the int 232198 (0x38b06) to become "00038b06".
I can of course acomplish this by using this code:
#include <iostream>
#include <iomanip>
int main()
{
std::cout <<
std::hex <<
std::setw(8) <<
std::setfill('0') <<
232198 <<
std::endl;
return 0;
}
Which prints out:
00038b06
But that only prints it out to the console, and as I mentioned before, want to store it a char array.
I don't care if the code is portable or not, this just has to work for windows.
stringstreams are useful to do this:
std::stringstream sstr;
sstr <<
std::hex <<
std::setw(8) <<
std::setfill('0') <<
232198;
std::string str = sstr.str();
now str contains the formatted number. str.c_str() will give you a const char*.
This question already has answers here:
C++ convert hex string to signed integer
(10 answers)
Closed 8 years ago.
I have searched online, but it doesn't seem to be a solution to my problem. Basically I have a std::string which contains a hexadecimal memory address (like 0x10FD7F04, for example). This number is read from a text file and saved as a std::string, obviously.
I need to convert this string to an int value, but keeping the hex notation, 0x. Is there any way to do this?
You can use C++11 std::stoi function:
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string your_string_rep{"0x10FD7F04"};
int int_rep = stoi(your_string_rep, 0, 16);
std::cout << int_rep << '\n';
std::cout << std::hex << std::showbase << int_rep << '\n';
}
Outputs:
285048580
0x10fd7f04
I need to convert this string to an int value, but keeping the hex notation, 0x. Is there any way to do this?
There are two parts to your question:
Convert the string hexadecimal representation to an integer.
std::string your_string_rep{ "0x10FD7F04" };
std::istringstream buffer{ your_string_rep };
int value = 0;
buffer >> std::hex >> value;
Keeping the hex notation on the resulting value. This is not necessary/possible, because an int is already a hexadecimal value (and a decimal value and a binary value, depending on how you interpret it).
In other words, with the code above, you can just write:
assert(value == 0x10FD7F04); // will evaluate to true (assertion passes)
Alternatively you can use something like this
std::string hexstring("0x10FD7F04");
int num;
sscanf( hexstring.data(), "%x", &num);
This question already has answers here:
decimal to 8 bit binary conversion in c++
(3 answers)
Closed 9 years ago.
I was trying to print an integer in binary format using output stream manipulators in C++ but I was unsuccessful in doing so.
I tried using the following code to manipulate the base.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int binary, gray;
cin >> binary;
cout << std::setbase(2) << (binary >> 1);
}
But I guess only decimal, hexadecimal and octal bases are supported in this manipulator.
Is there any way I can print an integer in binary format using manipulators in iomanip header?
Use bitset i.e.
cout << bitset<16>(10).toString() << endl;
std::setbase only supports bases of 8, 10, or 16. You can use std::bitset<N> to print out the binary representation yourself however:
#include <bitset>
int main()
{
std::cout << std::bitset<16>(0x1F); // 0000 0000 0001 1111
}
#include <bitset>
#include<iostream>
int main(){
int i=13;
std::bitset<8> bit (i);
std::cout<<bit.to_string();
return 0;
}
result:
00001101
cout<<bitset<8>(0xd).to_string();
gives the same
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
I want to convert an int variable to string to put it in the textbox.
I have tried this code:
int x = 123;
std::ostringstream osstream;
osstream << x;
std::string string_x = osstream.str();
But it doesn't work.
Try using stringstream instead
std::stringstream osstream;
osstream << x;
std::string string_x = osstream.str();
std::cout << string_x << std::endl;
Works on my computer.
I had another issue once where I had to append a whitespace to that, so try this too:
std::stringstream osstream;
osstream << x << " ";