Reputation: 2997
While converting String to Unicode, I am looking for a true equivalent of C# .ToString("X4") in Java.
C# Code:
char[] stringChars = "Notices".ToCharArray();
List<string> ucs2code = new List<string>();
foreach (char chr in stringChars)
{
string chrCode = ((int)chr).ToString("X4");
ucs2code.Add(chrCode);
}
string charCode = string.Join("", ucs2code);
It gives me 004E006F00740069006300650073
as a result which is exactly what I need.
Java code that I tried
char[] stringChars = "Notices".toCharArray();
String charCode = "";
for (char chr : stringChars) {
String chrCode = Integer.toHexString((int)chr);
charCode += chrCode;
}
This gives me 4e6f7469636573
It looks correct but not quite what I need.
Is there anything I missed? some suggestions would help a lot.
Upvotes: 4
Views: 3085
Reputation: 1907
I think there isn't are true equivalent.
Java has a Format
-class, if you would like to take a look at that.
function:Integer.toHexString((int)chr)
Is here the correct way to do it, so that should be it.
Upvotes: 4