Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Sending and receiving Integers on Sockets


    angio

    Recommended Posts

    I was curious if anyone has some other ideas to send/receive an integer without using a wrapper-class for OutputStream/InputStream

        public int readInt() throws IOException {
            int number = 0;
            byte[] bytes = new byte[4];
            for (int i = 0; i < 4; i++) {
                bytes[i] = (byte) inputStream.read();
            }
            for (int i = 3; i >= 0; i--) {
                byte tmp = bytes[i];
                for (int j = 7; j >= 0; j--) {
                    if (((tmp >> j) & 1) == 1) number |= 1 << (j + (i * 8));
                }
            }
            return number;
        }
    
        public void send(int integer) throws IOException {
            synchronized (lock) {
                byte[] split = new byte[4];
                for (int i = 0; i < split.length; i++) {
                    split[i] = (byte) ((integer >> (i * 8)) & 255);
                }
                outputStream.write(OpCode.INTEGER.ordinal());
                outputStream.write(split);
                outputStream.flush();
            }
        }

     

    Link to comment
    Share on other sites

    17 hours ago, angio said:

    I was curious if anyone has some other ideas to send/receive an integer without using a wrapper-class for OutputStream/InputStream

    
        public int readInt() throws IOException {
            int number = 0;
            byte[] bytes = new byte[4];
            for (int i = 0; i < 4; i++) {
                bytes[i] = (byte) inputStream.read();
            }
            for (int i = 3; i >= 0; i--) {
                byte tmp = bytes[i];
                for (int j = 7; j >= 0; j--) {
                    if (((tmp >> j) & 1) == 1) number |= 1 << (j + (i * 8));
                }
            }
            return number;
        }
    
        public void send(int integer) throws IOException {
            synchronized (lock) {
                byte[] split = new byte[4];
                for (int i = 0; i < split.length; i++) {
                    split[i] = (byte) ((integer >> (i * 8)) & 255);
                }
                outputStream.write(OpCode.INTEGER.ordinal());
                outputStream.write(split);
                outputStream.flush();
            }
        }

     

    Do you want to work solely with bitwise operations?
    Because otherwise you can use the ByteBuffer class. Especially for the readInt

    ByteBuffer.wrap(byteArr).getInt()

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.