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
  • Try asking for help in the chatbox
  • Sending and receiving Integers on Sockets


    angio
     Share

    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();
            }
        }

     

    Edited by angio
    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

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • 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.