0

I am trying to convert a string to integer string representation and later convert it back I will explain the requirement in detail

Suppose I have a password string "abc", I want to convert it in to a string of ascii values of each character "097098099" Then later I want to convert it back in to a string "abc"

In c this can be done easily using sprintf and scanf but in Java I am confused. Thank you.

  • 4
    Possible duplicate of [Converting String to Int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – Rohan Khude Dec 11 '16 at 11:48
  • 1
    What are you confused about? Have you looked up how to convert there and back? – Carcigenicate Dec 11 '16 at 11:50
  • You could convert each character in int via casting and concatenate them in a String – aleb2000 Dec 11 '16 at 11:50
  • 2
    Your question cause me to ask the following question: "Why?!". Also it cause me to kindly ask you to share some code that you have already implemented and concrete problems that you have with it. – AlexR Dec 11 '16 at 11:51
  • @RohanKhude he's not asking to convert a String to an int but to convert each ASCII character into it's int representation – aleb2000 Dec 11 '16 at 11:51
  • Possible duplicate of [How to convert ASCII code (0-255) to a String of the associated character?](http://stackoverflow.com/questions/7693994/how-to-convert-ascii-code-0-255-to-a-string-of-the-associated-character) – andolsi zied Dec 11 '16 at 13:41
  • Be sure to _document, in the code_, how characters correspond to digits. _Show how_ you would tackle this in C with `sprintf and scanf`, re-read the docs on [Java's printf](https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#printf-java.util.Locale-java.lang.String-java.lang.Object...-), describe what problem you are left with. – greybeard Dec 11 '16 at 17:24
  • Why? I was trying to use shamir's secret sharing algorithm to create secret shares for a password. Algorithm uses BigInteger initialised with number string(for example "123"), but actual password was an normal alphabet string like ("a@bcd"). So some how I need to convert the string password to a number string and convert it back. – saheer muhammad Dec 12 '16 at 11:06

3 Answers3

0

Use this method to convert to Ascii representation of source string:

public static void main(String[] args) {
    String source = "abc";
    String dest = convertToAsciiString(source);
    System.out.println(dest);
}

public static String convertToAsciiString(String source) {
    String dest = "";
    int index = 0;
    while (index < source.length()) {
        char c = source.charAt(index);
        int charAscii = c;
        dest += (charAscii >=100 ?charAscii : "0" + charAscii);
        index++;
    }
    return dest;
}
saeid rastak
  • 325
  • 2
  • 11
  • The questioner appears to want a three digit representation of the ASCII: 'a' -> 097, not 97, in his example. – rossum Dec 11 '16 at 12:11
0

Break a larger problem up into smaller problems. Solve each smaller problem individually and then combine the small solutions into the large solution for the whole problem.

In this case I can see two smaller problems to solve:

  1. Convert a single ASCII character to its integer representation.

  2. Combine the integer representations into the overall string

This has the feel of homework, so I'm not going to write a program for you, but I will help with some pseudocode:

myString <- "abc"
newString <- ""

for each character thisChar in myString
  // Here "&" is the string concatenation operator
  newString <- newString & convertToInteger(thisChar)
endfor

return newString

That is not Java, and there are a number of Java-specific tweaks that you should use in the Java version. You will learn them better if you find them for yourself. You could start by looking at the Javadocs for the StringBuilder class.

rossum
  • 15,344
  • 1
  • 24
  • 38
0

You could do something like this, this is the method to convert to an int representation of the ASCII string, following this example you can write the reverse method on your own making the casting in the other way around.

public static String convertToIntString(String str) {
    StringBuilder builder = new StringBuilder(str.length() * 3);
    str.chars().map(c -> (int)c).forEach(i -> builder.append((Integer.toString(i).length() == 2 ? Integer.toString(i).length() == 1 ? "00" : "0" : "") + i));
    return builder.toString();
}

The code inside the forEach(...) is to be sure that the char is written with three digits ( 097 instead of 97)

Beware the code I wrote uses the Java Stream API so will work only if you have Java 8 installed.

aleb2000
  • 452
  • 4
  • 10