 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
niallmulhare Guest
|
Posted: Wed Feb 02, 2005 11:49 am Post subject: String to integer |
|
|
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
|
Posted: Wed Feb 02, 2005 1:14 pm Post subject: Re: String to integer |
|
|
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
|
Posted: Wed Feb 02, 2005 1:47 pm Post subject: Re: String to integer |
|
|
"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
|
Posted: Wed Feb 02, 2005 2:02 pm Post subject: Re: String to integer |
|
|
"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
|
Posted: Thu Feb 03, 2005 3:50 pm Post subject: Re: String to integer |
|
|
| 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
|
Posted: Sat Feb 05, 2005 6:27 pm Post subject: Re: String to integer |
|
|
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
|
Posted: Tue Feb 08, 2005 2:33 pm Post subject: Re: String to integer |
|
|
"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 |
|
 |
|
|
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
|
|