In Java, is there a simple method to convert the format of a given string? For example, I have the string "test22". I'd like the binary value and hex value. As well as possibly the ascii values of each character?
My solution would be to take the String, convert it to a char array, and then convert the integer values of the char array into binary or hex through the Integer.toBinaryString() or Integer.toHexString() or Integer.toOctalString() if you would like.
just replace binary string with hex and the function will do the same thing
public String convertToBinary(String str){
char [] array = str.toCharArray();
String binaryToBeReturned = "";
for(int i=0;i<str.length();i++){
binaryToBeReturned += Integer.toBinaryString((int)array[i]) + " ";
}
return binaryToBeReturned;
}
Also to get the ASCII values of the String int value = (int)string.charAt(i); will get the ASCII value.
I added a space just for formatting, not sure how you needed it formatted, and this is just a simple implementation.
Related
I have a String containing ASCII representation of a character i.e.
String test = "0x07";
Is there a way I can somehow parse it to its character value.
I want something like
char c = 0x07;
But what the character exactly is, will be known only by reading the value in the string.
You have to add one step:
String test = "0x07";
int decimal = Integer.decode(test);
char c = (char) decimal;
I am converting a string into hexadecimal string and i want to convert that into octal string
I am doing it as follows
String s="I found the reason 2 days ago i was too bussy to update you.The problem was that the UDHL was missing. I should have added 06 at the beginning. it all works fine.For some reason I thought kannel adds this by itself (not very difficult...), but now I know it doesn't...";
String hex = String.format("%040x", new BigInteger(1, s.getBytes("UTF-8")));
Output is
4920666f756e642074686520726561736f6e203220646179732061676f20692077617320746f6f20627573737920746f2075706461746520796f752e5468652070726f626c656d20776173207468617420746865205544484c20776173206d697373696e672e20492073686f756c6420686176652061646465642030362061742074686520626567696e6e696e672e2020697420616c6c20776f726b732066696e652e466f7220736f6d6520726561736f6e20492074686f75676874206b616e6e656c2061646473207468697320627920697473656c6620286e6f74207665727920646966666963756c742e2e2e292c20627574206e6f772049206b6e6f7720697420646f65736e27742e2e2e
I need to convert hex to octal string. I tried like this
String octal = Integer.toOctalString(Integer.parseInt(hex,16));
But as expected it gave me number format exception as hex string have some characters in it.
I want to know how can i conver hex string to octal string.
As per the discussion in the comments, you want to convert each byte individually and convert them to octal:
String s = "My string to convert";
byte[] bytes = s.getBytes("UTF-8");
for (byte b : bytes) {
String octalValue = Integer.toString(b, 8);
// Do whatever
}
The problem is, that your input (String hex) is to big to be stored in a single integer.
3 hex digits correspond to 4 octal digits. So split your input string in chunks of three digits, use the conversion you already figured out, and concatenate the outputs.
I need to generate the hexadecimal code of Java characters into strings, and parse those strings again later. I found here that parsing can be performed as following:
char c = "\u041f".toCharArray()[0];
I was hoping for something more elegant like Integer.valueOf() for parsing.
How about generating the hexadecimal unicode properly?
This will generate a hex string representation of the char:
char ch = 'ö';
String hex = String.format("%04x", (int) ch);
And this will convert the hex string back into a char:
int hexToInt = Integer.parseInt(hex, 16);
char intToChar = (char)hexToInt;
After doing some deeper reading, the javadoc says the Character methods based on char parameters do not support all unicode values, but those taking code points (i.e., int) do.
Hence, I have been performing the following test:
int codePointCopyright = Integer.parseInt("00A9", 16);
System.out.println(Integer.toHexString(codePointCopyright));
System.out.println(Character.isValidCodePoint(codePointCopyright));
char[] toChars = Character.toChars(codePointCopyright);
System.out.println(toChars);
System.out.println();
int codePointAsian = Integer.parseInt("20011", 16);
System.out.println(Integer.toHexString(codePointAsian));
System.out.println(Character.isValidCodePoint(codePointAsian));
char[] toCharsAsian = Character.toChars(codePointAsian);
System.out.println(toCharsAsian);
and I am getting:
Therefore, I should not talk about char in my question, but rather about array of chars, since Unicode characters can be represented with more than one char. On the other side, an int covers it all.
On String level:
The following uses not char but int, say for Chinese, but is also adequate for chars.
int cp = "\u041f".codePointAt(0);
String s = new String(Character.toChars(cp));
On native2ascii level:
If you want to convert back and forth between \uXXXX and Unicode character, use from apache, commons-lang the StringEscapeUtils:
String t = StringEscapeUtils.escapeJava(s + "ö");
System.out.println(t);
On the command-line native2ascii can convert back and forth files between u-escaped and say UTF-8.
How do you convert from string to char if, for example, the string is entered as an argument in command line (not using scanner). E.g. how would you convert "abcd" to char?
String input = args[0]
String [] part = input.split("");
//splits string into 2 parts (action and characters to encode)
String action = part[0];
// action is what is done to letter i.e. decrypt or encrypt
String plainText = part[1];
char [] letters = plainText.toCharArray();
If you want to convert a String with more than 2 characters to a char array:
String hello = "Hello";
char[] char_array = hello.toCharArray();
Otherwise if you only need one specific char, use hello.charAt(0).
Please notice that you only can store one single 16bit Unicode character inside a char datatype. That's the reason why you need an array for more than 2 characters.
I want to format int numbers as hex strings. System.out.println(Integer.toHexString(1)); prints 1 but I want it as 0x00000001. How do I do that?
Try this
System.out.println(String.format("0x%08X", 1));
You can use the String.format to format an integer as a hex string.
System.out.println(String.format("0x%08X", 1));
That is, pad with zeros, and make the total width 8. The 1 is converted to hex for you. The above line gives: 0x00000001 and
System.out.println(String.format("0x%08X", 234));
gives: 0x000000EA
From formatting syntax documented on Java's Formatter class:
Integer intObject = Integer.valueOf(1);
String s = String.format("0x%08x", intObject);
System.out.println(s);
Less verbose:
System.out.printf("0x%08x", 1); //"Use %0X for upper case letters
I don't know Java too intimately, but there must be a way you can pad the output from the toHexString function with a '0' to a length of 8. If "0x" will always be at the beginning, just tack on that string to the beginning.
Java 17+
There is a new immutable class dedicated to conversion into and formatting hexadecimal numbers. The easiest way to go is using HexFormat::toHexDigits which includes leading zeroes:
String hex = "0x" + HexFormat.of().toHexDigits(1);
// 0x00000001
Beware, one has to concatenate with the "0x" prefix as such method ignores defined prefixes and suffixes, so the following snippet doesn't work as expected (only HexFormat::formatHex methods work with them):
String hex = HexFormat.of().withPrefix("0x").toHexDigits(1);
// 00000001
Returns the eight hexadecimal characters for the int value. Each nibble (4 bits) from most significant to least significant of the value is formatted as if by toLowHexDigit(nibble). The delimiter, prefix and suffix are not used.
Alternatively use the advantage of HexFormat::formatHex formatting to two hexadecimal characters, and a StringBuilder as an Appendable prefix containing "0x":
Each byte value is formatted as the prefix, two hexadecimal characters selected from uppercase or lowercase digits, and the suffix.
StringBuilder hex = HexFormat.of()
.formatHex(new StringBuilder("0x"), new byte[] {0, 0, 0, 1});
// 0x00000001
StringBuilder hex = HexFormat.of()
.formatHex(new StringBuilder("0x"), ByteBuffer.allocate(4).putInt(1).array());
// 0x00000001
You can use a java.util.Formatter or the printf method on a PrintStream.
This is String extension for Kotlin
//if lengthOfResultTextNeeded = 3 and input String is "AC", the result is = "0AC"
//if lengthOfResultTextNeeded = 4 and input String is "AC", the result is = "00AC"
fun String.unSignedHex(lengthOfResultTextNeeded: Int): String {
val count =
lengthOfResultTextNeeded - this.length
val buildHex4DigitString = StringBuilder()
var i = 1
while (i <= count) {
buildHex4DigitString.append("0")
++i
}
return buildHex4DigitString.toString() + this
}