aspose file tools*
The moose likes Java in General and the fly likes Reading characters from a string and converting to Uniocode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading characters from a string and converting to Uniocode" Watch "Reading characters from a string and converting to Uniocode" New topic
Author

Reading characters from a string and converting to Uniocode

Jeethu Jerry
Greenhorn

Joined: Feb 13, 2014
Posts: 10
Hai all.I am new to this forum.I need all your help to improve my code which reads only a single character from the string to generate Unicode..Will it be fine if I use toCharArray()*??Please help me to work out this program as early as possible..
This is my coding..
public class A {
02 public static void getUnicode(String str_code) {
03 char h = 'h';
04 String hexStr = hexStr(h);
05 String binaryUnicode = "";
06
07 for (int i = 0; i < hexStr.length(); i+=2) {
08 String sub = hexStr.substring(i, i + 2);
09 int hexToInt = Integer.parseInt(sub, 16);
10 String intToBin = padBin(hexToInt);
11 binaryUnicode += intToBin;
12
13 System.out.printf("0x%s > %3d > %s\n", sub, hexToInt, intToBin);
14 }
15
16 System.out.printf("\nOUTPUT:\n");
17 System.out.printf("Char : %c\n", str_code.charAt(0));
18 System.out.printf("Unicode : \\u%s\n", hexStr);
19 System.out.printf("Binary : %s\n", binaryUnicode);
20 }
21
22 private static String hexStr(char ch) {
23 return String.format("%04x", (int) ch);
24 }
25
26 private static String padBin(int intVal) {
27 return String.format("%8s",
28 Integer.toBinaryString(intVal)).replace(' ', '0');
29 }
30
31 public static void main(String args[]) {
32 getUnicode("hello");
33 }
34 }
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3365
    
    9
Hi Jeethu, welcome to CodeRanch.

When you need some help and you post some code, be sure to UseCodeTags. People will be more likely to respond to your question.

Why don't you use some of the nifty methods in the Java API? String.charAt() can get a character from any position in a String. Charset can encode and decode characters. If you have to use String.format() to perform business logic, you're probably doing something wrong.
 
Consider Paul's rocket mass heater.
 
subject: Reading characters from a string and converting to Uniocode
 
Similar Threads
Integrating 2 different classes
[newbie] String.replace()/replaceAll() removed spaces
Converting Infix to Postfix Expressions
Characters in String : Unicode 16-bit to custom 32-bit
Please help, java program terminating unexpectedly without reason