Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weird number conversion in Java

Tags:

java

I am testing something and calling the StringTokenizer and is getting some weird conversion... forget about the fact that I should be delimiting the \ in the "\7767546" but I'm just curious what's with the \11 until \77 in java

here is my code:

String path = "C:\\temp\\\\7800000\7767546.pdf";
String delimeter = "\\";
String[] values = new String[3];
int counter = 0;
StringTokenizer st = new StringTokenizer(path,delimeter); 
while(st.hasMoreTokens()){ 
           values[counter] = st.nextToken();
           System.out.println(" values[counter]" + values[counter]); 
           ++counter;
} 

here's the output:

values[counter]C:

values[counter]temp

values[counter]7800000?67546.pdf

if you notice, the \77 in my original String became ? .....is that like a unicode thing?

like image 949
ayeen c Avatar asked Mar 20 '14 20:03

ayeen c


1 Answers

As the Java Language Specification states

OctalEscape:
    \ OctalDigit
    \ OctalDigit OctalDigit
    \ ZeroToThree OctalDigit OctalDigit

OctalDigit: one of
    0 1 2 3 4 5 6 7

ZeroToThree: one of
    0 1 2 3

the following String or character literal is an octal escape

\77

In octal, the value 77 is 63 which is the ? character.

Note that this has nothing to do with the StringTokenizer. It applies to your String literal

"C:\\temp\\\\7800000\7767546.pdf"

which, if you printed out, would print as

C:\temp\\7800000?67546.pdf

because that is the value stored.

like image 67
Sotirios Delimanolis Avatar answered Oct 01 '22 22:10

Sotirios Delimanolis