1

After a lot of searching, I am unable to find what i am looking for

I have 2 questions like:

String str = "Hello"

Now I want its value in long

072101108108111L

where Ascii

072 = H

101 = e

108 = l

108 = l

111 = o

makes Hello.

How can I do this and then convert it back to string?

For example if I have a long value

 072101108108111L

then how can I get back the string "Hello" in java?

here is where I am at:

I can get string to long like this

String str = "Hello";

StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
   sb.append((int)c);
   BigInteger mInt = new BigInteger(sb.toString());
   String valu = mIn.tostring();

but value is 72 for "H"; I want it to be 072 so that the result is 3 characters, so that it fits the function to convert back to String.

Any help plz

teobais
  • 2,820
  • 1
  • 24
  • 36
android_guy
  • 152
  • 1
  • 2
  • 14
  • 3
    Welcome to the site, and happy New Year! Please provide code for your latest attempt to solve this problem independently, even if your code did not work. – Sergey Kalinichenko Jan 01 '16 at 13:30
  • What have you tried? Also, if it's always going to be 3 digits per letter you could just split the string every 3 characters, ditch the `L`, and convert the ASCII back to characters. – Arc676 Jan 01 '16 at 13:30
  • Follow this link : http://stackoverflow.com/questions/1854924/javaconvert-cast-long-to-string – technerd Jan 01 '16 at 13:36
  • i have edited my first post see where i am struggling and happy new year all :) decided to do something in android from 1st jan today :) – android_guy Jan 01 '16 at 13:37
  • Create a table ASCII, where the each character is mapped to a code, in the loop where you get each character find the code from that table, add the code to StringBuilder, reapeat. Do the same for reverse string. – Roman C Jan 01 '16 at 13:38

2 Answers2

2

Let me give you hint on how to start.

We can get the ascii character value using a for loop and append it to a string.

for(int i = 0; i < s.length(); ++i)
{
  int ch = (int)s.charAt(i);
  System.out.println(ch);
}

Prints 
72
101
108
108
111

Now, you want to add a zero if it is less than 100

StringBuilder ans = new StringBuilder();
for(...){
...
if(ch < 100){
 ans.append("0).append(ch);
}
}

Try using a String itself instead of a long.

String longValue = "072101108108111";

Now, you know that each letter is represented as 3 characters, split it by 3 characters each.

for(int i = 0; i < s.length; i += 3)
{
  int ch = Integer.parseInt(s.substring(i, i + 3));//get the integer representation of the character
  System.out.println((char)ch);
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • what about long value to string backwards – android_guy Jan 01 '16 at 13:41
  • I'm afraid that would be a problem. Try creating a long number with a 0 in the beginnning because each character represents 3 characters. Or a word which has more than 7 letters(exceeds long limit). Try using a String itself instead of a long. `String longValue = "072101108108111";`. This makes it easier. – Uma Kanth Jan 01 '16 at 13:45
  • can you give me a example with the longvalue in string bro ? – android_guy Jan 01 '16 at 13:53
1
private static String longToString(String string) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < string.length(); i = i + 3) {
        String substring = string.substring(i, i + 3);
        char ch = (char) Integer.parseInt(substring);
        sb.append(ch);
    }
    return sb.toString();
}

private static String stringToLong(String string) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < string.length(); ++i) {
        int ch = (int) string.charAt(i);
        if (ch < 100) {
            if(ch<10)
            {
            sb.append('0');
            }
            sb.append('0').append(ch);
        } else {
            sb.append(ch);
        }
    }
    return sb.toString();
}
lucky
  • 164
  • 1
  • 5