Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Binary to ASCII Question

Reply
Thread Tools

Binary to ASCII Question

 
 
dog
Guest
Posts: n/a
 
      10-23-2003
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?




 
Reply With Quote
 
 
 
 
Marco Schmidt
Guest
Posts: n/a
 
      10-23-2003
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
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      10-23-2003
In article <3f983d89$(E-Mail Removed)>,
"dog" <(E-Mail Removed)> wrote:

>: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?
>o 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 =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
dog
Guest
Posts: n/a
 
      10-23-2003

"Marco Schmidt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Sajjad Lateef
Guest
Posts: n/a
 
      10-23-2003
On Thu, 23 Oct 2003 17:22:17 -0500, dog wrote:

>
> "Marco Schmidt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.


Get a database that can store binary files, then.

Choose the right tool for the job.
 
Reply With Quote
 
David Zimmerman
Guest
Posts: n/a
 
      10-23-2003


dog wrote:

> "Marco Schmidt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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.
>
>

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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-23-2003
On Thu, 23 Oct 2003 18:11:17 -0500, "Sajjad Lateef"
<(E-Mail Removed)> wrote or quoted :

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


What is it? Check out types like BLOB CLOB for something to store raw
bytes.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-23-2003
On Thu, 23 Oct 2003 23:17:57 GMT, David Zimmerman
<(E-Mail Removed)> wrote or quoted :

>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


see http://mindprod.com/jgloss/base64.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
K P
Guest
Posts: n/a
 
      10-24-2003
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

"dog" <(E-Mail Removed)> wrote in message news:<3f985480$(E-Mail Removed)>...
> "Marco Schmidt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.

 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      10-27-2003
"dog" <(E-Mail Removed)> wrote in message
news:3f983d89$(E-Mail Removed)...
> 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.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
[FR/EN] how to convert the characters ASCII(0-255) to ASCII(0-127) Alextophi Perl Misc 8 12-30-2005 10:43 AM
Question about reading a big binary file and write it into several text (ascii) files Albert Tu Python 2 01-25-2005 03:56 AM
routine/module to translate microsoft extended ascii to plain ascii James O'Brien Perl Misc 3 03-05-2004 04:33 PM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments