Home > Archive > Java Security > February 2005 > String to integer
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| niallmulhare 2005-02-02, 8:59 am |
| 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
| |
| Johann Burkard 2005-02-02, 4:01 pm |
| niallmulhare wrote:
> 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@news.t-online.com> )
| |
| Wannabee 2005-02-02, 4:01 pm |
|
"niallmulhare" wrote
> 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 ...
| |
| Wannabee 2005-02-02, 4:01 pm |
|
"Wannabee" wrote
> 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!
| |
| Warren 2005-02-03, 4:01 pm |
| > 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
| |
| Mr. Skeptic 2005-02-05, 4:00 pm |
| 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");
|
|
|
|
|