我输入了一个UNICODE字符串,我想把它转换为\ uxxxx格式但如果我写这段代码
class Master(numOfWorkers: Int, numOfMessages: Int, numOfElements: Int, listener: ActorRef) extends Actor {
var pi: Double = _
var numOfResults: Int = _
val start: Long = System.currentTimeMillis
val workerRouter = {
val routees = Vector.fill(5) {
val r = context.actorOf(Props[Worker])
context watch r
ActorRefRoutee(r)
}
Router(RoundRobinRoutingLogic(), routees)
}
def receive = {
case Calculate => for (i <- 0 until numOfMessages) workerRouter.route(Work(i * numOfElements, numOfElements), sender())
case Result(value) =>
pi += value
numOfResults += 1
if (numOfResults == numOfMessages) {
listener ! PiApproximation(pi, duration = (System.currentTimeMillis - start).millis)
context.stop(self)
}
}
}
我有这个结果:
String inString = "èaéKlüfsäöråédéroße";
for (int i = 0; i < inString.length(); i++) {
Integer charCode = (int)inString.charAt(i);
result += "\\u" + strlenFix(charCode.toString().toUpperCase());
}
static public String strlenFix(String str) {
while (str.length()<4) str="0"+str;
return str;
}
答案 0 :(得分:1)
试试这个
public static void main(String[] args) {
String result = "";
String inString = "èaéKlüfsäöråédéroße";
for (int i = 0; i < inString.length(); i++) {
Integer charCode = (int)inString.charAt(i);
result += "\\u" + Integer.toHexString(charCode) ;
}
System.out.println(result);
}
static public String strlenFix(String str) {
while (str.length()<4) str="0"+str;
return str;
{
答案 1 :(得分:1)
再次在引用上调用静态方法!
正因为如此,您认为charCode.toString(16)
会在 toString
上调用charCode
并返回其16个基本版本,但会将其编译为{{1} }。
你可能想要像Integer.toString(16)
这样的东西。
答案 2 :(得分:0)
所以,就这样做:
public static String unicodeEscaped(char ch) {
if (ch < 0x10) {
return "\\u000" + Integer.toHexString(ch);
} else if (ch < 0x100) {
return "\\u00" + Integer.toHexString(ch);
} else if (ch < 0x1000) {
return "\\u0" + Integer.toHexString(ch);
}
return "\\u" + Integer.toHexString(ch);
}
String inString = "èaéKlüfsäöråédéroße";
for (int i = 0; i < inString.length(); i++) {
Integer charCode = (int)inString.charAt(i);
result += unicodeEscaped(charCode);
}
答案 3 :(得分:0)
查看以下代码段
public static void main(String[] args) {
String result = "";
String inString = "èaéKlüfsäöråédéroße";
for (int i = 0; i < inString.length(); i++) {
result += "\\u" + Integer.toHexString(inString.charAt(i) | 0x10000)+" ";
}
System.out.println(result);
}
答案 4 :(得分:0)
那些\uXXXX
转义符通常后跟一个4位十六进制号。
将代码点转换为此类Unicode转义的最简单方法是使用String.format
:
String escaped = String.format("\\u%04X", (int) codePoint);
你可以放弃你的strlenFix
功能,所以你的循环就是:
for (int i = 0; i < inString.length(); i++) {
result += String.format("\\u%04x", (int) inString.charAt(i));
}