Why output is 0 for from_chars | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
+ 2

# Why output is 0 for from_chars

Hi Refer code below: Should not it print 2 as output as I am asking to provide me int? I know I have given base as 2 , but don't it print then binary representation of value 2? https://sololearn.com/compiler-playground/cITNirdLGdYi/?ref=app

17th Apr 2024, 12:44 PM
Ketan Lalcheta
10 Respostas
+ 4
It is because of the from_chars function. If you want to convert a string representation in binary format to an integer, you first need to convert the binary string to decimal and then use from_chars with base 10.
17th Apr 2024, 1:14 PM
`Š½ŃŃįØā“ā°ā¶
+ 3
My basic understanding was wrong. I thought output of from_chars will be decimal or binary based on input argument. This was wrong from my side. Actually, input string is converted to int or float always , never binary. Last argument suggesting 2 0r 10 is an indication about format of input string. If input is "10" and we will pass 2 (means format is binary for input "10"), int output will be 2. If input is "2" and we will pass 10 (means format is decimal for input "2"), int output will be 2. Many thanks `Š½ŃŃpā“ā°ā¶ for your patience to help me understand š
17th Apr 2024, 6:28 PM
Ketan Lalcheta
+ 2
Exactly!
17th Apr 2024, 6:35 PM
`Š½ŃŃįØā“ā°ā¶
+ 1
In your code, the first from_chars call converts the string "2" from decimal to an integer and stores it in res, which is then printed. And the second from_chars call attempts to convert the same string "2" into an integer in base 2 (binary). Actually to convert an integer to binary representation, we have to manually convert the integer to its binary form using bit manipulation or predefined functions in C++. Using std::bitset is a convenient method or way to display the binary representation of an integer. But here is an issue with the C++ standard library version we are using in Sololearn. The std::from_chars function was introduced in C++17, but Sololearn using an older compiler may be that does not support C++17 or later. So, an alternative approach would be to use std::stoi to convert the string to an integer. I told you to use this alternative way! Here is thatā¤µļø https://sololearn.com/compiler-playground/c16RCQ9c6XQv/?ref=app
17th Apr 2024, 2:43 PM
`Š½ŃŃįØā“ā°ā¶
+ 1
The problem isn't in Sololearn compiler it is in your code. I was wrong! You convert the string str to an integer res using base 10 (decimal). Then convert the string str to an integer res2 using base 2 (binary). But 2 is not a binary number. What I do, I directly provide the binary representation "10" of 2 as the input string to from_chars with base 2. Therefore, res2 is correctly initialized to 2 in binary representation, and when converted to a string using bitset, it correctly shows the binary representation as "00000000000000000000000000000010". The long string of binary digits, "00000000000000000000000000000010", is because bitset<sizeof(int) * 8>(res2) creates a bitset object with enough bits to represent an integer of size sizeof(int) * 8. In most systems, an int is typically 4 bytes, or 32 bits. I just trim the string to remove them before printing. Ketan Lalcheta Here is your solution for this code! https://sololearn.com/compiler-playground/cx9vgGI4iTAz/?ref=app
17th Apr 2024, 4:37 PM
`Š½ŃŃįØā“ā°ā¶
+ 1
Ketan Lalcheta No need to thanks brother. It feels happy to help you āŗļø
17th Apr 2024, 6:30 PM
`Š½ŃŃįØā“ā°ā¶
+ 1
With my above understanding, it makes sense to me now why I was getting wage output in my question code. string str = "2"; int res2; from_chars(str.data(),str.data()+str.size(),res2,2); Above code suggest to compiler that give me int from string and I would have given binary format string (as last argument is 2 provided) This is invalid input string as char 2 is never present in binary format. 1 and 0 were only allowed
17th Apr 2024, 6:33 PM
Ketan Lalcheta
0
I could not get your point about what is input and how to use it. If string "2" is there in decimal format as value 2, can I get its binary format using from_chars ?
17th Apr 2024, 2:14 PM
Ketan Lalcheta
0
Do you mean to say that sololearn does not support required version for binary format using from_chars? If so, could you please share correct code and I will check it out on other compiler. P.s. : std::cout << __cplusplus; prints 201703 on sololearn . So, it is 2017 version of c++ on sololearn as well
17th Apr 2024, 3:24 PM
Ketan Lalcheta
0
Sololearn has C++17! I checked already! Thanks for your concern. https://sololearn.com/compiler-playground/c32FP1z6zu0p/?ref=app
17th Apr 2024, 4:59 PM
`Š½ŃŃįØā“ā°ā¶
Quente hoje
C++