I want to get every characters of ASCII in normal char. If I only put char key only, it would return dec.
My request:
char alph = //ascii dec to normal char
For example: A in dec is 65
Note: I don't have the characters, but I do have the ASCII codes in dec like 65.
because I need user input like 65
In this case you can do this:
#include <iostream>
using namespace std;
int main() {
int code;
cout << "Enter a char code:" << endl;
cin >> code;
char char_from_code = code;
cout << char_from_code << endl;
return 0;
}
This will ouput:
Enter a char code:
65
A
It seems you have misunderstood the concept.
The numerical value is always there. Whether you print it as the letter or the numerical value depends on how you print.
std::cout will print chars as letters (aka chars) so you'll need to cast it to another integer type to print the value.
char c = 'a';
cout << c << endl; // Prints a
cout << (uint32_t)c << endl; // Prints 97
cout << endl;
uint32_t i=98;
cout << i << endl;
cout << (char)i << endl;
Output:
a
97
98
b
This is the method, very simple and then just need to make your own user interface to get input dec
#include <iostream>
using namespace std;
int main() {
int dec = 65;
cout << char(dec);
cin.get();
return 0;
}
Looks like you need hex/unhex converter. See at boost, or use this bicycle:
vector<unsigned char> dec2bin( const string& _hex )
{
vector<unsigned char> ret;
if( _hex.size() < 2 )
{
return ret;
}
for( size_t i = 0; i <= _hex.size() - 2; i += 2 )
{
string two = string( _hex.data() + i, 2 );
stringstream ss( two );
string ttt = ss.str();
int tmp;
ss >> /*hex >>*/ tmp;
unsigned char c = (unsigned char)tmp;
ret.insert( ret.end(), c );
}
return ret;
}
int main()
{
string a = "65";
unsigned char c = dec2bin( a )[0];
cout << (char)c << endl;
return 0;
}
Related
#include <iostream>
using namespace std;
template <class T> class Linear {
private:
T *a;
T key;
int n;
public:
Linear();
void LS();
};
template <class T> Linear<T>::Linear() {
a = new T[10];
cout << "\nEnter the no. of elements in the array";
cin >> n;
cout << "\nEnter the elements in the array";
for (int i = 0; i < n; i++) cin >> a[i];
cout << "\nEnter the key value";
cin >> key;
}
template <class T> void Linear<T>::LS() {
int flag = 0, i;
for (i = 0; i < n; i++) {
if (key == a[i]) {
flag = 1;
break;
}
}
if (flag == 1) cout << "\nElement found at" << i + 1 << "index";
else cout << "\nElement not found";
}
int main() {
Linear<char> l;
l.LS();
return 0;
}
The code is intended to read multiple digits into the array. However when I input
5 23 24 25 26 27
I expect to see
23 24 25 26 27
but I see
2 3 2 4 2
The problem ( thanks to #brc-dd for clarifying ) is that cin behaves counter intuitively with regards to char types. It only reads one character at a time rather than treating char like a number which in all other respects it is. The following code demonstrates this fact
#include <iostream>
#include <stdint.h>
#include <sstream>
using namespace std;
int main(){
std::stringstream ss("27");
int8_t v_int8;
char v_char;
int v_int;
long v_long;
short v_short;
ss >> v_int8;
std::cout << v_int8 << std::endl;
ss.seekg(0);
ss >> v_char;
std::cout << v_char << std::endl;
ss.seekg(0);
ss >> v_int;
std::cout << v_int << std::endl;
ss.seekg(0);
ss >> v_int;
std::cout << v_int << std::endl;
ss.seekg(0);
ss >> v_short;
std::cout << v_short << std::endl;
ss.seekg(0);
ss >> v_long;
std::cout << v_long << std::endl;
ss.seekg(0);
}
the output being
2
2
27
27
27
27
Note that even if you try to get clever and use int8_t it is really only an alias for char and will read character by character rather than as a number like you might assume.
Note: stringstream is like using cin except the input comes from a string instead of user input. ss.seekg(0) just puts the stream back to the beginning of the string.
I must write short script which will change int to ASCII char and char to ASCII int. But i don't know how. I wrote this short code but something is wrong. I'm programming for half-year. Can someone write it working? It is my first time to use functions in c++
#include <iostream>
#include <conio.h>
using namespace std;
char toChar(int n) {
return n + '0';
}
int toInt(char c) {
return c - '0';
}
int main()
{
int number;
cout << "Int: ";
cin >> number;
cout << "ASCII: " << static_cast<char>(number);
getch();
return 0;
}
Thank You very much Guys, I have done it with shorter and working code.
#include <iostream>
using namespace std;
int main(){
int a=68;
cout<<char(a);
char c='D';
cout<<int(c);
return 0;
}
you could also use printf, so you don't need to create other function for converting the number.
int i = 64;
char c = 'a';
printf("number to ascii corresponding to %d is %c\n", i, i);
printf("ascii to number corresponding to %c is %d\n", c, c);
the output is gonna be
number to ascii corresponding to 64 is A
ascii to number corresponding to a is 97
May be you could start with the code below. If this is incomplete, could you please complete your question with test cases?
#include <iostream>
using namespace std;
char toChar(int n)
{ //you should test here the number shall be ranging from 0 to 127
return (char)n;
}
int toInt(char c)
{
return (int)c;
}
int main()
{
int number = 97;
cout << "number to ascii corresponding to " << number << " is " <<(char)number << " or " << toChar(number) <<endl;
char car='H';
cout << "ascii to number corresponding to " << car << " is " << (int)car << " or " << toInt(car) << endl;
return 0;
}
The output is:
number to ascii corresponding to 97 is a or a
ascii to number corresponding to H is 72 or 72
#include <iostream>
using namespace std;
char toChar(int n)
{
if (n > 127 || n < 0)
return 0;
return (char)n;
}
int toInt(char c)
{
return (int)c;
}
int main()
{
int number = 97;
cout << "char corresponding to number " << number << " is '" << toChar(number) << "'\n";
char car='H';
cout << "number corresponding to char '" << car << "' is " << toInt(car) << "\n";
return 0;
}
output:
char corresponding to number 97 is 'a'
number corresponding to char 'H' is 72'
Originally I thought you were just looking to convert the number:
You can simply add and substract '0', to char and from char:
char toChar(int n) {
return n + '0';
}
int toInt(char c) {
return c - '0';
}
If you just want to cast the type, read this
I am confused as to how to take multiple strings that have numbers in them and convert them into an int. I have multiple lines of strings that are stored in data into ints and then insert them into a 2 dimensional arrays called values. A similar question was posted earlier on StackOverflow; however it does not seem to be working for me. I printed out what each line in data is, each string in data is as follows.
75
95 64
17 42 82
18 35 87 10
However when I output the numbers from value by using two for loops in main, it outputs as this.
75
0
0
0
95
64
0
0
95
64
0
0
17
42
82
0
17
42
82
0
18
35
87
10
18
35
87
10
0
0
0
0
I found that there are 8 columns and 8 elements in the array values when I printed the sizeof(values) and sizeof(values[0]); however, it appears that the program terminated as the last print statement, where I print hello does not occur. I provided the code I'm using below. I would like to know why this is occurring
and how I can fix it? Thanks.
//const char DELIMITER = ' ';
int **values, // This is your 2D array of values, read from the file.
**sums; // This is your 2D array of partial sums, used in DP.
int num_rows; // num_rows tells you how many rows the 2D array has.
// The first row has 1 column, the second row has 2 columns, and
// so on...
bool load_values_from_file(const string &filename) {
ifstream input_file(filename.c_str());
if (!input_file) {
cerr << "Error: Cannot open file '" << filename << "'." << endl;
return false;
}
input_file.exceptions(ifstream::badbit);
string line;
vector<string> data;
try {
while (getline(input_file, line)) {
data.push_back(line);
num_rows ++;
}
input_file.close();
} catch (const ifstream::failure &f) {
cerr << "Error: An I/O error occurred reading '" << filename << "'.";
return false;
}
for(int x = 0; x < data.size(); x++){
cout << data[x] << endl;
}
//https://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers
//Help on making multiple numbers in a string into seperate ints
values = new int*[num_rows];
vector<int> v;
for(int y = 0; y < data.size(); y++){
istringstream stream(data[y]);
values[y] = new int[y + 1];
int z = 0;
while(1) {
int n;
stream >> n;
if(!stream)
break;
values[y][z] = n;
z++;
}
z = 0;
}
sums = values;
return true;
}
int main(int argc, char * const argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <filename>" << endl;
return 1;
}
string filename(argv[1]);
if (!load_values_from_file(filename)) {
return 1;
}
cout << sizeof(values) << endl;
cout << sizeof(values[0]) << endl;
for(int x = 0; x < sizeof(values); x++){
for(int y = 0; y < sizeof(values[x]); y++){
cout << values[x][y] << endl;
}
cout << endl;
}
cout << "hello" << endl;
return 0;
}
See this :
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
int main()
{
string str;
ifstream fs("D:\\Myfolder\\try.txt", ios::in);
stringstream ss;
std::string item;
if (fs.is_open())
{
ss << fs.rdbuf();
str = ss.str();
cout << str << endl;
fs.close();
}
cout << "\n\n Output \n\n";
while (std::getline(ss, item, ' '))
{
cout << std::atoi(item.c_str()) <<" ";
}
return 0;
}
I'd like to show each letter's ascii code
for example
Input: HelloWorld
Ascii Value: 72 + 101 + 108 ... = 1100
And here's my now-code
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char str[32] = { 0 };
int value = 0, i;
cout << "Input: ";
cin >> str;
for (i=0;i<32;i++)
{
value += str[i];
}
cout << "Ascii Value:" << value << endl;
return 0;
}
I only can take the total value of ascii code such as 1100,
not every code value of each letters such as 7 + 11 + ... = 1100.
How can I fix it?
You should use a string for your input (it's c++, not c). Your for loop sums 32 characters, even if the user inputs a shorter string (the programm will read random values from memory). For conversion from int to char you can use stringstream. This results in
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string input;
std::stringstream sstr;
int value = 0;
std::cout << "Input: ";
std::cin >> input;
for (int i = 0; i < input.size(); i++) {
sstr << int(input[i]) << " + ";
value += input[i];
}
std::string str(sstr.str());
std::cout << "Ascii Value:" << str.substr(0, str.size() - 3) << " = " << value << std::endl;
return 0;
}
I have a char array (lets' say "13 314 43 12") and i want to put the first number (13) into a separate integer . how do i do that ? is there any way like splitting the first number into 10 + 3 and then adding them to the int ?
I am not sure what you mean by getting 1 and 3, but if you want to split the space-separated string into integers I suggest using a stream.
std::istringstream iss(s);
int n;
while(iss >> n)
{
std::cout << "Integer: " << n << std::endl;
}
[edit] Alternatively, you could parse the string yourself, something like this:
char* input = "13 314 43 12";
char* c = input;
int n = 0;
for( char* c = input; *c != 0; ++c )
{
if( *c == ' ')
{
std::cout << "Integer: " << n << std::endl;
n = 0;
continue;
}
n *= 10;
n += c[0] - '0';
}
std::cout << "Integer: " << n << std::endl;
#include <cstring>
#include <iostream>
#include <stdlib.h>
int main ()
{
char str[] = "13 45 46 96";
char * pch = strtok (str," ");
while (pch != NULL)
{
std::cout << atoi(pch) << "\n"; // or int smth=atoi(pch)
pch = strtok (NULL, " ");
}
return 0;
}
If you just want the first number, just use a function like atoi() or strtol(). They extract a number until it runs into the null terminated character or a non-numeric number.
According to your question I think following code will give some idea.
#include <string>
#include <iostream>
using namespace std;
int main(){
char s[] = "13 314 43 12";
//print first interger
int v = atoi(s);
cout << v << std::endl;
//print all integer
for (char c : s){
if (c == ' ' || c == '\0'){
}else{
int i = c - '0';
cout << i << std::endl; // here 13 print as 1 and 3
}
}
}
If you want to print first number you can use
int v = atoi(s);
cout << v << std::endl;
If you want to split and print all integers Ex: 13 as 1,3
for (char c : s){
if (c == ' ' || c == '\0'){
}else{
int i = c - '0';
cout << i << std::endl; // here 13 print as 1 and 3
}
}