Transform string to a unicode

Hello all,I want to know how can we transform a string to a 16-bit unicode, please?( i want to transform a string to look like "\u062a\u0635\u0628\u062d " and then store it in a string variable to use it).can you give me the code?it's urgent
[284 byte] By [ahd292000a] at [2007-10-3 4:45:36]
# 1

Create a StringBuffer

for each char in your string

append "\u" to the string buffer

convert the char to an int

format the int to a 4 char hex

append the hex chars to the string buffer.

convert the string buffer to a String.

sabre150a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 2

public class a

{

public static void main( String args[] )

{

System.out.println( getit( args[ 0 ] ) );

}

public static String getit( String what )

{

StringBuildersb = new StringBuilder();

for ( int i = 0; i < what.length(); i++ )

{

sb.append( String.format( "\\u%04x", ( int ) what.charAt( i ) ) );

}

return sb.toString();

}

}

java a 09aA

\u0030\u0039\u0061\u0041

java a 09aA獒

\u0030\u0039\u0061\u0041\u00e9\u00e1

BIJ001a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 3

Thanks alot for your code,

I want you to see the following code (part of my code):

while (class3.rs.next())

{

for ( int i = 1; i <= class3.rsmd.getColumnCount(); i++)

{

for ( int j = 0; j < class3.rs.getString(i).length(); j++ )

{

sb.append( String.format( "\\u%04x", ( int ) rs.getString(i).charAt( j ) ) );

}

String w=sb+"";

PdfPCell cell = new PdfPCell(new Phrase(w,f2));

table.addCell(cell);

}

}

document.add(table);

document.close();

Now, if you noticed these classes or objects :

PdfPCell (class), table (object ), document (object)

I used these classes from free library called (iText) if you know it.

to help me store data that i retrieve it from database in a PDF file.

my problem is when I used the above code as it is, it will stores the data that I retrieve it from database as "\u0030\u0039\u0061\u0041"

and it didn't give me the equivalent word for this unicode.

but if I changed the variable (w) in the following statement from the code:

PdfPCell cell = new PdfPCell(new Phrase(w,f2));

and put this string "\u0030\u0039\u0061\u0041" instead of it

it will store the equivalent word for this unicode in the PDF file.

I don't know what's the problem?

and why it do it like that?

second problem is when I retreive the data from the database it gave me this error (java.lang.NullPointerException)

I think it is because the size of data (rows)

because whenn I retreive one row it didn't give me this error.

so, what is wrong here?

and what I have to do?

help me please

ahd292000a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 4
> Hello all,> > I want to know how can we transform a string to a> 16-bit unicode, a small correction here for the OP is that all the Strings in java are unicode 16 bit. Strings cannot have any other encoding.
kilyasa at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 5

transform a string to a 16-bit unicode:

public class a

{

public static void main( String args[] )

{

System.out.println( getit( args[ 0 ] ) );

}

public static String getit( String what )

{

StringBuildersb = new StringBuilder();

for ( int i = 0; i < what.length(); i++ )

{

sb.append( String.format( "\\u%08x", ( int ) what.charAt( i ) ) );

}

return sb.toString();

}

}

ahd292000a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 6

I want you to see the following code (part of my code):

while (class3.rs.next())

{

for ( int i = 1; i <= class3.rsmd.getColumnCount(); i++)

{

for ( int j = 0; j < class3.rs.getString(i).length(); j++ )

{

sb.append( String.format( "\\u%04x", ( int ) rs.getString(i).charAt( j ) ) );

}

String w=sb+"";

PdfPCell cell = new PdfPCell(new Phrase(w,f2));

table.addCell(cell);

}

}

document.add(table);

document.close();

Now, if you noticed these classes or objects :

PdfPCell (class), table (object ), document (object)

I used these classes from free library called (iText) if you know it.

to help me store data that i retrieve it from database in a PDF file.

my problem is when I used the above code as it is, it will stores the data that I retrieve it from database as "\u0030\u0039\u0061\u0041"

and it didn't give me the equivalent word for this unicode.

but if I changed the variable (w) in the following statement from the code:

PdfPCell cell = new PdfPCell(new Phrase(w,f2));

and put this string "\u0030\u0039\u0061\u0041" instead of it

