LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-17-2004, 08:44 AM   #1
BadTaste
LQ Newbie
 
Registered: Jul 2004
Posts: 11

Rep: Reputation: 0
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.
 
Old 08-17-2004, 09:17 AM   #2
rkdugar
LQ Newbie
 
Registered: Jul 2004
Posts: 13

Rep: Reputation: 0
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
 
Old 08-17-2004, 09:33 AM   #3
BadTaste
LQ Newbie
 
Registered: Jul 2004
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 08-17-2004, 10:03 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 08-17-2004, 10:06 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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
 
Old 08-17-2004, 10:14 AM   #6
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
atoi is used to convert "1234" to 1234 and won't take anything else as argument. What are u exactly trying to acheive?
 
Old 08-17-2004, 11:51 AM   #7
BadTaste
LQ Newbie
 
Registered: Jul 2004
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 08-17-2004, 12:08 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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?
 
Old 08-17-2004, 12:26 PM   #9
BadTaste
LQ Newbie
 
Registered: Jul 2004
Posts: 11

Original Poster
Rep: Reputation: 0
that should help thanks
 
Old 08-18-2004, 12:16 AM   #10
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Lol! This guy is funny!
 
Old 08-18-2004, 12:17 AM   #11
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Guess I shouldn't say that though... he might be a newb... sorry!
 
Old 08-18-2004, 10:23 AM   #12
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a int to string irfanhab Programming 6 07-30-2005 09:40 PM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM
int to string+sendto pantera Programming 3 08-19-2004 07:19 PM
Hash-Function (string to int) Hady Programming 5 04-05-2004 01:53 AM
string to int in C h/w Programming 2 12-05-2003 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration