+ 2

char to int

can anyone share your knowledge to me for this, i'm declaring a decimal numbers by using the "char" data type. and also the output must also be the same. instead of characters, it will still return decimal values. ex: Input: 1234 (but the data type used was char) Output: 1234

3rd Oct 2017, 1:14 PM
Clark Justine L. Gonzales
Clark Justine L. Gonzales - avatar
21 ответ
+ 5
Is char type capable for accepting 1234 as a value? isn't it limited to 255? it's only one byte (8 bits) data, are you sure, or am I misunderstanding the question?
3rd Oct 2017, 1:58 PM
Ipang
+ 4
@Kirk Schafer, thanks, that's an interesting point, certainly a char combination will accept the value because together they form 16 bit data which can take 1234 value. I didn't know the formula for combining byte value as you explained, it was great!. Another interesting point, so I tested the fact you pointed out, and write a text file with 'aa', and view the content in hex editor I see 61 61, which, combined as 16 bit value makes up 0x6161, the hexadecimal representation of 24,929 just like your example. I really appreciate your enlightenment, thank you once again :)
3rd Oct 2017, 2:37 PM
Ipang
+ 4
@~ swim ~, that's exactly true, however the question did not tag Java, it tagged C instead, and in C/C++ a char is one byte, I was questioning the fact of how a single byte (char) can hold a value as large as 1234. In Java a char is defined as two bytes because Java is Unicode compliant, in Unicode standard a single character indeed consists of two bytes so that non latin characters can be handled correctly. Thank you for the explanation mate, there's always something new to learn everyday, I really appreciate it :)
4th Oct 2017, 8:28 AM
Ipang
+ 4
@~ swim ~, I'll keep that wchar_t in mind, thanks for the advice mate ;-)
4th Oct 2017, 8:47 AM
Ipang
+ 4
@~ swim ~, Thanks for more info on the Unicode string usage, actually I haven't got that far, but I'm gonna be keeping those in my library for later use. Big thanks mate :)
4th Oct 2017, 10:19 AM
Ipang
+ 4
@~ swim ~, Now that's overwhelming, okay, lemme make a note on that... Thanks for #brainstorm :-D
4th Oct 2017, 10:51 AM
Ipang
+ 3
@lpang Multibyte characters would get this done, but not one. cout << int('a')<< endl; 97 cout << 'aa'<< endl; 24929 cout << 97*256 + 97 << endl; 24929 You can infer from this that the limit is 256 values, since the next byte up is * 256 to record it properly.
3rd Oct 2017, 2:10 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
(quietly) Multibyte character sets (MBCS, UTF-8, 16, 32 ...) can be longer than 2 bytes per represented char. gcc on SoloLearn (64-bit server I think) is configured in 32-bit mode. cout << 'abcd'; // is valid here, 32 bits Each 'slot' is a power of 256, in the same fashion as all bases: a*256³ + b*256² + c*256¹ + d (*256° or 1) [sharp turn ahead] You can use the same formula for IP addresses: 127.0.0.1 = 127*256³ + 0 + 0 + 1 = 2130706433 $ ping 2130706433 PING 2130706433 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.194 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.227 ms
4th Oct 2017, 2:04 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Oh damn, I meant 0x30. You are right, I'll edit it! That's what you get for not double checking :P
3rd Oct 2017, 9:47 PM
Schindlabua
Schindlabua - avatar
+ 2
thank you for your shared ideas!
4th Oct 2017, 11:09 AM
Clark Justine L. Gonzales
Clark Justine L. Gonzales - avatar
+ 1
You can convert a single digit like this: char one_char = '1'; int one_int = one_char - 0x30; However it is better to use C's atoi function that converts char*'s to ints. #include <stdlib.h> char* string = "1234"; int number = atoi(string);
3rd Oct 2017, 1:35 PM
Schindlabua
Schindlabua - avatar
+ 1
@Shindlabua, isn't it rather '1' - 48 as 48 == '0' ?
3rd Oct 2017, 7:16 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
How about converting char to int?
4th Oct 2017, 4:38 AM
Clark Justine L. Gonzales
Clark Justine L. Gonzales - avatar
+ 1
char c = '5', * s = "1234"; Getting the ascii value of a char : (int)c; Getting the value of the figure : (int)(c - '0'); Translating a string into an integer : atoi(s);
4th Oct 2017, 4:43 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
Hahaha no problem, it can happen to anyone ;)
4th Oct 2017, 4:36 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar