convert int to char without using ASCII [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can i convert an int below 10 to a char for example :
5 -> '5'
(convert int to char without using ASCII table)

Since digits are always consecutive in a standard character set, you can write:
int number = 5;
int character = number + '0';
/* Here, character == '5' */
See, for instance, C11 standard.
n1570, § 5.2.1 Character sets
The 10 decimal digits: 0 1 2 3 4 5 6 7 8 9
[...]
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
The same applies in C++.
n3337, § 2.3 Character sets
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

If it is number 0-9 it is good to use:
int i = 5;
char ch = i + '0';
But probably the best option is to use itoa()
int i = 124;
char buffer[33];
itoa(i, buffer, 10); //10 mean decimal.

Here's an option
char c;
int x;
//...
switch ( x )
{
case 1:
c = '1';
break;
//and so on
}
And another:
std::map<int, char> mapping;
mapping[1] = '1';
//...
char c = mapping[4];

I'm not sure I understand what you mean by "without using ASCII table", but
int i = 5;
char c = i + '0';
will do what you want. The character codes for '0' through '9' are guaranteed to be consecutive and in the proper order.

Related

What is '0' means? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm new to programming and sometimes see expressions like this
...
for (int i=0; i<str1.length(); i++)
{
int sum = ((str1[i]-'0')+(str2[i]-'0'));
str.push_back(sum%10 + '0');
}
...
So that is '0' here? Is it some kind of converting or something?
It's literally the character zero '0'
The operation str[i] - '0' is used to convert the representation of a digit into its numeric value.
Since the characters from '0' to '9' are following in the ascii table, having respectively, the values 48 to 57, when you perform the operation '3' - '0', the interpreter will use the ascii values, 51 - 48 == 3, so you can convert '3' to 3
ASCII Table
(source of the picture : Wikipedia)
'0' is the character zero. Since characters are sequential, adding a digit to 0 will produce the character representing that digit (once cast back to a char). E.g., (char)(2 + '0') (i.e., the integer two plus the character zero) will produce '2' (i.e., the character two).
Well
str2[i] - '0'
Converts character representation of a digit ('3', '7', '9') into its integer value (3, 7, 9). More accurate (and more wordy) construction is
(int)char.GetNumericValue(str2[i])
What is implied, but not really stated, in the other answers is that int and char can be treated as fairly equivalent in c#. You can do math on chars just like you can on ints, and you can convert back and forth between int and char with ease. The number you get is based on the position in the utf8 character tables for the char.
'0' as a char has an int value of 48, so you could do:
int x = 7;
char c = x+48; //c would be '7' as a char, or 55 as an int
Other examples:
char c = 'a';
c++;
Console.Write(c); //prints 'b', because 'a' + 1 is b
It's quite logical and reasonably helpful sometimes* but the main reason you might see '0' is that it's easier to remember '0' than it is to remember 48 (it's slightly easier to remember the hex version 0x30)
All these give you char 5 from int 5:
char five = 5 + 48;
char five = 5 + 0x30;
char five = 5 + '0';
Which one would you find easiest to remember? :)
*for example, say you wanted to count the chars in an ascii string, you could do:
var counts = new int[256];
foreach(char c in string)
counts[c]++;
You can use the char to index the array just like you can an int. At the end of the operation "hello world" would have put a 1 in index 104 (the h), a 3 in index 108(the l) etc..
Sure these days you might use a Dictionary<char, int> but appreciating that intrinsic char/int equivalence and how it can be used has its merits..
What is '0' means?
In C++, it is a character literal.
The fundamental reason why/how str1[i] - '0' works is through promotion.
In particular when you wrote:
str1[i]-'0'
This means
both str1[i] and '0' will be promoted to an int. And so the result will be an int.
Let's looks at some example for more clarifications:
char c1 = 'E';
char c2 = 'F';
int result = c1 + c2;
Here both c1 and c2 will be promoted to an int. And the result will be an int. In particular, c1 will become(promoted to) 69 and c2 will become(promoted to) 70. And so result will be 69 + 70 which is the integer value 139.

Take a digit of a string as an integer in C++ [duplicate]

This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 2 years ago.
I want to add the first digit of a string 111 to the integer x = 0 so that it equals
x = 0 + 1 = 1
The following code takes the character 1 instead of the integer 1:
int x = 0;
string str = "111";
x += str[1];
std::stoi did not work either:
x += std::stoi(str[1]);
The simple way to convert a digit to an integer is to substract '0' from it.
x += str[0] - '0';
This works because the encodings of the decimal digits are guaranteed to be continuous. So subtracting the lowest digit gives you the digit value.
Your other error is that the first character of a string is str[0] not str[1].

How to convert hexadecimal to decimal recursively [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to do this convertion but have no clue. Can someone help me? I need this to complete a calculator of base convertion. I also need that the function return a integer to convert to use in my others functions.
PS: sorry for my bad english.
i expect that 11F to be an integer 287.
Here's something with recursion:
int hexToBase10(const std::string& number, size_t pos = 0) {
if (pos == number.length())
return 0;
char digit = number[number.size() - pos - 1];
int add = digit >= '0' && digit <= '9' ? digit - '0'
: digit - 'A' + 10;
return 16 * hexToBase10(number, pos + 1) + add;
}
Call it this way:
hexToBase10("11F"); // 287
Btw, it seems to be more safe to use std::hex.
Provided it fits into at most unsigned long long, you can use strtoul/strtoull from stdlib.h to parse it from a base-16 string into an integer.
You can then simply print that integer in base 10.
#include <stdlib.h>
#include <stdio.h>
int main()
{
char const *hex = "11F";
char *endptr;
unsigned long ul = strtoul(hex,&endptr,16);
printf("%lu\n", ul);
}

Converting int to String of its ASCII [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm looking a way to convert an integer for example 50, to its ASCII as string.
So far, I tried casting integer to char with
int a = 57 ;
char c = char(a);
But I couldnt convert that char into string. Looking forward to your help!
http://en.cppreference.com/w/cpp/string/basic_string/basic_string
int a = 50;
std::string s(1, char(a));
Using the "fill" constructor (2).
A string is a squence of char and 0 at end.
int a = 57 ;
char c[2] = { (char)a, 0 };
-
#include <string>
std::string str;
str += (char)a;
I don't get your question..the value of int is already the ASCII value.
In your example, 57 is the ASCII value for character 9. Then you can't convert that character to integer where in fact you were the one that declared its value.
If you want character to its ASCII value:
char b;
cin>>b;
int a = int (b);

Understanding how to convert char[0] to int [duplicate]

This question already has answers here:
How does subtracting the character '0' from a char change it into an int?
(4 answers)
Closed 8 years ago.
I was trying to go in a loop and each time convert a character in a string to it's integer value and I don't mean the ASCII value. Tried to use atoi() with no luck but then I stumbled upon this question Convert single char to int and my code worked. The code is as follows:
std::string tmp = "87532621";
for(i=0;i<tmp.length();i++)
{
**int num = tmp[i] - '0';**
//do some processing
}
I fail to understand why the following line of code works. My question is how is it converting the char value to integer type?
int num = tmp[i] - '0';
Each char in your string is an ascii value. The ascii values are just 7 bit numbers.
The numerical values for the character digits lies in a sequence 0123456789 which is very convenient because it makes it possible to write
int zero = '0' - '0'; // 0 (zilch)
int one = '1' - '0'; // one (1)
int nine = '9' - '0'; // 9 (three times three)
And so on.
The actual numerical values are not important for this to work. The fact that the are next to each other in the character set is.
See wikipedia - ascii for the actual numerical values.