convert string to integer and getback same string from integer value in c
I want to convert string "hello world" to some integer value and get back "hello world" string from converted integer value. I tried but getting issue while converting int to string.
I converted the string to an integer using this code:
char str[50];
int i, len; int result=0;
printf("Enter string to be converted: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
result = result * 10 + ( str[i] - '0' );
}
printf("%dn", result);
and then I tried converting it back like this:
printf("%dn", result);
int rem; int j = 0;
char result_str[50];
while (result !=0) {
result = result - '0' ;
rem = result%10 ;
result = result/10;
result_str[j] = 'a' + (rem -1);
printf("%c",result_str[j]); j++;
}
printf("result string = %s",result_str);
Sample output:
$ ./a.out
Enter string to be converted: hello
619663
619663
eccfhresult string = eccfh
When I try the string "hello world" I get a segmentation fault.
c
add a comment |
I want to convert string "hello world" to some integer value and get back "hello world" string from converted integer value. I tried but getting issue while converting int to string.
I converted the string to an integer using this code:
char str[50];
int i, len; int result=0;
printf("Enter string to be converted: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
result = result * 10 + ( str[i] - '0' );
}
printf("%dn", result);
and then I tried converting it back like this:
printf("%dn", result);
int rem; int j = 0;
char result_str[50];
while (result !=0) {
result = result - '0' ;
rem = result%10 ;
result = result/10;
result_str[j] = 'a' + (rem -1);
printf("%c",result_str[j]); j++;
}
printf("result string = %s",result_str);
Sample output:
$ ./a.out
Enter string to be converted: hello
619663
619663
eccfhresult string = eccfh
When I try the string "hello world" I get a segmentation fault.
c
3
How us what you tried and how it failed. Also show us your debugging effort.
– Sourav Ghosh
Nov 13 '18 at 6:22
What have you tried so far? Please show and somebody could help!
– Mike
Nov 13 '18 at 6:23
1
@VijayDubey Please stop posting code in comments and edit your question instead.
– Swordfish
Nov 13 '18 at 6:33
add a comment |
I want to convert string "hello world" to some integer value and get back "hello world" string from converted integer value. I tried but getting issue while converting int to string.
I converted the string to an integer using this code:
char str[50];
int i, len; int result=0;
printf("Enter string to be converted: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
result = result * 10 + ( str[i] - '0' );
}
printf("%dn", result);
and then I tried converting it back like this:
printf("%dn", result);
int rem; int j = 0;
char result_str[50];
while (result !=0) {
result = result - '0' ;
rem = result%10 ;
result = result/10;
result_str[j] = 'a' + (rem -1);
printf("%c",result_str[j]); j++;
}
printf("result string = %s",result_str);
Sample output:
$ ./a.out
Enter string to be converted: hello
619663
619663
eccfhresult string = eccfh
When I try the string "hello world" I get a segmentation fault.
c
I want to convert string "hello world" to some integer value and get back "hello world" string from converted integer value. I tried but getting issue while converting int to string.
I converted the string to an integer using this code:
char str[50];
int i, len; int result=0;
printf("Enter string to be converted: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
result = result * 10 + ( str[i] - '0' );
}
printf("%dn", result);
and then I tried converting it back like this:
printf("%dn", result);
int rem; int j = 0;
char result_str[50];
while (result !=0) {
result = result - '0' ;
rem = result%10 ;
result = result/10;
result_str[j] = 'a' + (rem -1);
printf("%c",result_str[j]); j++;
}
printf("result string = %s",result_str);
Sample output:
$ ./a.out
Enter string to be converted: hello
619663
619663
eccfhresult string = eccfh
When I try the string "hello world" I get a segmentation fault.
c
c
edited Nov 13 '18 at 7:08
Broman
6,246112241
6,246112241
asked Nov 13 '18 at 6:17
Vijay Dubey
64
64
3
How us what you tried and how it failed. Also show us your debugging effort.
– Sourav Ghosh
Nov 13 '18 at 6:22
What have you tried so far? Please show and somebody could help!
– Mike
Nov 13 '18 at 6:23
1
@VijayDubey Please stop posting code in comments and edit your question instead.
– Swordfish
Nov 13 '18 at 6:33
add a comment |
3
How us what you tried and how it failed. Also show us your debugging effort.
– Sourav Ghosh
Nov 13 '18 at 6:22
What have you tried so far? Please show and somebody could help!
– Mike
Nov 13 '18 at 6:23
1
@VijayDubey Please stop posting code in comments and edit your question instead.
– Swordfish
Nov 13 '18 at 6:33
3
3
How us what you tried and how it failed. Also show us your debugging effort.
– Sourav Ghosh
Nov 13 '18 at 6:22
How us what you tried and how it failed. Also show us your debugging effort.
– Sourav Ghosh
Nov 13 '18 at 6:22
What have you tried so far? Please show and somebody could help!
– Mike
Nov 13 '18 at 6:23
What have you tried so far? Please show and somebody could help!
– Mike
Nov 13 '18 at 6:23
1
1
@VijayDubey Please stop posting code in comments and edit your question instead.
– Swordfish
Nov 13 '18 at 6:33
@VijayDubey Please stop posting code in comments and edit your question instead.
– Swordfish
Nov 13 '18 at 6:33
add a comment |
1 Answer
1
active
oldest
votes
There is absolutely no way to convert a string to an int and then back again the way you want.
An int
is typically 4 bytes, and you're using a char[50]
which is 50 bytes. This means that an int
can have 2³² different values, while a char[50]
can have 2⁴⁰⁰ different values. So it is simply impossible to map them one to one.
Let's take an example outside the realm of code and computers and just focus on numbers. Can you imagine a method to convert a two-digit number to a one-digit number and back? If this was possible, then we would not need two digits in the first place. If such a method existed, you would be able to store an infinite amount of data in a single bit.
You can convert a char[4]
to an int
and back. It's actually really easy. This code will print abcd
.
char str[4] = { 'a', 'b', 'c', 'd'};
// Convert to int
int result = *(int*) str;
char newstr[4];
// Convert back
char * ptr = (char*) &result;
for(int i=0; i<4; i++)
newstr[i] = ptr[i];
printf("%.4sn", newstr);
Note that I completely ignored termination of the string, and just instructed printf
to stop after four characters.
Oh, and never use the gets
function. It's dangerous.
1
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274913%2fconvert-string-to-integer-and-getback-same-string-from-integer-value-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is absolutely no way to convert a string to an int and then back again the way you want.
An int
is typically 4 bytes, and you're using a char[50]
which is 50 bytes. This means that an int
can have 2³² different values, while a char[50]
can have 2⁴⁰⁰ different values. So it is simply impossible to map them one to one.
Let's take an example outside the realm of code and computers and just focus on numbers. Can you imagine a method to convert a two-digit number to a one-digit number and back? If this was possible, then we would not need two digits in the first place. If such a method existed, you would be able to store an infinite amount of data in a single bit.
You can convert a char[4]
to an int
and back. It's actually really easy. This code will print abcd
.
char str[4] = { 'a', 'b', 'c', 'd'};
// Convert to int
int result = *(int*) str;
char newstr[4];
// Convert back
char * ptr = (char*) &result;
for(int i=0; i<4; i++)
newstr[i] = ptr[i];
printf("%.4sn", newstr);
Note that I completely ignored termination of the string, and just instructed printf
to stop after four characters.
Oh, and never use the gets
function. It's dangerous.
1
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
add a comment |
There is absolutely no way to convert a string to an int and then back again the way you want.
An int
is typically 4 bytes, and you're using a char[50]
which is 50 bytes. This means that an int
can have 2³² different values, while a char[50]
can have 2⁴⁰⁰ different values. So it is simply impossible to map them one to one.
Let's take an example outside the realm of code and computers and just focus on numbers. Can you imagine a method to convert a two-digit number to a one-digit number and back? If this was possible, then we would not need two digits in the first place. If such a method existed, you would be able to store an infinite amount of data in a single bit.
You can convert a char[4]
to an int
and back. It's actually really easy. This code will print abcd
.
char str[4] = { 'a', 'b', 'c', 'd'};
// Convert to int
int result = *(int*) str;
char newstr[4];
// Convert back
char * ptr = (char*) &result;
for(int i=0; i<4; i++)
newstr[i] = ptr[i];
printf("%.4sn", newstr);
Note that I completely ignored termination of the string, and just instructed printf
to stop after four characters.
Oh, and never use the gets
function. It's dangerous.
1
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
add a comment |
There is absolutely no way to convert a string to an int and then back again the way you want.
An int
is typically 4 bytes, and you're using a char[50]
which is 50 bytes. This means that an int
can have 2³² different values, while a char[50]
can have 2⁴⁰⁰ different values. So it is simply impossible to map them one to one.
Let's take an example outside the realm of code and computers and just focus on numbers. Can you imagine a method to convert a two-digit number to a one-digit number and back? If this was possible, then we would not need two digits in the first place. If such a method existed, you would be able to store an infinite amount of data in a single bit.
You can convert a char[4]
to an int
and back. It's actually really easy. This code will print abcd
.
char str[4] = { 'a', 'b', 'c', 'd'};
// Convert to int
int result = *(int*) str;
char newstr[4];
// Convert back
char * ptr = (char*) &result;
for(int i=0; i<4; i++)
newstr[i] = ptr[i];
printf("%.4sn", newstr);
Note that I completely ignored termination of the string, and just instructed printf
to stop after four characters.
Oh, and never use the gets
function. It's dangerous.
There is absolutely no way to convert a string to an int and then back again the way you want.
An int
is typically 4 bytes, and you're using a char[50]
which is 50 bytes. This means that an int
can have 2³² different values, while a char[50]
can have 2⁴⁰⁰ different values. So it is simply impossible to map them one to one.
Let's take an example outside the realm of code and computers and just focus on numbers. Can you imagine a method to convert a two-digit number to a one-digit number and back? If this was possible, then we would not need two digits in the first place. If such a method existed, you would be able to store an infinite amount of data in a single bit.
You can convert a char[4]
to an int
and back. It's actually really easy. This code will print abcd
.
char str[4] = { 'a', 'b', 'c', 'd'};
// Convert to int
int result = *(int*) str;
char newstr[4];
// Convert back
char * ptr = (char*) &result;
for(int i=0; i<4; i++)
newstr[i] = ptr[i];
printf("%.4sn", newstr);
Note that I completely ignored termination of the string, and just instructed printf
to stop after four characters.
Oh, and never use the gets
function. It's dangerous.
edited Nov 13 '18 at 7:04
answered Nov 13 '18 at 6:50
Broman
6,246112241
6,246112241
1
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
add a comment |
1
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
1
1
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@VijayDubey To do what?
– Broman
Nov 13 '18 at 7:04
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
@Broman is completely right, its nonsense, what are you trying to do, there is no technical way. Theoretically, you can implement two options - use ascii code of every and each character in the string , or use pointers and print addresses, but addresses of pointers are not exactly the int numbers
– xxxvodnikxxx
Nov 13 '18 at 7:09
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
Thanks @Broman.
– Vijay Dubey
Nov 13 '18 at 10:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274913%2fconvert-string-to-integer-and-getback-same-string-from-integer-value-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
How us what you tried and how it failed. Also show us your debugging effort.
– Sourav Ghosh
Nov 13 '18 at 6:22
What have you tried so far? Please show and somebody could help!
– Mike
Nov 13 '18 at 6:23
1
@VijayDubey Please stop posting code in comments and edit your question instead.
– Swordfish
Nov 13 '18 at 6:33