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);
}
Related
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 2 years ago.
Improve this question
What should I do if i want to take an input in C++ but the integer is very long (e.g. 1000101010101001)?
I tried using long long int but that didn't work.
int main()
{
long int number = 14072020;
binaryeven(number);
int num2;
binaryodd(number);
checkeven(14072020,1101011010111000110101001);
return 0;
}
You should read the value in a std::string, as "1101011010111000110101001".
Then, check the last digit. For binary, '0' is even, '1' is odd. For decimal, '0', '2', '4', '6', '8' are even, the others are odd.
No need to check anything but the last digit/bit.
If you need read from text a binary representation of some value just use std::bitset.
std::bitset<32> x;
while(std::cin >> x) {
std::cout << x << " = " << x.to_ullong() << '\n';
}
https://godbolt.org/z/d3MGfK
If you need use integral literal in binary representation you can do it since C++14:
checkeven(14072020, 0b1101011010111000110101001);
https://godbolt.org/z/nY3s3n
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 6 years ago.
Improve this question
I am interested in understanding the implementation of converting decimal to binary. Could somebody clarify the purpose of using left-shift and Right-shift in the following code?
void static inline unsignedToBinary(unsigned x, char*& bin)
{
bin = (char*) malloc(33);
int p = 0;
for (unsigned i = (1 << 31); i > 0; i >>= 1)
bin[p++] = ((x&i) == i) ? '1' : '0';
bin[p] = '\0';
}
This is a straightforward implementation of binary conversion that uses bit operations.
Variable i represents the mask - an int containing 2k value, where k is the position of the bit.
The initial value is 231, produced with left-shifting 1 by 31.
for loop uses >>= 1 to right-shift the mask until 1 gets shifted out of it, making i == 0.
At each iteration x&i is compared to i. The comparison succeeds when x contains 1 in the position where i has its 1; it fails otherwise.
Note: Although using malloc in C++ is certainly allowed, it is not ideal. If you want to stay with C strings, use new char[33] instead. A more C++-like approach would be using std::string.
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);
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
We are given a integer number, and the task is to tell whether the binary representation of the number includes equal number of binary 1's and 0's or not?
I want the solution in constant time.
I have the code for calculating no of 1s with the help of hamming weight algorithm!
Please help i want to count no of 0's!!
In production code (I mean if not restricted by rules dictated in an assignment) I'd do it like this:
#include <iostream>
#include <bitset>
int main()
{
int k(24); // an example integer - the one you check for equality of 0's and ones
std::bitset<32> bs(k); // I suppose 32 bit numbers - choose your own length
if ( 16 == bs.count() ) // 16 is half the bit length - count returns the bits that are swithced ON
{
std::cout << "Equal number of 1s and 0s\n";
}
}
I mean after all the question is tagged c++
If x - is your number, N1 is the number of "1" then
int N0 = ceil(log2(x)) - N1;
will calculate number of "0". Do not forget
#include <math.h>
int numberOfZeros = numberOfBinaryDigits - numberOfOnes;
Where number of binary digits is either based on the storage used for the data, or log2.
32 bit integer examples:
Using bit operators (and multiply):
int bitcount(unsigned int i)
{
// generate a bit count in each pair of bits
i = i - ( (i >> 1) & 0x55555555);
// generate a bit count in each nibble
i = (i & 0x33333333) + ( (i >> 2) & 0x33333333 );
// sum up the bits counts in the nibbles
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
Using gcc popcount:
int bitcount(unsigned int i)
{
return(__builtin_popcount(i));
}
using visual studio popcnt:
int bitcount(unsigned int i)
{
return(_popcnt(i));
}
// if(16 == bitcount(i)), then equal number of 1's and 0's.
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.