Link to home
Start Free TrialLog in
Avatar of theartha
thearthaFlag for United States of America

asked on

How to convert java Long to c#

Hi There,

I am new to C#. But experience in Java.

How to convert java Long to c#

Java:

private Long recNumber;
recNumber = new Long(this.getCreateDate());

C#:

 private long recNumber;
this.recNumber = Convert.ToInt64(this.getCreateDate());

I got OverflowException with error Value was either too large or too small for an Int64.

Please advice.

Thanks.
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

public String encrypt(String plaintext) throws Exception {
MessageDigest md = null;

md = MessageDigest.getInstance("SHA");

md.update(plaintext.getBytes("UTF-8"));

byte raw[] = md.digest();

String hash = (new BASE64Encoder()).encode(raw);

return hash;
}
If you're in a 32 bit OS you may need to use ToInt32 (I can't double-check right now but as a long is 32 bits on a 32 bit OS, a 64 bit long in C# will probably fail the type checking).

Also what is "getCreateDate()" ?   If it's an object, it'll need to implement the IConvertible interface and define how it gets converted to an Int64.  

If it's a regular 32 bit long... no conversion needed.  
Avatar of theartha

ASKER

I tried in C# ...

plainText i.e String doesn't contain definition for getBytes("UTF-8")

Please advice...
What's the return type of this.getCreateDate()?
@Kelaros:

I got the same error when I use Int32. getCreateDate() is a String of 80 characters in length.
@wdosanjos:

this.getCreateDate() returns a String
Please provide some sample values returned by this.getCreateDate().
I think the Java and the C# versions of this.getCreateDate() are returning different values, because the Java's new Long(this.getCreateDate()) would throw a NumberFormatException exception if the value returned by this.getCreateDate() is not a long.
ASKER CERTIFIED SOLUTION
Avatar of theartha
theartha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This solution works for me....