RE: Taking UTF-8 and UTF-16 into consideration . . . |
||
![]() Ker Socrat hmm you have errors in your code :P did you actually try this? Also your code will just add a space in FRONT of the string for each char entered, so if you enter a the result is fine but if you enter abcdefg you will get 7 spaces then the binary string. The code i submitted gave the same result as your code with a lot less coding.... if you run my code you will see that it gives back the righ binary value for any ascii charater even if its 16. you can initiate i twice in the same loop. you would need to change it to :: public static String str2Bin(String str){
StringBuffer binaryString = new StringBuffer();
int strlen = str.length();
for(int i=0;i < strlen;++i){ //faster loop
int decimal = str.charAt(i);
String binary = Integer.toBinaryString(decimal);
if(decimal > -126 && decimal < 127){ //UTF-8
int trailingZeros = 8 - binary.length();
for(int j = 0; i < trailingZeros;++i)
binaryString.insert(0,"0");
}//end if
else{ //UTF-16
int trailingZeros = 16 - binary.length();
for(int k = 0;i < trailingZeros;++i)
binaryString.insert(0,"0");
}
binaryString.insert(0," ");
binaryString.append(binary);
}
return binaryString.toString();
}
that would make it work. But that is still not the result we wanna have. We want to get the actually numeric value of the ascii char like ╨ <- that is a value, then we take each of those numbers and convert it to binary. That will give the same result as the php version.Kernel Socrat X/O CAUniversity CAUniversity.org Administrator Cyberarmy Staff Member CAUniversity.org Replies:
|
||
| CyberArmy::Forum v0.6 Generated In 0.02336 seconds |