I have 2 files writen in java, if someone can please covert them in C# and give it.

The problem is with type of variables and operators, many operators are not overloaded like in java, idk what to say...

This one will not work in C# as integer

result |= _decrypt[_off++] << 0x18 & 0xff000000;

i change it to long, but i get "Warnings" in smallers

Also, here get error with << operator

result |= (_decrypt[_off++] & 0xffl) << 8l;

----------------------------------------------------------

/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.util.network;

/**
 * This class ...
 *
 * @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:12 $
 */
public abstract class BaseRecievePacket
{
    private byte[] _decrypt;
    private int _off;

    public BaseRecievePacket(byte[] decrypt)
    {
        _decrypt = decrypt;
        _off = 1;       // skip packet type id
    }

    public int readD()
    {
        int result = _decrypt[_off++] & 0xff;
        result |= _decrypt[_off++] << 8 & 0xff00;
        result |= _decrypt[_off++] << 0x10 & 0xff0000;
        result |= _decrypt[_off++] << 0x18 & 0xff000000;
        return result;
    }

    public int readC()
    {
        int result = _decrypt[_off++] &0xff;
        return result;
    }

    public int readH()
    {
        int result = _decrypt[_off++] &0xff;
        result |= _decrypt[_off++] << 8 &0xff00;
        return result;
    }

    public double readF()
    {
        long result = _decrypt[_off++] & 0xff;
        result |= (_decrypt[_off++] & 0xffl) << 8l;
        result |= (_decrypt[_off++] & 0xffl) << 16l;
        result |= (_decrypt[_off++] & 0xffl) << 24l;
        result |= (_decrypt[_off++] & 0xffl) << 32l;
        result |= (_decrypt[_off++] & 0xffl) << 40l;
        result |= (_decrypt[_off++] & 0xffl) << 48l;
        result |= (_decrypt[_off++] & 0xffl) << 56l;
        return Double.longBitsToDouble(result);
    }

    public String readS()
    {
        String result = null;
        try
        {
            result = new String(_decrypt,_off,_decrypt.length-_off, "UTF-16LE");
            result = result.substring(0, result.indexOf(0x00));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        _off += result.length()*2 + 2;
        return result;
    }

    public final byte[] readB(int length)
    {
        byte[] result = new byte[length];
        for(int i = 0; i < length; i++)
        {
            result[i]=_decrypt[_off+i];
        }
        _off += length;
        return result;
    }

    public long readQ()
    {
        long result = _decrypt[_off++] & 0xff;
        result |= (_decrypt[_off++] & 0xffl) << 8l;
        result |= (_decrypt[_off++] & 0xffl) << 16l;
        result |= (_decrypt[_off++] & 0xffl) << 24l;
        result |= (_decrypt[_off++] & 0xffl) << 32l;
        result |= (_decrypt[_off++] & 0xffl) << 40l;
        result |= (_decrypt[_off++] & 0xffl) << 48l;
        result |= (_decrypt[_off++] & 0xffl) << 56l;
        return result;
    }
}

Recommended Answers

All 7 Replies

And 2nd File...

/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.util.network;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * This class ...
 *
 * @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:11 $
 */
public abstract class BaseSendablePacket
{
    ByteArrayOutputStream _bao;

    protected BaseSendablePacket()
    {
        _bao = new ByteArrayOutputStream();
    }

    protected void writeD(int value)
    {
        _bao.write(value &0xff);
        _bao.write(value >> 8 &0xff);
        _bao.write(value >> 16 &0xff);
        _bao.write(value >> 24 &0xff);
    }

    protected void writeH(int value)
    {
        _bao.write(value &0xff);
        _bao.write(value >> 8 &0xff);
    }

    protected void writeC(int value)
    {
        _bao.write(value &0xff);
    }

    protected void writeF(double org)
    {
        long value = Double.doubleToRawLongBits(org);
        _bao.write((int)(value &0xff));
        _bao.write((int)(value >> 8 &0xff));
        _bao.write((int)(value >> 16 &0xff));
        _bao.write((int)(value >> 24 &0xff));
        _bao.write((int)(value >> 32 &0xff));
        _bao.write((int)(value >> 40 &0xff));
        _bao.write((int)(value >> 48 &0xff));
        _bao.write((int)(value >> 56 &0xff));
    }

