Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / February 2005

Tip: Looking for answers? Try searching our database.

String to integer

Thread view: 
niallmulhare - 02 Feb 2005 11:49 GMT
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 - 02 Feb 2005 13:14 GMT
> 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
Signature

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 - 02 Feb 2005 13:47 GMT
> 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 - 02 Feb 2005 14:02 GMT
>   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 - 03 Feb 2005 15:50 GMT
> 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 ........"
[quoted text clipped - 3 lines]
>
> 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 - 05 Feb 2005 18:27 GMT
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");
Warren - 08 Feb 2005 14:33 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.