Binary to ASCII Question

D

dog

Binary to ascii Question

I pick a JPG a.jpg

I read in it using ServletInputStream to a byte[] array

bytes are, of course, signed one byte things
So if you got through the array you get int values between -127 and 127

If you say

String s = new String(byteArray)
char[] x = s.toCharArray();

And then go through that away you get int values way above 256

Why?

Really just wondering. I would think it would translate each byte to the
unicode equal which is what I am trying to do. Okay that didn't work, but
what did it do? Anyeasy way to go from a JPG to unicode?
Do I add 128 to all the byte value of just knock of the sign? Or niether?
 
M

Marco Schmidt

dog:

[...]
Really just wondering. I would think it would translate each byte to the
unicode equal which is what I am trying to do. Okay that didn't work, but
what did it do? Anyeasy way to go from a JPG to unicode?
Do I add 128 to all the byte value of just knock of the sign? Or niether?

Not sure what you are trying to accomplish here. Why would you want to
convert the binary data of a JPEG file to a character stream?

You can convert bytes to int values in the interval 0..255 by using
the & operator:

byte b = ...;
int i = b & 0xff;

Same with char:

char c = b & 0xff;

If you really use the String constructor, some of the original bytes
may get converted to other values because the default character
encoding of the JVM running may require a conversion.

The question remains: Why do you want the binary data as a character
array or String?

Regards,
Marco
 
S

Steve W. Jackson

dog said:
:Binary to ascii Question
:
:I pick a JPG a.jpg
:
:I read in it using ServletInputStream to a byte[] array
:
:bytes are, of course, signed one byte things
:So if you got through the array you get int values between -127 and 127
:
:If you say
:
:String s = new String(byteArray)
:char[] x = s.toCharArray();
:
:And then go through that away you get int values way above 256
:
:Why?
:
:Really just wondering. I would think it would translate each byte to the
:unicode equal which is what I am trying to do. Okay that didn't work, but
:what did it do? Anyeasy way to go from a JPG to unicode?
:Do I add 128 to all the byte value of just knock of the sign? Or niether?

Have you read the API docs on this? You'll notice that the String
constructor that accepts a byte array specifically states that it
constructs a new String by decoding the specified array of bytes using
the platform's default charset. So if that array of bytes didn't come
from Unicode or other actual character data, then you probably shouldn't
logically expect the resulting String to have any meaningful content.

= Steve =
 
D

dog

Marco Schmidt said:
dog:
Not sure what you are trying to accomplish here. Why would you want to
convert the binary data of a JPEG file to a character stream?

Have a database that only excepts characters, but wish to store images.
 
S

Sajjad Lateef

Have a database that only excepts characters, but wish to store images.

Get a database that can store binary files, then.

Choose the right tool for the job.
 
D

David Zimmerman

dog said:
Have a database that only excepts characters, but wish to store images.
Use base64 encoding to convert your byte stream to ascii characters. It
involves a 4/3 expansion but it's safe. kibjects.org has one, there's
one deep inside javax.mail somwhere, or it's easy to code from first
principles. google for base64 and you'll find it
 
K

K P

i dont know if this helps you... use Base 64 encoding.. u can convert
byte array into string array and when displaying the Image Base 64
decode it..hope this helps
we storing an Image in form of a CLOB.. using this method.. if u are
intrested please let me know if u want more details
-pvk
 
D

Dale King

dog said:
Binary to ascii Question

I pick a JPG a.jpg

I read in it using ServletInputStream to a byte[] array

bytes are, of course, signed one byte things
So if you got through the array you get int values between -127 and 127

If you say

String s = new String(byteArray)
char[] x = s.toCharArray();

And then go through that away you get int values way above 256

Why?

Here is the description of the String( byte[] ) constructor: "Constructs a
new String by decoding the specified array of bytes using the platform's
default charset."

So the array of bytes is decoded using a charset and the charset is platform
dependent. That alone should tell you that you are doing something wrong
since it means that you will get different answers on different systems. On
Unix with ISO8859-1 as the platform default you would have gotten the result
you wanted.

Most likely you are doing this on Windoze, where the platform default
encoding is Cp1252. Cp1252 has a number of special symbols in the range 0x80
to 0x9F. The range 0x80 to 0x9F in Unicode is devoted to control characters.
To map bytes to 0x80 to 0x9F to the same values in Unicode would be
incorrect because you would be changing the meaning.

For example, 0x93 in Cp1252 represents a left curly quote. 0x93 in Unicode
represents the Set Transmit State (STS) control character. The proper
Unicode value for left curly quote is 0x201C. Therefore the code you posted
will turn a 0x93 into a 0x201C so that the meaning of the symbol is
preserved.

Cp1252 also has 5 values which are undefined (0x81, 0x8D, 0x8F, 0x90, 0x9D).
Since these have no meaning in Cp1252, what do you map them to? The answer
is that they get mapped to the Unicode Replacement character 0xFFFD.

So that answers why. You should follow the advice of others and use a
database that supports blobs, or use something like Base64.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top