Converting String to byte array

R

Raga

Hi,

There is a code for converting String to byte array, as follows:

"
public byte[] toByteArray(String s)
{
char[] c = s.toCharArray();
int len = c.length;
byte[] b = new byte[len * 2];

for ( int i = 0 ; i < len ; i++ )
{
b[i * 2] = (byte)(c);
b[(i * 2) + 1] = (byte)(c >> 8);
}

return b;
}

"

But this isn't doing the conversion properly. For example, for the €
(euro) symbol, it converts to some other unreadable symbol. Also, same
is the case for square brackets. Any idea why this' so? What's wrong
with the above code?

Thanks.
 
R

Raga

Raga said:
Hi,

There is a code for converting String to byte array, as follows:

"
public byte[] toByteArray(String s)
{
char[] c = s.toCharArray();
int len = c.length;
byte[] b = new byte[len * 2];

for ( int i = 0 ; i < len ; i++ )
{
b[i * 2] = (byte)(c);
b[(i * 2) + 1] = (byte)(c >> 8);
}

return b;
}

"

But this isn't doing the conversion properly. For example, for the €
(euro) symbol, it converts to some other unreadable symbol. Also, same
is the case for square brackets. Any idea why this' so? What's wrong
with the above code?

the encoding format is UTF-8.
 
O

Oliver Wong

Raga said:
Hi,

There is a code for converting String to byte array, as follows:

"
public byte[] toByteArray(String s)
{
char[] c = s.toCharArray();
int len = c.length;
byte[] b = new byte[len * 2];

for ( int i = 0 ; i < len ; i++ )
{
b[i * 2] = (byte)(c);
b[(i * 2) + 1] = (byte)(c >> 8);
}

return b;
}

"

But this isn't doing the conversion properly. For example, for the ?
(euro) symbol, it converts to some other unreadable symbol. Also, same
is the case for square brackets. Any idea why this' so? What's wrong
with the above code?

the encoding format is UTF-8.


Your code seems to be contradicting this claim. It seems to be encoding
in Sun's special custom encoding (which is similar to, but distinct from,
UTF-16).

- Oliver
 
T

Thomas Fritsch

Raga said:
Raga said:
Hi,

There is a code for converting String to byte array, as follows:

"
public byte[] toByteArray(String s)
{
char[] c = s.toCharArray();
int len = c.length;
byte[] b = new byte[len * 2];

for ( int i = 0 ; i < len ; i++ )
{
b[i * 2] = (byte)(c);
b[(i * 2) + 1] = (byte)(c >> 8);
}

return b;
}

"

I think you have just re-invented the wheel. Your code above converts a
String to a UTF-16 coded byte array. Your code above is equivalent to:
public byte[] toByteArray(String s)
{
return s.getBytes("UTF-16");
}
Hence the question arises: Did you really want to decode the String to a
UTF-16 coded byte array?
the encoding format is UTF-8.
Which encoding format is UTF-8? The one you get, or the one you want?
May be you simply want:
public byte[] toByteArray(String s)
{
return s.getBytes("UTF-8");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top