Binary to ASCII Question

Discussion in 'Java' started by dog, Oct 23, 2003.

  1. dog

    dog Guest

    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?
     
    dog, Oct 23, 2003
    #1
    1. Advertising

  2. 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
     
    Marco Schmidt, Oct 23, 2003
    #2
    1. Advertising

  3. In article <3f983d89$>,
    "dog" <> 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?
    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Oct 23, 2003
    #3
  4. dog

    dog Guest

    "Marco Schmidt" <> wrote in message
    news:...
    > 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.
     
    dog, Oct 23, 2003
    #4
  5. On Thu, 23 Oct 2003 17:22:17 -0500, dog wrote:

    >
    > "Marco Schmidt" <> wrote in message
    > news:...
    >> 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.
     
    Sajjad Lateef, Oct 24, 2003
    #5
  6. dog wrote:

    > "Marco Schmidt" <> wrote in message
    > news:...
    >
    >>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
     
    David Zimmerman, Oct 24, 2003
    #6
  7. dog

    Roedy Green Guest

    On Thu, 23 Oct 2003 18:11:17 -0500, "Sajjad Lateef"
    <> 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.
     
    Roedy Green, Oct 24, 2003
    #7
  8. dog

    Roedy Green Guest

    On Thu, 23 Oct 2003 23:17:57 GMT, David Zimmerman
    <> 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.
     
    Roedy Green, Oct 24, 2003
    #8
  9. dog

    K P Guest

    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" <> wrote in message news:<3f985480$>...
    > "Marco Schmidt" <> wrote in message
    > news:...
    > > 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.
     
    K P, Oct 24, 2003
    #9
  10. dog

    Dale King Guest

    "dog" <> wrote in message
    news:3f983d89$...
    > 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.
     
    Dale King, Oct 27, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens
    Replies:
    8
    Views:
    3,070
    John Harrison
    Jul 15, 2003
  2. TOXiC
    Replies:
    5
    Views:
    1,332
    TOXiC
    Jan 31, 2007
  3. James O'Brien
    Replies:
    3
    Views:
    316
    Ben Morrow
    Mar 5, 2004
  4. Alextophi
    Replies:
    8
    Views:
    587
    Alan J. Flavell
    Dec 30, 2005
  5. bruce
    Replies:
    38
    Views:
    334
    Mark Lawrence
    Nov 1, 2013
Loading...

Share This Page