it will store the equivalent word for this unicode in the PDF file.

I don't know what's the problem?

and why it do it like that?

second problem is when I retreive the data from the database it gave me this error (java.lang.NullPointerException)

I think it is because the size of data (rows)

because whenn I retreive one row it didn't give me this error.

so, what is wrong here?

and what I have to do?

help me please, it's urgent

ahd292000a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 7

I am very unclear what you ar trying to do. You asked how to turn a String into UNICODE which we (or at least I) thought you meant to create a new String with each each character of the orignal converted to it's \uxxxx form. Your latest post indicates that this is not what you wanted.

I use iText to generate PDF and I place PDF documents into a database. In the sense that they are not text files, PDF files are binary files so I store them in my database as BLOBs. I don't have to convert anything to UNICODE since BLOBs hold binary data. You cannot reliably store PDF files in CLOBS.

The fact that you have a java.lang.NullPointerException is almost certainly nothing to do with UNICODE and without seeing much more code it is almost impossible to diagnose the problem. You need to print out the exception stack trace to see which line of your code is the source of the NullPointerException.

sabre150a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 8

your right that I asked how to turn a String into UNICODE

and if you noticed my code you will find that I used the code that turn a string retrieved from database into unicode then store in a PDF file,but my problem here is when I tried to store these data in the PDF File, it stored as it is, I mean it stored the unicode string as it is (string) not convert it to the equivalent character as in the following example.

I used this way (turn a String into UNICODE) because I found an example of how to put data written in a language other than english in a PDF file.

this is the example:

import java.awt.Color;

import java.io.FileOutputStream;

import com.lowagie.text.Chunk;

import com.lowagie.text.Document;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.PageSize;

import com.lowagie.text.Phrase;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.ColumnText;

import com.lowagie.text.pdf.PdfContentByte;

import com.lowagie.text.pdf.PdfPCell;

import com.lowagie.text.pdf.PdfPTable;

import com.lowagie.text.pdf.PdfWriter;

/**

* Writing RTL text such as Arabic or Hebrew.

*/

public class RightToLeft {

/**

* Writing RTL text such as Arabic or Hebrew.

* @param args no arguments needed

*/

public static void main(String[] args) {

try {

// step 1

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

// step 2

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("righttoleft.pdf"));

// step 3

document.open();

// step 4

PdfContentByte cb = writer.getDirectContent();

BaseFont bf = BaseFont.createFont("c:\\windows\\fonts\\times.ttf", BaseFont.IDENTITY_H, true);

Font f2 = new Font(bf, 24, Font.NORMAL, Color.BLUE);

float llx = 100;

float lly = 100;

float urx = 500;

float ury = 800;

ColumnText ct = new ColumnText(cb);

ct.setSimpleColumn(llx, lly, urx, ury, 24, Element.ALIGN_LEFT);

ct.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);

ct.setLeading(0, 1);

ct.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

ct.setAlignment(Element.ALIGN_CENTER);

ct.addText(new Chunk(ar1, new Font(bf, 16)));

ct.addText(new Chunk(ar2, new Font(bf, 16, Font.NORMAL, Color.red)));

ct.go();

ct.setAlignment(Element.ALIGN_JUSTIFIED);

ct.addText(new Chunk(ar3, new Font(bf, 12)));

ct.go();

ct.setAlignment(Element.ALIGN_CENTER);

ct.addText(new Chunk(ar4, new Font(bf, 14)));

ct.go();

ct.setSpaceCharRatio(PdfWriter.SPACE_CHAR_RATIO_DEFAULT);

ct.setAlignment(Element.ALIGN_CENTER);

ct.addText(new Chunk("\n\n\n", new Font(bf, 16)));

ct.addText(new Chunk(he1, new Font(bf, 16)));

ct.addText(new Chunk(he2, new Font(bf, 16, Font.NORMAL, Color.red)));

ct.go();

ct.setAlignment(Element.ALIGN_JUSTIFIED);

ct.addText(new Chunk(he3, new Font(bf, 12)));

ct.go();

ct.setAlignment(Element.ALIGN_CENTER);

ct.addText(new Chunk(he4, new Font(bf, 14)));

ct.go();

document.newPage();

String atext = "\u062a\u0635\u0628\u062d ";

PdfPTable table = new PdfPTable(5);

table.setWidthPercentage(100);

table.setRunDirection(PdfWriter.RUN_DIRECTION_NO_BIDI);

for (int k = 0; k < 5; ++k) {

PdfPCell cell = new PdfPCell(new Phrase(10, atext + k, f2));

if (k == 2) {

cell.setColspan(2);

++k;

}

table.addCell(cell);

}

table.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);

