Converting arrays into byte arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matt melton

    Converting arrays into byte arrays

    Hi there,
    I am trying to write a method that accepts an array of any primitive
    type and will return the same array without copying memory as an array
    of bytes.

    ie. I'd like to be able to do something like:

    char[] chars = "Hello!";
    byte[] bytes = (byte[]) chars;

    which obviously won't work.

    Is there any way of casting arrays?

    Thanks for any help you can give me.

    Matt
  • Neomorph

    #2
    Re: Converting arrays into byte arrays

    On Wed, 06 Aug 2003 11:36:59 +0100, matt melton <hairy_marmite@ yahoo.co.uk>
    two-finger typed:
    [color=blue]
    >Hi there,
    > I am trying to write a method that accepts an array of any primitive
    >type and will return the same array without copying memory as an array
    >of bytes.[/color]

    You will have to overload the method (i.e. write a different method for
    each type of array).
    [color=blue]
    >
    >ie. I'd like to be able to do something like:
    >
    >char[] chars = "Hello!";[/color]

    You are assigning a String object to an array ?
    Use:
    String hello = "Hello!" ;
    char [] chars = new char [hello.length()];
    hello.getChars( 0,hello.length( ),chars,0);

    Or:
    char [] chars = getChars("Hello !");

    with an extra (local) method:
    public char [] getChars(String s) {
    char [] chars = new char [s.length()];
    hello.getChars( 0,s.length(),ch ars,0);
    }
    [color=blue]
    >byte[] bytes = (byte[]) chars;[/color]

    bytes and chars are two different things.
    chars are 16 bit (Unicode), bytes are 8 bit.

    The resulting bytes out of a string would have to represent an encoding of
    the string to support Unicode characters above the codevalue 255.

    Have a look at the String API for more information.

    Converting doubles and floats into bytes may pose even more of a challenge,
    if you want the storage to be according to IEEE standards.
    Have you looked at Serialization ?[color=blue]
    >
    >which obviously won't work.
    >
    >Is there any way of casting arrays?
    >
    >Thanks for any help you can give me.
    >
    >Matt[/color]


    Cheers.

    Comment

    • Ante Bagaric

      #3
      Re: Converting arrays into byte arrays


      "matt melton" <hairy_marmite@ yahoo.co.uk> wrote in message news:3F30DA4B.5 2E745BB@yahoo.c o.uk...[color=blue]
      > Hi there,
      > I am trying to write a method that accepts an array of any primitive
      > type and will return the same array without copying memory as an array
      > of bytes.
      >
      > ie. I'd like to be able to do something like:
      >
      > char[] chars = "Hello!";
      > byte[] bytes = (byte[]) chars;
      >
      > which obviously won't work.
      >
      > Is there any way of casting arrays?[/color]

      Actually, you don't need to 'cast' arrays of primitives to arrays of bytes.

      What you need is something else (I don't know what, exactly), but you
      think, for some reason, that your goal can be acomplished by doing
      something like that (treating a block of memory like an array of bytes,
      regardless of what's inside that block. That's precisely what you shouldn't
      even think about, in Java. Go back to C for stunts like that. (-: )

      So, it would be better if you went one step back and said what you really
      wanted to do, instead of asking detailed help on something that could
      easily be a bad idea in the first place.



      Comment

      • matt melton

        #4
        Re: Converting arrays into byte arrays

        Hi

        I might need to elaborate on what I'm really trying to do,

        I am writing an API to allow simple message passing of arrays between
        VM's on different machines. I'd like to use UDP and Datagrams for the
        trasfer ( I have my reasons, they may not be valid though ).
        I want it to be as light weight and quick as possible:
        So just a simple byte tag to identify the type of the array, rather than
        serializing the array.
        As little memory copying between the array itself and a DatagramPacket
        as possible, the minimum being zero if it is feasible ( which I am
        beginning to think isn't).

        What I really want is to be able to view a primitive array,
        as an array of any other primitive type.

        ie

        byte[16] bytearray int[4] intarray double[2] doublearry
        0_____8 0_______8 0_______8
        0 |__A__| 0 | A | | A |
        1 |__B__| |___B___| | B |
        2 |__C__| 1 | C | | C |
        3 |__D__| |___D___| |___D___|
        4 |__E__| 2 | E | | E |
        5 |__F__| |___F___| | F |
        6 |__G__| 3 | G | | G |
        7 |__H__| |___H___| |___H___|
        8 |__I__| 4 | I | | I |
        9 |__J__| |___J___| | J |
        10|__K__| 5 | K | | K |
        11|__L__| |___L___| |___L___|
        12|__M__| 6 | M | | M |
        13|__N__| |___N___| | N |
        14|__O__| 7 | O | | O |
        15|__P__| |___P___| |___P___|

        all pointing at the same array in memory.
        I can use a java.nio.ByteBu ffer to put a double into a ByteArray, and
        view it as both 8 bytes and 2 integers etc.

        but I cannot create an array of one type and then place it straight into
        a DatagramPacket constructor as the buffer to use for the data like
        this:

        sendDouble( double[] data , offset , length , address , port){

        //copy double[] data into new byte[] newData

        dsocket.send( new DatagramPacket( newData , offset , length,
        address , port));
        }

        without doing a memory copy of some kind to get the data out of the
        double array into a byte array even though they contain the same
        bytes...
        hope that makes sense.

        Is java designed to prevent this kind of memory tampering, even if the
        view would be read only?

        I'll just have to settle for a single memory copy( + the one from the
        DatagramPacket to the network interface w3hich I can't do anything
        about) I suppose.

        Thanks


        Neomorph wrote:[color=blue]
        >
        > On Wed, 06 Aug 2003 11:36:59 +0100, matt melton <hairy_marmite@ yahoo.co.uk>
        > two-finger typed:
        >[color=green]
        > >Hi there,
        > > I am trying to write a method that accepts an array of any primitive
        > >type and will return the same array without copying memory as an array
        > >of bytes.[/color]
        >
        > You will have to overload the method (i.e. write a different method for
        > each type of array).
        >[color=green]
        > >
        > >ie. I'd like to be able to do something like:
        > >
        > >char[] chars = "Hello!";[/color]
        >
        > You are assigning a String object to an array ?
        > Use:
        > String hello = "Hello!" ;
        > char [] chars = new char [hello.length()];
        > hello.getChars( 0,hello.length( ),chars,0);
        >
        > Or:
        > char [] chars = getChars("Hello !");
        >
        > with an extra (local) method:
        > public char [] getChars(String s) {
        > char [] chars = new char [s.length()];
        > hello.getChars( 0,s.length(),ch ars,0);
        > }
        >[color=green]
        > >byte[] bytes = (byte[]) chars;[/color]
        >
        > bytes and chars are two different things.
        > chars are 16 bit (Unicode), bytes are 8 bit.
        >
        > The resulting bytes out of a string would have to represent an encoding of
        > the string to support Unicode characters above the codevalue 255.
        >
        > Have a look at the String API for more information.
        >
        > Converting doubles and floats into bytes may pose even more of a challenge,
        > if you want the storage to be according to IEEE standards.
        > Have you looked at Serialization ?[color=green]
        > >
        > >which obviously won't work.
        > >
        > >Is there any way of casting arrays?
        > >
        > >Thanks for any help you can give me.
        > >
        > >Matt[/color]
        >
        > Cheers.[/color]

        Comment

        • matt melton

          #5
          Re: Converting arrays into byte arrays

          Hi there,
          it's just a question of effeciency, I don't like to copy memory when I
          don't have to, but If I must I must. If it makes the program more
          stable secure etc..
          then I'll have to like it.
          It just seemed a bit of waste to have the same information stored in
          two places with identical data just so it can be accessed in a
          particular way. I guess it's due to the protability reasons etc...

          Cheers

          [color=blue]
          > Actually, you don't need to 'cast' arrays of primitives to arrays of bytes.
          >
          > What you need is something else (I don't know what, exactly), but you
          > think, for some reason, that your goal can be acomplished by doing
          > something like that (treating a block of memory like an array of bytes,
          > regardless of what's inside that block. That's precisely what you shouldn't
          > even think about, in Java. Go back to C for stunts like that. (-: )
          >
          > So, it would be better if you went one step back and said what you really
          > wanted to do, instead of asking detailed help on something that could
          > easily be a bad idea in the first place.[/color]

          Comment

          • Neomorph

            #6
            Re: Converting arrays into byte arrays

            On 7 Aug 2003 01:25:18 -0700, hairy_marmite@y ahoo.co.uk (matt melton)
            two-finger typed:
            [color=blue]
            >Hi there,
            >it's just a question of effeciency, I don't like to copy memory when I
            >don't have to, but If I must I must. If it makes the program more
            >stable secure etc..
            >then I'll have to like it.
            >It just seemed a bit of waste to have the same information stored in
            >two places with identical data just so it can be accessed in a
            >particular way. I guess it's due to the protability reasons etc...[/color]

            Not just for portability, but also for security and bug repellant.

            Basically data in memory is always typed in java, to stop the possibility
            of bugs with respect to wrong interpretation of memory content (as code for
            example - which is an occurence that makes hacking Microsoft Operating
            Systems, browsers and even their game console so relatively easy).

            An array, for example, is more than just a series of memory locations fora
            specific datatype, it also holds the unchangable length of the array.

            You could probably use JNI if you are so bent on reusing memory, but that
            would make your application non-portable and more open to bugs...
            [color=blue]
            >
            >Cheers
            >
            >[color=green]
            >> Actually, you don't need to 'cast' arrays of primitives to arrays of bytes.
            >>
            >> What you need is something else (I don't know what, exactly), but you
            >> think, for some reason, that your goal can be acomplished by doing
            >> something like that (treating a block of memory like an array of bytes,
            >> regardless of what's inside that block. That's precisely what you shouldn't
            >> even think about, in Java. Go back to C for stunts like that. (-: )
            >>
            >> So, it would be better if you went one step back and said what you really
            >> wanted to do, instead of asking detailed help on something that could
            >> easily be a bad idea in the first place.[/color][/color]

            Cheers.

            Comment

            Working...