Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-17-2004, 08:44 AM
|
#1
|
LQ Newbie
Registered: Jul 2004
Posts: 11
Rep:
|
string to int
hello
I'm coding an encryption algorithm and need to go from a string of text to there integer representation to encrypt them. using getline is a good way of getting the string so is there a function that would translate this to an int and then back to the string after decryption.
|
|
|
08-17-2004, 09:17 AM
|
#2
|
LQ Newbie
Registered: Jul 2004
Posts: 13
Rep:
|
You can use the function "atoi()" to convert the string to int and you can use "itoa()" to convert int to string in C.
Cheers,
rkdugar
|
|
|
08-17-2004, 09:33 AM
|
#3
|
LQ Newbie
Registered: Jul 2004
Posts: 11
Original Poster
Rep:
|
i might be doing something wrong but atoi doesnt work. its ok if your changing a number string to an integer but not a string made up of letters. all i seem to get back is 0
|
|
|
08-17-2004, 10:03 AM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Quote:
Originally posted by BadTaste
i might be doing something wrong but atoi doesnt work. its ok if your changing a number string to an integer but not a string made up of letters. all i seem to get back is 0
|
How else would it work? What does your encryption string look like? Are there other symbols besides letters? Do the non-numeric characters have a set number associated with them? I'm confused as to what you're actually looking for.
|
|
|
08-17-2004, 10:06 AM
|
#5
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
So you want the ASCII representation of each character? Just cast each individual char to an int.
For example:
Code:
#include <iostream>
using namespace std;
int main()
{
char str[6]="Hello";
for (int c=0;c<5;c++)
{
cout << (int)str[c] << endl;
}
}
Results:
72
101
108
108
111
|
|
|
08-17-2004, 10:14 AM
|
#6
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Rep:
|
atoi is used to convert "1234" to 1234 and won't take anything else as argument. What are u exactly trying to acheive?
|
|
|
08-17-2004, 11:51 AM
|
#7
|
LQ Newbie
Registered: Jul 2004
Posts: 11
Original Poster
Rep:
|
what i am trying to do is read in a line of text from a file convert it to its integer so hello would be 072101108108111 and then get back to hello. i was hoping their might be a function that could do that or i might be using the string to int functions wrong. if not then the only other way i can see would be to read in a char at a time convert to its integer check if it is less than 100 if it is add a 0 so "a" = 097 then place that in a string go to the next char b = 098 and so on until the end of the line. then convert that string 097098 to a number and thats the number that is encrypted. for me the first way would be a bit easier
|
|
|
08-17-2004, 12:08 PM
|
#8
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Code:
$ cat cvr.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char str[6] = "Hello", *s, chstr[4];
char buf[100] = "", *b;
char recon[6];
for(s = str, b = buf;*s;s++, b += 3)
sprintf(b, "%03d", *s);
printf("Converted: %s\n", buf);
chstr[3] = '\0';
for(b = buf, s = recon;*b;b += 3, s++)
{
strncpy(chstr, b, 3);
*s = atoi(chstr);
}
recon[5] = '\0';
printf("Reconstructed: %s\n", recon);
return 0;
}
...gives me...
Quote:
$ ./cvr
Converted: 072101108108111
Reconstructed: Hello
|
Is that what you're looking for?
|
|
|
08-17-2004, 12:26 PM
|
#9
|
LQ Newbie
Registered: Jul 2004
Posts: 11
Original Poster
Rep:
|
that should help thanks
|
|
|
08-18-2004, 12:16 AM
|
#10
|
Member
Registered: Aug 2002
Distribution: Debian
Posts: 540
Rep:
|
Lol! This guy is funny! 
|
|
|
08-18-2004, 12:17 AM
|
#11
|
Member
Registered: Aug 2002
Distribution: Debian
Posts: 540
Rep:
|
Guess I shouldn't say that though... he might be a newb... sorry! 
|
|
|
08-18-2004, 10:23 AM
|
#12
|
Member
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458
Rep:
|
if you want to convert a string to a int. in java there is hashcode in hte string class.
for example
Code:
string str = "hello";
int strInteger = str.hashCode();
this will convert this str in its integer representation. for example
Code:
(((((72 x 10^4)+((((68x10^3))+(((76x10^2)))+((76x10^1)))) + (79x10^0))))).
this will give you unqiue interger representation of the string hello.
for encryption and decryption hashcode are the way to go.
to convert it back to a string its just as easier, converto byteArray and then to string.
let me know if this was helpful
Last edited by djgerbavore; 08-18-2004 at 10:27 AM.
|
|
|
All times are GMT -5. The time now is 06:38 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|