Transcribe Java Code to C# please help?

Hi Ladoes and Gents,

I am very new to Java and had a little code I would love some help with.  I basically need to get the associative C# version of this Java code.  Please if you could comment the heck out of the code to describe what it does on the c# versioni as well it would be greatly appreciated.

FYI I have tried to install the java to C# conversion assistant on a few different machines with different versions of visual studio with no luck.  I get an error message so a automated conversion is out.

Let me please relay my gracious thank you in advance.  I hope everyone is well.

Best - Dynotoe
public abstract class Data { 
    public long time; 
     
    public Data() { 
    } 
 
    public Data(long time) { 
        this.time = time; 
    } 
 
    public abstract void toBytes(byte[] buff, int off); 
     
    public abstract int getBytesCount(); 
 
    protected static final int putLong(final byte[] b, final int off, final long val) { 
        b[off + 7] = (byte) (val >>> 0); 
        b[off + 6] = (byte) (val >>> 8); 
        b[off + 5] = (byte) (val >>> 16); 
        b[off + 4] = (byte) (val >>> 24); 
        b[off + 3] = (byte) (val >>> 32); 
        b[off + 2] = (byte) (val >>> 40); 
        b[off + 1] = (byte) (val >>> 48); 
        b[off + 0] = (byte) (val >>> 56); 
        return off + 8; 
    } 
 
    protected static final int putDouble(final byte[] b, final int off, final double val) { 
        final long j = Double.doubleToLongBits(val); 
        b[off + 7] = (byte) (j >>> 0); 
        b[off + 6] = (byte) (j >>> 8); 
        b[off + 5] = (byte) (j >>> 16); 
        b[off + 4] = (byte) (j >>> 24); 
        b[off + 3] = (byte) (j >>> 32); 
        b[off + 2] = (byte) (j >>> 40); 
        b[off + 1] = (byte) (j >>> 48); 
        b[off + 0] = (byte) (j >>> 56); 
        return off + 8; 
    } 
 
    protected static final long getLong(final byte[] b, final int off) { 
        return ((b[off + 7] & 0xFFL) << 0) + 
                ((b[off + 6] & 0xFFL) << 8) + 
                ((b[off + 5] & 0xFFL) << 16) + 
                ((b[off + 4] & 0xFFL) << 24) + 
                ((b[off + 3] & 0xFFL) << 32) + 
                ((b[off + 2] & 0xFFL) << 40) + 
                ((b[off + 1] & 0xFFL) << 48) + 
                (((long) b[off + 0]) << 56); 
    } 
 
    protected static final double getDouble(final byte[] b, final int off) { 
        final long j = ((b[off + 7] & 0xFFL) << 0) + 
                ((b[off + 6] & 0xFFL) << 8) + 
                ((b[off + 5] & 0xFFL) << 16) + 
                ((b[off + 4] & 0xFFL) << 24) + 
                ((b[off + 3] & 0xFFL) << 32) + 
                ((b[off + 2] & 0xFFL) << 40) + 
                ((b[off + 1] & 0xFFL) << 48) + 
                (((long) b[off + 0]) << 56); 
        return Double.longBitsToDouble(j); 
    } 
 
    @Override 
    public int hashCode() { 
        final int prime = 31; 
        int result = 1; 
        result = prime * result + (int) (time ^ (time >>> 32)); 
        return result; 
    } 
 
    @Override 
    public boolean equals(Object obj) { 
        if (this == obj) 
            return true; 
        if (obj == null) 
            return false; 
        if (getClass() != obj.getClass()) 
            return false; 
        final Data other = (Data) obj; 
        if (time != other.time) 
            return false; 
        return true; 
    } 
}

Open in new window

DynotoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

spuleCommented:
The conversion is not too difficult as C# is very similar to Java. One of the important things was to correct the property and method names so that they match the conventions.

//Import some necessary libraries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication { //something like a package in Java (supply the name of your application)
    abstract class Data {
        public long Time;

        public Data(long time) {
            this.Time = time;
        }

        public abstract void ToBytes(byte[] buff, int off);

        public abstract int GetBytesCount(); 

        protected static int PutLong(byte[] b, int off, long val) { //methods are "final" by default
            long j = BitConverter.DoubleToInt64Bits(val);
            b[off + 7] = (byte)(val >> 0); //the unsigned shift is not needed because byte is an unsigned type
            b[off + 6] = (byte)(val >> 8); 
            b[off + 5] = (byte)(val >> 16); 
            b[off + 4] = (byte)(val >> 24); 
            b[off + 3] = (byte)(val >> 32); 
            b[off + 2] = (byte)(val >> 40); 
            b[off + 1] = (byte)(val >> 48); 
            b[off + 0] = (byte)(val >> 56); 
            return off + 8; 
        }

        protected static int PutDouble(byte[] b, int off, double val) {
            long j = BitConverter.DoubleToInt64Bits(val); 
            b[off + 7] = (byte) (j >> 0); 
            b[off + 6] = (byte) (j >> 8); 
            b[off + 5] = (byte) (j >> 16); 
            b[off + 4] = (byte) (j >> 24); 
            b[off + 3] = (byte) (j >> 32); 
            b[off + 2] = (byte) (j >> 40); 
            b[off + 1] = (byte) (j >> 48); 
            b[off + 0] = (byte) (j >> 56); 
            return off + 8; 
        } 

        protected static long GetLong(byte[] b, int off) { 
            return ((b[off + 7] & 0xFFL) << 0) + 
                    ((b[off + 6] & 0xFFL) << 8) + 
                    ((b[off + 5] & 0xFFL) << 16) + 
                    ((b[off + 4] & 0xFFL) << 24) + 
                    ((b[off + 3] & 0xFFL) << 32) + 
                    ((b[off + 2] & 0xFFL) << 40) + 
                    ((b[off + 1] & 0xFFL) << 48) + 
                    (((long) b[off + 0]) << 56); 
        } 
     
        protected static double GetDouble(byte[] b, int off) { 
            long j = ((b[off + 7] & 0xFFL) << 0) + 
                    ((b[off + 6] & 0xFFL) << 8) + 
                    ((b[off + 5] & 0xFFL) << 16) + 
                    ((b[off + 4] & 0xFFL) << 24) + 
                    ((b[off + 3] & 0xFFL) << 32) + 
                    ((b[off + 2] & 0xFFL) << 40) + 
                    ((b[off + 1] & 0xFFL) << 48) + 
                    (((long) b[off + 0]) << 56); 
            return BitConverter.Int64BitsToDouble(j); 
        } 

        public int GetHashCode() { 
            int prime = 31; 
            int result = 1; 
            result = prime * result + (int) (Time ^ (Time >> 32));
            return result; 
        } 
     
        public bool Equals(Object obj) { 
            if (this == obj) 
                return true; 
            if (obj == null) 
                return false; 
            if (GetType() != obj.GetType()) 
                return false; 
            Data other = (Data) obj; 
            if (Time != other.Time) 
                return false; 
            return true; 
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DynotoeAuthor Commented:
Wow that's great..  I really appreciate the help,so thank you very much.  In case your interested I will be posting a couple of others if you want the points, seems right up your alley.  Plus you'll know ow they go together.

Have a great Sunday.

-D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.