AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String to integer

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Security and Java
View previous topic :: View next topic  
Author Message
niallmulhare
Guest





PostPosted: Wed Feb 02, 2005 11:49 am    Post subject: String to integer Reply with quote



Im nearly imbarresed to ask this question ,but how can I convert a string
like "niall Brennan" to its integer equivlent i.e "14 9 1 12 12 ........"

If you knwo I would be a very happy person thank you. ( its in this
section because im applying it to as RSA encryption which I wrote.

Niall

Back to top
Johann Burkard
Guest





PostPosted: Wed Feb 02, 2005 1:14 pm    Post subject: Re: String to integer Reply with quote



niallmulhare wrote:
Quote:
Im nearly imbarresed to ask this question ,but how can I convert a string
like "niall Brennan" to its integer equivlent i.e "14 9 1 12 12 ........"


public String convert(String in) {
int l = in.length();
StringBuffer out = new StringBuffer(l * 3);
char c;
for (int i = 0; i < l; ++i) {
c = Character.toLowerCase(in.charAt(i));
if (Character.isLetter(c)) {
out.append(c - 96);
out.append(' ');
}
}
return out.toString();
}

Untested. And only works with ASCII data.

Johann
--
Die _wunde_ Stelle ist Dein voellig absurdes Verhalten, mit dem
Du auch noch gegenueber treten wolltest.
(*Tönnes in <9vomdu$9sn$00$1 (AT) news (DOT) t-online.com>)

Back to top
Wannabee
Guest





PostPosted: Wed Feb 02, 2005 1:47 pm    Post subject: Re: String to integer Reply with quote




"niallmulhare" wrote
Quote:
Im nearly imbarresed to ask this question ,but how can I convert a string
like "niall Brennan" to its integer equivlent i.e "14 9 1 12 12 ........"

If you knwo I would be a very happy person thank you. ( its in this
section because im applying it to as RSA encryption which I wrote.

How about the following?
I suppose you want to economically convert a String into a BigInteger. If
economy is not your goal then there are other, more simple solutions.


import java.math.BigInteger;


// Enumerate in CHARSET all the characters to be used
public String CHARSET = ". ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ-1234567890,/&+'";

public int NBROFCHARS = CHARSET.length();
public BigInteger MULTIPLIER = new BigInteger("" + NBROFCHARS);


private static BigInteger textToNumber(String text) {
BigInteger result = BigInteger.ZERO;
text = text.toUpperCase();
for (int i = text.length() - 1; i >= 0; i--) {
int place = CHARSET.indexOf("" + text.charAt(i));
if (place < 0)
place = 1; // unknown replaced by a space because space is
2nd in CHARSET
result = result.multiply(MULTIPLIER);
BigInteger tobeadded = new BigInteger("" + place);
result = result.add(tobeadded);
}
return result;
}


And the reverse transformation, that is BigInteger to String :


public String numberToString(BigInteger number)
{
StringBuffer text = new StringBuffer();
while (number.compareTo(BigInteger.ZERO) > 0) {
BigInteger[] b = number.divideAndRemainder(MULTIPLIER );
number = b[0];
int place= b[1].intValue();
char c = CHARSET.charAt(place);
text.append(c);
}
return text.toString();
}


This is a fraction from a program. I made some changes, hope I got it all
right ...



Back to top
Wannabee
Guest





PostPosted: Wed Feb 02, 2005 2:02 pm    Post subject: Re: String to integer Reply with quote


"Wannabee" wrote
Quote:
private static BigInteger textToNumber(String text) {

Correction : not static if CHARSET and MULTIPLIER are not static ...

I hope there are not many other mistakes ... I hope you get the idea!



Back to top
Warren
Guest





PostPosted: Thu Feb 03, 2005 3:50 pm    Post subject: Re: String to integer Reply with quote

Quote:
Im nearly imbarresed to ask this question ,but how can I convert a string
like "niall Brennan" to its integer equivlent i.e "14 9 1 12 12 ........"

If you knwo I would be a very happy person thank you. ( its in this
section because im applying it to as RSA encryption which I wrote.

Niall

Just curious, but why are you converting to the index of the characters
(plus one) in the alphabet?

String.toByteArray() will give you the characters as bytes, which you can
cast up to an int for each byte, but you'll also have to take 'a' from each
byte and add 1 in order to get the numbers you want.

And are you sure it's a string of numbers you want, and not an array?

w
a
z




Back to top
Mr. Skeptic
Guest





PostPosted: Sat Feb 05, 2005 6:27 pm    Post subject: Re: String to integer Reply with quote

I think the previous poster was thinking of the String.getBytes()
method. For example if you were experimenting with RSA encryption and
you had defined a BigInteger n (the modulus), BigInteger e (encryption
exponent), and BigInteger d(decryption exponent), then
consider the following code fragment

String plain = "good afternoon";
BigInteger plainBI = new BigInteger(plain.getBytes());
System.out.println("the plaintext string as a BigInteger is "
+plainBI.toString(16));
BigInteger cipher = plainBI.modPow(e, n);
System.out.println("the ciphertext string as a BigInteger is "
+cipher.toString(16));
BigInteger decrypt = cipher.modPow(d, n);
System.out.println("the decrypt string as a BigInteger is "
+decrypt.toString(16));
String decrypted = new String (decrypt.toByteArray());
if (! decrypted.equals(plain))
System.out.println("Something is broken");

Back to top
Warren
Guest





PostPosted: Tue Feb 08, 2005 2:33 pm    Post subject: Re: String to integer Reply with quote

"Mr. Skeptic" <ghstark (AT) gmail (DOT) com> wrote

Quote:
I think the previous poster was thinking of the String.getBytes()
method.

Er, yeah, sorry about that. I've been doing a lot of stuff with
ByteArrayOutputStream recently, and that has a toByteArray() method.
Slightly different method name, does the same thing.. :-)

Still, what you said above was more or less what I was getting at.

thanks for the clarification

w
a
z



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Security and Java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.