    protected void writeS(String text)
    {
        try
        {
            if (text != null)
            {
                _bao.write(text.getBytes("UTF-16LE"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        _bao.write(0);
        _bao.write(0);
    }

    protected void writeB(byte[] array)
    {
        try
        {
            _bao.write(array);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    protected void writeQ(long value)
    {
        _bao.write((int) (value & 0xff));
        _bao.write((int) (value >> 8 & 0xff));
        _bao.write((int) (value >> 16 & 0xff));
        _bao.write((int) (value >> 24 & 0xff));
        _bao.write((int) (value >> 32 & 0xff));
        _bao.write((int) (value >> 40 & 0xff));
        _bao.write((int) (value >> 48 & 0xff));
        _bao.write((int) (value >> 56 & 0xff));
    }

    public int getLength()
    {
        return _bao.size()+2;
    }

    public byte[] getBytes()
    {
        //if (this instanceof Init)
        //  writeD(0x00); //reserve for XOR initial key

        writeD(0x00); // reserve for checksum

        int padding = _bao.size() % 8;
        if (padding != 0)
        {
            for (int i = padding; i<8;i++)
            {
                writeC(0x00);
            }
        }

        return _bao.toByteArray();
    }

    public abstract byte[] getContent() throws IOException;
}

I have done 1 file, because im newbie in C#, i wait your opinion, how it look

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Core.l2server.util.network
{
    public class BaseSendablePacket
    {       
        ByteArrayOutputStream BA;

        public BaseSendablePacket() 
        {
            BA = new ByteArrayOutputStream();
        }

        public void WriteD(int Value)
        {
            BA.Write(Value & 0xff);
            BA.Write(Value >> 8 & 0xff);
            BA.Write(Value >> 16 & 0xff);
            BA.Write(Value >> 24 & 0xff);
        }

        public void WriteH(int Value)
        {
            BA.Write(Value & 0xff);
            BA.Write(Value >> 8 & 0xff);
        }

        public void WriteC(int Value)
        {
            BA.Write(Value & 0xff);
        }

        public void WriteB(byte[] Array)
        {
            try 
            {
                BA.Write(Array);
            }
            catch (IOException e)
            {
                Console.WriteLine("WriteB: Error " + e.StackTrace);
            }
        }

        public void WriteF(double org)
        {

            long Value = BitConverter.DoubleToInt64Bits(org);

            BA.Write((int)(Value & 0xff));
            BA.Write((int)(Value >> 8 & 0xff));
            BA.Write((int)(Value >> 16 & 0xff));
            BA.Write((int)(Value >> 24 & 0xff));
            BA.Write((int)(Value >> 32 & 0xff));
            BA.Write((int)(Value >> 40 & 0xff));
            BA.Write((int)(Value >> 48 & 0xff));
            BA.Write((int)(Value >> 56 & 0xff));
        }

        public void WriteQ(long value)
        {
            BA.Write((int)(value & 0xff));
            BA.Write((int)(value >> 8 & 0xff));
            BA.Write((int)(value >> 16 & 0xff));
            BA.Write((int)(value >> 24 & 0xff));
            BA.Write((int)(value >> 32 & 0xff));
            BA.Write((int)(value >> 40 & 0xff));
            BA.Write((int)(value >> 48 & 0xff));
            BA.Write((int)(value >> 56 & 0xff));
        }

        public void WriteS(string Text)
        {
            try
            {
                if (Text != null)
                {
                    Encoding _En = Encoding.GetEncoding("UTF-16LE");
                    BA.Write(_En.GetBytes(Text.ToCharArray()));
                    Console.WriteLine(Encoding.GetEncoding("UTF-16LE"));
                }
            }
            catch (Exception e)
            {
                Console.Write("WriteS ERROR: " + e.StackTrace);
            }

            BA.Write(0);
            BA.Write(0);
        }

        public int GetLength()
        {
            return BA.Size() + 2;
        }

        public byte[] GetBytes()
        {
            WriteD(0x00); // reserve for checksum

            int Padding = BA.Size() % 8;

            if (Padding != 0)
            {
                for (int i = Padding; i < 8; i++)
                {
                    WriteC(0x00);
                }
            }
            return BA.ToByteArray();
        }


    }
}

Um I thought writing code for someone isn't allowd here....

Dont think so, anyway ... i done it ;p

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Core.l2server.util.network
{
    public class BaseRecievePacket
    {
        private Logger _l;
        private byte[] _Packet;
        private int _Offset;

        public BaseRecievePacket() { }
        public BaseRecievePacket(byte[] Packet)
        {
            _Packet = Packet;
            _Offset = 1; // Skip packet type ID
        }

        public byte[] ReadB(int length)
        {
            byte[] result = new byte[length];
            try
            {
                for (int i = 0; i < length; i++)
                {
                    result[i] = _Packet[_Offset + i];
                }
                _Offset += length;
            }
            catch
            { _l.Write(Logger.Type.PACKET, "An error happen in <ReadB()>"); }

            return result;
        }

        public int ReadC()
        {
            int result = 0;
            try {
                result = _Packet[_Offset++] & 0xff;
            }
            catch
            { _l.Write(Logger.Type.PACKET, "An error happen in <ReadC()>"); }

            return result;
        }

        public int ReadD()
        {
            int result = 0;
            try
            {
                result = _Packet[_Offset++] & 0xff;
                result |= _Packet[_Offset++] << 8 & 0xff00;
                result |= _Packet[_Offset++] << 0x10 & 0xff0000;
                result |= _Packet[_Offset++] << 0x18 & Convert.ToInt32(0xff000000);
            }
            catch
            { _l.Write(Logger.Type.PACKET, "An error happen in <ReadD()>"); }

            return result;
        }

        public int ReadH()
        {
            int result = 0;
            try
            {
                result = _Packet[_Offset++] & 0xff;
                result |= _Packet[_Offset++] << 8 & 0xff00;
            }
            catch
            { _l.Write(Logger.Type.PACKET, "An error happen in <ReadH()>"); }

            return result;
        }

        public double ReadF()
        {
            long result = 0;
            try
            {
                result = _Packet[_Offset++] & 0xff;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 8;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 16;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 24;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 32;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 40;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 48;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 56;
            }
            catch
            {   _l.Write(Logger.Type.PACKET, "An error happen in <ReadF()>");   }

            return Convert.ToDouble(result);
        }

        public string ReadS()
        {
            string result = "";
            try
            {
                result = Encoding.Unicode.GetString(_Packet, _Offset, _Packet.Length - _Offset);
                int id = result.IndexOf((char)0x00);

                if (!(id == -1))
                { result = result.Substring(0, id); }
                _Offset += (result.Length * 2) + 2;
            }
            catch
            {
                { _l.Write(Logger.Type.PACKET, "An error happen in <ReadS()>"); }
            }
            return result;
        }

        public long ReadQ()
        {
            long result = 0;
            try
            {
                result = _Packet[_Offset++] & 0xff;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 8;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 16;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 24;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 32;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 40;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 48;
                result |= (uint)(_Packet[_Offset++] & 0xff) << 56;
            }
            catch
            { _l.Write(Logger.Type.PACKET, "An error happen in <ReadQ()>"); }

            return result;
        }
    }

}

Um I thought writing code for someone isn't allowd here....

Writing the code for someone is not the problem.
Doing someone's homework with no effort from them is a problem.

... im newbie in C#, ...

I'm glad you're trying to do this.
It would be much better for you to figure out this one by yourself as it will teach you more than you could learn from someone else writing it.

The upper code is just conversion from java, http://svn.l2jserver.com/trunk/L2J_Server/java/com/l2jserver/util/network/

Im trying to convert everything in C# in my free time, you will say me " why you do that, C# is same sh1t with java.."

I do it just to learn from it, i want to learn work with bytes/sockets/threads/fileIO...
this project i think is good deal for me, if i done it in about 4-5 Month i will be glad :D

looks like a private server of mmo to me am i right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.