how to convert bytearray into char

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andremarim
    New Member
    • Jul 2007
    • 3

    how to convert bytearray into char

    Hi, i'm new in java and i wanna know how can i convert a bytearray into a char. Actually I need to take a data from a file (this file is written in a byte code, whose extensio is .dac) and to convert this data into a char.
    Please help me, and I would like to ask to explain me step by step all the process, including if i will need to put any import im my program.


    Thanks a lot.........and remarim
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by andremarim
    Hi, i'm new in java and i wanna know how can i convert a bytearray into a char. Actually I need to take a data from a file (this file is written in a byte code, whose extensio is .dac) and to convert this data into a char.
    Please help me, and I would like to ask to explain me step by step all the process, including if i will need to put any import im my program.


    Thanks a lot.........and remarim
    Find a method that converts into a number (I'm assuming we're talking about unsigned) or make one yourself and then simply cast it as a char to convert it via ASCII...

    Good luck,

    -blazed

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by andremarim
      Hi, i'm new in java and i wanna know how can i convert a bytearray into a char. Actually I need to take a data from a file (this file is written in a byte code, whose extensio is .dac) and to convert this data into a char.
      Please help me, and I would like to ask to explain me step by step all the process, including if i will need to put any import im my program.


      Thanks a lot.........and remarim
      First a remark:

      Read the "article index" post in the Java Articles section (see the menu bar
      at the top of this page). At the end of that index is a link to the complete API
      documentation for all the core classes. Download that documentation for regular
      inspection and reading. You'll find detailed documentation there for every class
      and method available in the core classes jar that comes with your JDK.

      Second a question for you:

      I don't understand your question: a char is just two bytes. You can't stick an
      entire byte array in one single char. Care to rephrase your question?

      kind regards,

      Jos

      Comment

      • andremarim
        New Member
        • Jul 2007
        • 3

        #4
        Originally posted by JosAH
        First a remark:

        Read the "article index" post in the Java Articles section (see the menu bar
        at the top of this page). At the end of that index is a link to the complete API
        documentation for all the core classes. Download that documentation for regular
        inspection and reading. You'll find detailed documentation there for every class
        and method available in the core classes jar that comes with your JDK.

        Second a question for you:

        I don't understand your question: a char is just two bytes. You can't stick an
        entire byte array in one single char. Care to rephrase your question?

        kind regards,

        Jos
        thanks guy. Actually I've already solved my problem. Cast worked fine.

        But, let me try to explain you what was my problem because by doing this I show you a another problem that I've to solve.
        in the first problem I had a file and in this file I'd to read a char codifyed in bytes. This is the why I needed to convert a bytearray into char.
        Now my problem is to convert a float into to bytearray. because I've to put some float values into a external file.

        Thanks a lot.

        andremarim

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by andremarim
          thanks guy. Actually I've already solved my problem. Cast worked fine.

          But, let me try to explain you what was my problem because by doing this I show you a another problem that I've to solve.
          in the first problem I had a file and in this file I'd to read a char codifyed in bytes. This is the why I needed to convert a bytearray into char.
          Now my problem is to convert a float into to bytearray. because I've to put some float values into a external file.

          Thanks a lot.

          andremarim
          Just so you know though, take a look at the Wrapper classes (Integer, Float, Double, etc.)... I believe they have methods to convert into byte arrays...

          Ok, just went to check, yup, I was correct, here's the float class:



          And the method you want is the byteValue() method (the first one that you see).

          Hope this helped,

          -blazed

          P.S. And here's the byte wrapper class, which I didn't know about: http://java.sun.com/j2se/1.4.2/docs/...lang/Byte.html

          Which will transform a byte into specific types, though I don't know why they haven't already made this, if you use anything that's more then one byte, which is almost anything you'd need to shift and whatnot...

          -blazed

          Comment

          • andremarim
            New Member
            • Jul 2007
            • 3

            #6
            THANKS very much.....I solved my problem, actually we solved my problem........ ...thanks again........

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by andremarim
              THANKS very much.....I solved my problem, actually we solved my problem........ ...thanks again........
              Only now I understand your problem. The answer screams for a ByteBuffer.
              Check it out in the nio package.

              kind regards,

              Jos

              Comment

              Working...