for (int k = 0; k < 5; ++k) {

PdfPCell cell = new PdfPCell(new Phrase(10, atext + k, f2));

if (k == 2) {

cell.setColspan(2);

++k;

}

table.addCell(cell);

}

table.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

for (int k = 0; k < 5; ++k) {

PdfPCell cell = new PdfPCell(new Phrase(10, atext + k, f2));

if (k == 2) {

cell.setColspan(2);

++k;

}

table.addCell(cell);

}

document.add(table);

// step 5

document.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

/** arabic text */

public static String ar1 = "\u0623\u0648\u0631\u0648\u0628\u0627, \u0628\u0631\u0645\u062c\u064a\u0627\u062a \u0627\u0644\u062d\u0627\u0633\u0648\u0628 + \u0627\u0646\u062a\u0631\u0646\u064a\u062a :\n\n";

/** arabic text */

public static String ar2 = "\u062a\u0635\u0628\u062d \u0639\u0627\u0644\u0645\u064a\u0627 \u0645\u0639 \u064a\u0648\u0646\u064a\u0643\u0648\u062f\n\n";

/** arabic text */

public static String ar3 = "\u062a\u0633\u062c\u0651\u0644 \u0627\u0644\u0622\u0646 \u0644\u062d\u0636\u0648\u0631 \u0627\u0644\u0645\u0624\u062a\u0645\u0631 \u0627\u0644\u062f\u0648\u0644\u064a " +

"\u0627\u0644\u0639\u0627\u0634\u0631 \u0644\u064a\u0648\u0646\u064a\u0643\u0648\u062f, \u0627\u0644\u0630\u064a \u0633\u064a\u0639\u0642\u062f \u0641\u064a 10-12 \u0622\u0630\u0627\u0631 1997 " +

"\u0628\u0645\u062f\u064a\u0646\u0629 \u0645\u0627\u064a\u0646\u062a\u0633, \u0623\u0644\u0645\u0627\u0646\u064a\u0627. \u0648\u0633\u064a\u062c\u0645\u0639 \u0627\u0644\u0645\u0624\u062a\u0645\u0631 " +

"\u0628\u064a\u0646 \u062e\u0628\u0631\u0627\u0621 \u0645\u0646 \u0643\u0627\u0641\u0629 \u0642\u0637\u0627\u0639\u0627\u062a \u0627\u0644\u0635\u0646\u0627\u0639\u0629 \u0639\u0644\u0649 " +

"\u0627\u0644\u0634\u0628\u0643\u0629 \u0627\u0644\u0639\u0627\u0644\u0645\u064a\u0629 \u0627\u0646\u062a\u0631\u0646\u064a\u062a \u0648\u064a\u0648\u0646\u064a\u0643\u0648\u062f, \u062d\u064a\u062b " +

"\u0633\u062a\u062a\u0645, \u0639\u0644\u0649 \u0627\u0644\u0635\u0639\u064a\u062f\u064a\u0646 \u0627\u0644\u062f\u0648\u0644\u064a \u0648\u0627\u0644\u0645\u062d\u0644\u064a \u0639\u0644\u0649 " +

"\u062d\u062f \u0633\u0648\u0627\u0621 \u0645\u0646\u0627\u0642\u0634\u0629 \u0633\u0628\u0644 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u064a\u0648\u0646\u0643\u0648\u062f \u0641\u064a " +

"\u0627\u0644\u0646\u0638\u0645 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0648\u0641\u064a\u0645\u0627 \u064a\u062e\u0635 \u0627\u0644\u062a\u0637\u0628\u064a\u0642\u0627\u062a " +

