Sunday, 15 April 2012

casting - Type cast issue in java (ascii value to char) -



casting - Type cast issue in java (ascii value to char) -

the problem facing occurs when seek type cast ascii values char.

for example:

(char)145 //returns ? (char)129 //also returns ?

but supposed homecoming different character. happens many other values well.

i hope have been clear enough.

ascii 7-bit encoding system. programs utilize observe if file binary or textual. characters below 32 escape characters , used directives (for instance new lines, print command)

the programme still work. character stored short (thus 16 bits). values in range don't have interpretation. means textual output of both values lead nothing. on other hand comparisons (char) 145 == (char) 129 still work (return false). because processor, there no difference between short , character.

if interested in converting value such lowest 7 bits count (this modifying value such in valid range), can utilize masking:

int value = 145; value &= 0x7f; char c = (char) value;

java casting

No comments:

Post a Comment