Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java equivalent of Python’s struct.pack?

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java equivalent of Python’s struct.pack?

    Is there any function equivalent to Python's struct.pack in Java that allows me to pack and unpack values like this?

    pump_on = struct.pack("IIHHI", 0, 0, 21, 96, 512)
    event = open(("/dev/input/event7"), "r+",0)
    event.write(valve_on)


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java equivalent of Python’s struct.pack?

    Not as far as I know, but you can serialize Java objects that implement the Serializable interface and then use the Object writer to write the object data to a binary file.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Java equivalent of Python’s struct.pack?

    I take it you're actually creating a struct of a files contents there, in theory you could do this with a byte array.

    This might not be what you want and there might be a more simple solution to this but at least it works

        public static void main(String[] args) throws Throwable {
            // Create a "struct" or a buffer that we wish to output
            final ByteBuffer outputByteBuffer = ByteBuffer.allocate(30);
            outputByteBuffer.putChar('I').putChar('I').putChar('H').putChar('H').putChar('I').putInt(0).putInt(0).putInt(21).putInt(96).putInt(512);
            outputByteBuffer.flip();
     
            // Write the buffer to a file
            final File file = new File("/dev/input/event7");
            final FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.getChannel().write(outputByteBuffer);
            fileOutputStream.close();
     
            // Open the file and read in the buffer
            final ByteBuffer inputByteBuffer = ByteBuffer.allocate(30);
            final FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.getChannel().read(inputByteBuffer);
            fileInputStream.close();
     
            // Each character takes 2 bytes so therefore we have the characters at index 0, 2, 4, 6 and 8
            System.out.println(Character.toString(inputByteBuffer.getChar(0)) + Character.toString(inputByteBuffer.getChar(2)) + Character.toString(inputByteBuffer.getChar(4)) + Character.toString(inputByteBuffer.getChar(6)) + Character.toString(inputByteBuffer.getChar(8)));
     
            // First int will be on index 10 because of the 2 bytes taken by the character before it
            System.out.println(inputByteBuffer.getInt(10));
     
            // Second int will be a at 14 because an int takes 4 bytes
            System.out.println(inputByteBuffer.getInt(14));
     
            // Third int will be a at 14 because an int takes 4 bytes
            System.out.println(inputByteBuffer.getInt(18));
     
            // Forth int will be a at 14 because an int takes 4 bytes
            System.out.println(inputByteBuffer.getInt(22));
     
            // Fifth int will be a at 14 because an int takes 4 bytes
            System.out.println(inputByteBuffer.getInt(26));
     
        }

    // Json

Tags for this Thread