"\u0627\u0644\u062d\u0627\u0633\u0648\u0628\u064a\u0629, \u0627\u0644\u062e\u0637\u0648\u0637, \u062a\u0635\u0645\u064a\u0645 \u0627\u0644\u0646\u0635\u0648\u0635 \u0648\u0627\u0644\u062d\u0648\u0633\u0628\u0629 " +

"\u0645\u062a\u0639\u062f\u062f\u0629 \u0627\u0644\u0644\u063a\u0627\u062a.\n\n";

/** arabic text */

public static String ar4 = "\u0639\u0646\u062f\u0645\u0627 \u064a\u0631\u064a\u062f \u0627\u0644\u0639\u0627\u0644\u0645 \u0623\u0646 \u064a\u062a\u0643\u0644\u0651\u0645, \u0641\u0647\u0648 \u064a\u062a\u062d\u062f\u0651\u062b \u0628\u0644\u063a\u0629 \u064a\u0648\u0646\u064a\u0643\u0648\u062f\n\n";

/** hebrew text */

public static String he1 = "\u05d0\u05d9\u05e8\u05d5\u05e4\u05d4, \u05ea\u05d5\u05db\u05e0\u05d4 \u05d5\u05d4\u05d0\u05d9\u05e0\u05d8\u05e8\u05e0\u05d8:\n\n";

/** hebrew text */

public static String he2 = "Unicode \u05d9\u05d5\u05e6\u05d0 \u05dc\u05e9\u05d5\u05e7 \u05d4\u05e2\u05d5\u05dc\u05de\u05d9\n\n";

/** hebrew text */

public static String he3 = "\u05d4\u05d9\u05e8\u05e9\u05de\u05d5 \u05db\u05e2\u05ea \u05dc\u05db\u05e0\u05e1 Unicode \u05d4\u05d1\u05d9\u05e0\u05dc\u05d0\u05d5\u05de\u05d9 \u05d4\u05e2\u05e9\u05d9\u05e8\u05d9, \u05e9\u05d9\u05d9\u05e2\u05e8\u05da \u05d1\u05d9\u05df \u05d4\u05ea\u05d0\u05e8\u05d9\u05db\u05d9\u05dd " +

"12\u05be10 \u05d1\u05de\u05e8\u05e5 1997, \u05d1\u05de\u05d9\u05d9\u05e0\u05e5 \u05e9\u05d1\u05d2\u05e8\u05de\u05e0\u05d9\u05d4. \u05d1\u05db\u05e0\u05e1 \u05d9\u05e9\u05ea\u05ea\u05e4\u05d5 \u05de\u05d5\u05de\u05d7\u05d9\u05dd \u05de\u05db\u05dc \u05e2\u05e0\u05e4\u05d9 \u05d4\u05ea\u05e2\u05e9\u05d9\u05d9\u05d4 " +

"\u05d1\u05e0\u05d5\u05e9\u05d0 \u05d4\u05d0\u05d9\u05e0\u05d8\u05e8\u05e0\u05d8 \u05d4\u05e2\u05d5\u05dc\u05de\u05d9 \u05d5\u05d4\u05beUnicode, \u05d1\u05d4\u05ea\u05d0\u05de\u05d4 \u05dc\u05e9\u05d5\u05e7 \u05d4\u05d1\u05d9\u05e0\u05dc\u05d0\u05d5\u05de\u05d9 \u05d5\u05d4\u05de\u05e7\u05d5\u05de\u05d9, " +

"\u05d1\u05d9\u05d9\u05e9\u05d5\u05dd Unicode \u05d1\u05de\u05e2\u05e8\u05db\u05d5\u05ea \u05d4\u05e4\u05e2\u05dc\u05d4 \u05d5\u05d1\u05d9\u05d9\u05e9\u05d5\u05de\u05d9\u05dd, \u05d1\u05d2\u05d5\u05e4\u05e0\u05d9\u05dd, \u05d1\u05e4\u05e8\u05d9\u05e1\u05ea \u05d8\u05e7\u05e1\u05d8 \u05d5\u05d1\u05de\u05d7\u05e9\u05d5\u05d1 " +

"\u05e8\u05d1\u05be\u05dc\u05e9\u05d5\u05e0\u05d9.\n\n";

/** hebrew text */

