Avatar of theartha
theartha
Flag 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.
C#

Avatar of undefined
Last Comment
theartha

8/22/2022 - Mon
SAMIR BHOGAYTA

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;
}
Kelaros

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.  
theartha

ASKER
I tried in C# ...

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

Please advice...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
wdosanjos

What's the return type of this.getCreateDate()?
theartha

ASKER
@Kelaros:

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

ASKER
@wdosanjos:

this.getCreateDate() returns a String
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
wdosanjos

Please provide some sample values returned by this.getCreateDate().
wdosanjos

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
theartha

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
theartha

ASKER
This solution works for me....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes