2

I am unable to convert extended ASCII characters(having code greater than 128) into their codes.

I am using (int)'�' for conversion but its is giving

� -- 65533

I am using the following java function:

            static String decodeCandidateId2(String CandidateId ){     

            byte[] valueDecoded= Base64.decodeBase64(CandidateId.getBytes());
            CandidateId=new String(valueDecoded);
            String key="@!#&%$#@&^%$";
            String output="";

            for(int i=0; i<CandidateId.length(); i++) {
                int ascii=0;
                ascii=(int)CandidateId.charAt(i)-((int)key.charAt((i-1) % key.length()));                       

                output += Character.toString ((char) ascii);            
            }           
            return output;
        }

if CandidateId = "VXJaWl9lVlV0XpRbZHVZWFpeXVuAV5NVW3BRW19XVQ==" current output = 12979@2248゚6@5854998ᄑ1゚07008921, but i need to get 12979@224866@5854998@1507008921 as output.

Can anyone please help me to get the correct code.

4
  • You mean something like Character.toString( (char) 65533 );? Commented Oct 3, 2017 at 7:57
  • 1
    No, I am using ascii= (int)CandidateId.charAt(i); where CandidateId.charAt(i) having ascii code >128 Commented Oct 3, 2017 at 8:06
  • 2
    What makes you think 65533 is not the correct code? The character you've entered in your question is U+FFFD, the "replacement character" which is used when a character cannot be represented in Unicode. What character were you trying to put in your source code, and what character set did you save the source code as? Also, what encoding did you specify to the compiler with the -encoding switch, if any? Note: there is no such thing as an ASCII character with a code point of 128 or higher. ASCII is a seven bit encoding. Commented Oct 3, 2017 at 8:11
  • � UTF-16 (decimal) 65,533 Also, see java.lang.Character. In general, arithmetic on characters makes little sense. Convert to bytes using an agreed upon encoding and treat the bytes numerically. Commented Oct 3, 2017 at 13:21

1 Answer 1

3

Strictly speaking, there is no such thing as "Extended ASCII" - or there are several different colloquial definitions of this non-standard term. ASCII codes are from 0 to 127. Full stop.

The Java type char has values ranging from 0 to 65535, which are code points in the Basic Multilingual Plane of the Unicode character set.

Your encoding algorithm uses 16-bit subtraction. "Negative" values will be in the range 32768 to 65535 (since char values are unsigned). However, you seem to want to only deal with values in the range 0 to 255. To do that, you can force your arithmetic to be modulo 256 - e.g. by ANDing the result with 0xFF.

Sign up to request clarification or add additional context in comments.

3 Comments

Also Strings can't be built from a bunch of bytes and be assumed that the bytes are taken as "ASCII or something like that". When you build a String from a byte array Java will decode the bytes using a charset. That means if the bytes don't actually represent a text and are just binary garbage, decoding them with a charset will fail and produce error marks. When you want to deal with a sequence of bytes, use a byte array. Don't try and shove them in a String, that won't work and is irrational thinking. Strings are Strings, arrays are arrays.
ascii=Byte.toUnsignedInt(tempbyte)-((int)key.charAt((i-1) % key.length())); worked for me. thanks Dodgy.
@MontiChandra can you edit your question with solved answer as well? where does the tempbyte referred? *i didn't see which 1 to use in your current code

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.