public static String he4 = "\u05db\u05d0\u05e9\u05e8 \u05d4\u05e2\u05d5\u05dc\u05dd \u05e8\u05d5\u05e6\u05d4 \u05dc\u05d3\u05d1\u05e8, \u05d4\u05d5\u05d0 \u05de\u05d3\u05d1\u05e8 \u05d1\u05beUnicode\n\n";

}

if you noticed this code, you will find that the data that he want to put it in a PDF file,he already know its unicode as in (ar1,ar2,ar3,.....) and when you run this code you will get a PDF file with an Arabic and hebrew language.

that means he can convert the unicode to the equivalent character.

but in my code that I put in the previous post does not do that.

and this is my problem again:

if you noticed these classes or objects :

PdfPCell (class), table (object ), document (object)

I used these classes from free library called (iText) if you know it.

to help me store data that i retrieve it from database in a PDF file.

my problem is when I used the above code as it is, it will stores the data that I retrieve it from database as "\u0030\u0039\u0061\u0041"

and it didn't give me the equivalent word for this unicode.

but if I changed the variable (w) in the following statement from the code:

PdfPCell cell = new PdfPCell(new Phrase(w,f2));

and put this string "\u0030\u0039\u0061\u0041" instead of it

it will store the equivalent word for this unicode in the PDF file.

what can I do now?

and what is the problem in my code?

is there any improvments can I do it ?

please help

Message was edited by:

ahd292000

ahd292000a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 9

The Java compiler converts String of the form "\u0623\u0648\ to UNICODE as part of the compilation process. Since you already have your strings in UNICODE then just use them! You don't need to convert them to the "\uxxxx" form. In fact it will be wrong because iText has no way of knowing if you want to do the conversion or just output as the character string "\uxxx" without the conversion.

sabre150a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 10
JDK 1.4 can not to User StringBuilderHow to Transform string to a unicode in JDK1.4
m7777777billa at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 11
sabre150's reply is the only useful one here.Shame you didn't read it really.
ejpa at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 12

that day i just asked a question about

"Transform string to a unicode in JDK 1.42 "

why somebody typed "Shame" word ?

that is not significance at all.

/**

*@author LI

*@exception

*@param String strHouben

*@return String

*@throws UnsupportedEncodingException

*@since 1.42

*@version 2007/6/29

*/

private static String translateToUnicode(String strHouben) {

String strtemp = new String();

try {

OutputStreamWriter out = new OutputStreamWriter( new ByteArrayOutputStream() );

String unicodebyte=new String(strHouben.getBytes(out.getEncoding()),out.getEncoding());

char []a=unicodebyte.toCharArray();

StringBuffer buffer = new StringBuffer();

for(int m=0;m<a.length;m++){

buffer.append("\\u"+Integer.toHexString(a[m]));

}

strtemp= buffer.toString();

}

catch( Exception e ) {

e.printStackTrace();

}

return strtemp;

}

Message was edited by:

m7777777bill>

m7777777billa at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 13

> that day i just asked a question about

> "Transform string to a unicode in JDK 1.42 "

>

> why you typed "Shame" word ?

>

> that is not significance at all.

Your code is rubbish. The lineString unicodebyte=new String(strHouben.getBytes(out.getEncoding()),out.getEncoding());

takes the string referenced by 'strHouben' and first converts it to bytes using the character encoding out.getEncoding() which is the default character encoding since 'out' is constructed using the default character encoding. It then converts it back to a String using the exactly the same character encoding.

This means that the string pointed to by 'unicodebyte' will be exactly the same as the String pointed to by 'strHouben' as long as ALL the characters in 'strHouben' have a valid conversion under the default character encoding.

So what is the line of code doing? If all the characters of 'strHouben' have a valid conversion then nothing changes otherwise the string 'unicodebyte' is a corrupted form of 'strHouben'.

Please withdraw you code as it does not help anyone reading this thread.

sabre150a at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...
# 14
> why somebody typed "Shame" word ?I typed it because it was apt.> that is not significance at all.On the contrary. It was indeed a shame that you didn't read the thread. As for the code you've just posted, I agree with sabre150 completely. It is rubbish.
ejpa at 2007-7-14 22:49:55 > top of Java-index,Core,Core APIs...