0

I am trying to reform a string from ascii characters. I noticed when I break down ascii it matches the number with the ascii char, this changes when I tried to do (c+=c+0) on it as seen below, I tried to revert back to the original string but it doesn't seems to work. I was wondering does the ascii number change when you append it to a string?

StringBuilder b = new StringBuilder();
char c = "e".charAt(0);
System.out.println(c);   //'e'
System.out.println(c+0); //101 -ascii
b.append(c+=c+0);
char d = b.charAt(0);
System.out.println(d-=d+0);   //blank
3
  • d-=d-0?? Perhaps you meant d+0? Commented Apr 19, 2018 at 15:06
  • What are you trying to accomplish? Your code has lots of strange things going on, like b.append(c += c+0)... it will be helpful if you tell us what you're trying to do on each line, what happens, and what you expected to happen. Commented Apr 19, 2018 at 15:06
  • well is just (c = c+c+0) and yes you can write it as c*=2 but currently I have cut the method into small pieces for debugging so c*=2 will not work for the full form. I am trying to get String b back to the original form. 'e' Commented Apr 19, 2018 at 15:07

3 Answers 3

1
  • When you use + to sum a char with and int, the result will be promoted to int.
  • When you use compound assignment operators +=, the result will be converted to left operand data type(in this case, char)

This is what happens with your code:

StringBuilder b = new StringBuilder();
char c = "e".charAt(0);
System.out.println(c);   //'e'
System.out.println(c+0); //101 -ascii
b.append(c+=c+0); // result of c+c+0 is int 202, it is converted to char Ê
char d = b.charAt(0); // char d = Ê
System.out.println(d-=d+0); // result of d-(d+0) is int 0, it will be converted to null
Sign up to request clarification or add additional context in comments.

7 Comments

actually you are right, I missed that into a zero instead. System.out.println(String.valueOf(Character.toChars(d/2))); works
@logger were you responding to the right answer? Also, System.out.println((char) (d/2)); is much simpler. All you need to do is cast the int expression d/2 into a char.
@pkpnd Are you downvoting this answer? Is it not useful?
@user6690200 Your answer doesn't show the OP what the best code practices are. Converting a char to an int should be done with a cast; printing out d-=d+0 is just asking for trouble.
@pkpnd It does. If OP dose not know what is happening with his code, how can he found out how to solve it? I did not provide a solution, but it does not mean it is not useful.
|
0

It's not clear what you're trying to accomplish, but hopefully this example helps you figure out what's going on:

public static void main(String[] args) {
    StringBuilder b = new StringBuilder();
    char c = "e".charAt(0);
    System.out.println(c);   // e
    System.out.println((int) c); // 101
    char doubleC = (char) (c + c);
    b.append(doubleC);
    char d = b.charAt(0);
    System.out.println((int) d); // 202
    System.out.println(d - c); // 101
    System.out.println((char) (d - c)); // e
}

Whether a character is printed as a letter or its ASCII value depends on its type. You can see that (int) c casts the character to its int ASCII value. Your approach of c+0 also does this but in a less intuitive way.

You also apparently want to "undo" your modification to c, but subtracting d-d always gives you 0. You need to store your value of c and subtract that from d.

5 Comments

assume you dont have access to var c, just the string b to work with.
Well.. what are you trying to do? I can't help you solve your problem if I don't know what your problem is.
You can do System.out.println(d / 2);.
so I have a string, the encoding sequence is like this....it breaks string down into characters then it encrypt by doubles then add a integer sequence number. Then it pack back into a string. Now I am trying to recover that original string.
Then you need to reverse the "encryption" scheme. Suppose your encryption changes every character c to 2*c + n. To decrypt, you need to reverse those operations in the reverse order: (d - n) / 2, where d is your encrypted character.
0

+0s don't do anything because the character is underneath a number (so it's essentially like saying 101 + 0).

d-=d+0 evaluates to d = d - d + 0, so (because we're manipulating chars) d - d = 0

If you wanted to get c from d, you'd have to do d = d - c, because 'c' is the difference between the initial and the final value.

So no, the ASCII number of a character doesn't change, you're just not doing the reverse calculation as you should be.

It's like saying: x + y = z, and then wondering why z - z != y.

You should be doing: z - x = y

@Edit To add to your comment under pkpnd's answer - if you have only String b to work with, you'd still need to know the difference (i.e. by how much you increased/decreased the number) to reverse to the original chars.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.