[
Lists Home |
Date Index |
Thread Index
]
> I have a bunch of charefs like χ and I want to convert those in the
> approrpiate java string. What is the quickest way of doing this? A new
> DOMDoc perhaps?
>
> Can someone please share his|her experience and give me a helping hand?
The quickest code for doing this is to write a loop through the string,
with states for '&', '#', digit and ';'. Should be about twenty lines
in Java at most. I believe it is also the quickest way to write the code.
static String bunchToString(String bunch) {
StringBuffer buf=new StringBuffer();
int cc=0; char ch; int state=0; int i;
for(i=0;i!=bunch.length();++i) {
switch(ch=bunch.charAt(i)) {
case '&': state=1; break;
case '#': state=state==1?2:0; break;
case 'x': state=state==2?3:0; break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if(state==4||state==5) break;
state=state==2?4:state==3?5:0; break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
if(state==5) break;
state=state==3?5:0; break;
case ';': state=(state==4||state==5)?6:0; break;
}
switch(state) {
case 0: buf.append(ch); break;
case 1: case 2: case 3: break;
case 4: cc=cc*10+ch-'0'; break;
case 5:
cc=cc*16+('0'<=ch&&ch<='9'?ch-'0':
'A'<=ch&&ch<='F'?ch+10-'A':ch+10-'a'); break;
case 6: buf.append((char)cc); cc=0; break;
default: throw new RuntimeException("should not happen");
}
}
return buf.toString();
}
Untested, but the idea should be clear.
David Tolpin
http://davidashen.net/
|