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: Converting a piece of C# code to Java (unsigned longs, etc)

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting a piece of C# code to Java (unsigned longs, etc)

    Hello!

    I'm trying to convert this piece of C# code to java, but I'm stumbling to problems because java doesn't seem to have unsigned long.

    public static string keyHexToKey(byte[] keyHex)
    {
            string arg = "";
            string text = "0123456789ABCDEFGHJKLMNPRSTVWXYZ";
            for (int i = 0; i < 3; i++)
            {
                    ulong num = 0uL;
                    for (int j = 0; j < 5; j++)
                    {
                            num <<= 8;
                            num |= (ulong)keyHex[i * 5 + j];
                    }
                    for (int j = 0; j < 8; j++)
                    {
                            ulong num2 = num >> j * 5 & 31uL;
                            char c = text[(int)num2];
                            arg += c;
                    }
            }
            return arg;
    }

    What I came up by myself, but doesn't seem to work (return wrong values).
    private static String keyHexToKey(byte[] keyHex)
    	{
    	        String arg = "";
    	        String text = "0123456789ABCDEFGHJKLMNPRSTVWXYZ";
    	        char[] text2 = text.toCharArray();
     
    	        for (int i = 0; i < 3; i++)
    	        {
    	                //long num = 0uL;
    	        		long num = (long)0xff;
    	                for (int j = 0; j < 5; j++)
    	                {
    	                        num <<= 8;
    	                        num |= (long)keyHex[i * 5 + j];
    	                }
    	                for (int j = 0; j < 8; j++)
    	                {
    	                        long num2 = num >> j * 5 & (long)31;
    	                        //char c = text[(int)num2];
    	                        char c = text2[(int)num2];
    	                        arg += c;
    	                }
    	        }
    	        return arg;
    	}


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Converting a piece of C# code to Java (unsigned longs, etc)

    In Java a Long is a signed 64 bit value, and Java doesn't offer an unsigned version of this as they believe unsigned integers to be wrong, the only unsigned data type is a char.

    Also you do not need to cast literals to long, simple append "L" such as 32L would be the long value for 32.

    Your options are either, manually handle the sign, or use the BigInteger class

    BigInteger (Java Platform SE 7 )

    Chris

  3. #3
    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: Converting a piece of C# code to Java (unsigned longs, etc)

    The best way to translate the code is to figure out fundamentally what the intent of the code is.

    For example:

    ulong num = 0uL;
    for (int j = 0; j < 5; j++)
    {
        num <<= 8;
        num |= (ulong)keyHex[i * 5 + j];
    }

    This code takes each byte value from keyHex and shifts it into the LSB of num. There are 5 bytes shifted in.

    for (int j = 0; j < 8; j++)
    {
        ulong num2 = num >> j * 5 & 31uL;
        char c = text[(int)num2];
        arg += c;
    }

    This selects 5 bit chunks from num, working from the LSB to the MSB. There are 8 5-bit chunks. It then maps that value to the appropriate character and appends it to the output string.

    So the sole purpose of this code is to take 5-bit compressed values and expand them out, then map the results appropriately.

    The question then becomes how can you replicate this behavior in Java.

    As it turns out, pretty much the same exact code will work in Java (with a few minor differences to account for library and language semantics). Why? Because you never use the msb of the unsigned long, so it doesn't matter that it's unsigned.

Similar Threads

  1. How do I represent any character in a piece of code?
    By 93tomh in forum Java Theory & Questions
    Replies: 3
    Last Post: July 15th, 2012, 05:28 AM
  2. Converting Java Code to work with Android
    By Farmer in forum Android Development
    Replies: 2
    Last Post: May 12th, 2012, 09:16 AM
  3. ArrayList of longs
    By tarkal in forum Collections and Generics
    Replies: 2
    Last Post: September 26th, 2011, 03:51 PM
  4. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  5. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM