Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By jim829

Thread: From C# to Java, please help

  1. #1
    Slavica is offline Member
    Join Date
    Dec 2014
    Posts
    5
    Rep Power
    0

    Default From C# to Java, please help

    I need to convert c# function to java method. The important thing is String strKey. Parameters for testing are strMask= 4634958 and strSN=1394901184 and the result must be strKey = 2156325482!!! The result that I am getting with my Java code is 2138641814.

    This is C# code:

    public static bool Key (String strMask, String strSN, ref String strKey)
    {
    System.UInt16 wLo, wHi;
    System.UInt32 dwSNx, dwKey;
    System.UInt32 dwSN, dwMask;

    if (strMask=="") strMask="0";
    if (strSN=="") strSN="0";

    try
    {
    dwSN = System.Convert.ToUInt32(strSN,10);
    dwMask = System.Convert.ToUInt32(strMask,10);

    wLo = (System.UInt16) dwSN;

    wHi = (System.UInt16) (((System.UInt32)(dwSN >> 16)) & 0xFFFF );

    dwSNx = (System.UInt32) ((System.UInt32)wHi | (System.UInt32)(wLo << 16));

    dwKey = dwMask;

    dwKey ^= dwSNx;
    }
    catch
    {
    strKey="";
    return false;
    }
    strKey = dwKey.ToString();
    return true;

    }

    This is my Java code:

    public static boolean kljuc (String strMask, String strSN,String strKey ){
    short wLo, wHi;
    int dwSNx, dwKey, dwSN, dwMask;

    if (strMask=="") strMask="0";
    if (strSN=="") strSN="0";
    try{
    dwSN = Integer.parseInt(strSN, 10);
    dwMask = Integer.parseInt(strMask, 10);

    wLo = (short)dwSN;

    wHi = (short)((dwSN >> 16) & 0xFFFF );//casting u int redudantan

    dwSNx = (int)((int)wHi | (int)(wLo << 16));

    dwKey = dwMask^dwSNx;

    //dwKey ^= dwSNx;


    }

    catch(Exception e){
    strKey="";
    return false;
    }

    strKey= Integer.toString(dwKey);
    JOptionPane.showMessageDialog(null, strKey);
    return true;
    }




    Please help!!!!!!!
    Last edited by Slavica; 12-02-2014 at 11:14 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: From C# to Java, please help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: From C# to Java, please help

    I don't really want to debug someone else's code; I'd assume that if you're a programmer you can do something yourself.

    I'll just make a quick guess - possibly by NOT working with shorts in the Java code, you may prevent some problems. I make that guess because the C# code works with unsigned types which Java does not have.

    Java Code:
    short wLo, wHi; <-- wrong
    Java Code:
    int wLo, wHi; <-- possibly better
    
    
    wLo = dwSN;
    
    wHi = ((dwSN >> 16) & 0xFFFF ); // don't cast to short
    Not tested, no guarantee that anything will be better.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Slavica is offline Member
    Join Date
    Dec 2014
    Posts
    5
    Rep Power
    0

    Default Re: From C# to Java, please help

    Thank U!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: From C# to Java, please help

    If you are working with unsigned types then you should shift right using >>>. This will always shifts 0 into the leftmost position while >> depends on the sign.

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Slavica is offline Member
    Join Date
    Dec 2014
    Posts
    5
    Rep Power
    0

    Default Re: From C# to Java, please help

    Quote Originally Posted by jim829 View Post
    If you are working with unsigned types then you should shift right using >>>. This will always shifts 0 into the leftmost position while >> depends on the sign.

    Regards,
    Jim
    Thank U Jim...

    I have fixed one line to give a proper result like in C# ;

    Java:
    wLo =(int)((char)dwSN); //short does not work because of Overflow

    this works fine:

    wHi = (short)(((int)(dwSN >> 16)) & 0xFFFF);

    and now problem is left shifting here:
    dwSNx = (int)((int)wHi | (int)(wLo << 16));

    because wLo is initialized like integer with 32 bits, and in C# wLo is Uint16 with 16 bits :( If anyone knows how to solve this problem I would be very grateful. Thank U for your help!!!

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: From C# to Java, please help

    What values are you passing to the method?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Slavica is offline Member
    Join Date
    Dec 2014
    Posts
    5
    Rep Power
    0

    Default Re: From C# to Java, please help

    Quote Originally Posted by jim829 View Post
    What values are you passing to the method?

    Regards,
    Jim
    strSN=1394901184
    strMask=.4634958

    Result is strKey=2156325482

    Thank U for your time Jim :)

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: From C# to Java, please help

    Your answer you got is correct (sort of). The answer as you stated is
    2156325482. The result that you got is 2138641814. I am not certain how this came out positive but
    if you negate the latter you get -2138641814 (which is actually correct). If you mask off the sign you get 8841834.
    If you do an unsigned add (on paper) to Integer.MAX_VALUE and add 1 to correct for two's complement you get the correct
    unsigned answer.

    But Java does not support unsigned numbers so I recommend you make dwKey a long and mask off the last
    32 bits when you are done. And change >> to >>>. It didn't matter in this case but it could bite you in the future.

    Edit: I forgot to tell you that you do a lot of unnecessary casting in your code. Just work with integers until the end (as was previously recommended).

    Regards,
    Jim
    Last edited by jim829; 12-02-2014 at 04:05 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Slavica is offline Member
    Join Date
    Dec 2014
    Posts
    5
    Rep Power
    0

    Default Re: From C# to Java, please help

    Quote Originally Posted by jim829 View Post
    Your answer you got is correct (sort of). The answer as you stated is
    2156325482. The result that you got is 2138641814. I am not certain how this came out positive but
    if you negate the latter you get -2138641814 (which is actually correct). If you mask off the sign you get 8841834.
    If you do an unsigned add (on paper) to Integer.MAX_VALUE and add 1 to correct for two's complement you get the correct
    unsigned answer.

    But Java does not support unsigned numbers so I recommend you make dwKey a long and mask off the last
    32 bits when you are done. And change >> to >>>. It didn't matter in this case but it could bite you in the future.

    Edit: I forgot to tell you that you do a lot of unnecessary casting in your code. Just work with integers until the end (as was previously recommended).

    Regards,
    Jim
    Thank U so much Jim :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •