How to get an unsigned byte array from a BigInteger in Java?
<p>I need to convert a <a href="https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html" rel="nofollow noreferrer">BigInteger</a> to an unsigned integer encoded in big-endian format but I am having issues since <a href="https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toByteArray()" rel="nofollow noreferrer">BigInteger.toByteArray</a> returns a signed representation. How can I convert this value to an unsigned format?</p>

<p><strong>(Relatively) Helpful Background</strong></p>

<p>I am working on some code that uses JNI to have c++ call some Java methods to handle some cryptographic functionality (this is a Microsoft CNG provider that offloads some functionality to Java). I have the public key in Java and the BigInteger values that I need to convert are the <a href="https://docs.oracle.com/javase/7/docs/api/java/security/spec/ECPoint.html#getAffineX()" rel="nofollow noreferrer">coordinates of the Elliptic Curve Public Key</a>. According to the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa375520%28v=vs.85%29.aspx?f=255&amp;MSPPError=-2147217396#code-snippet-2" rel="nofollow noreferrer">CNG documentation</a> I need to provide these points as "unsigned integers encoded in big-endian format".</p>

<p><strong>Edit</strong></p>

<p>In hindsight, this might have been a silly post. I was getting confused with negative and positive numbers and how to handle that (and because it's late and my mind has turned to mush) but it turns out that I don't need to deal with that since the <a href="https://crypto.stackexchange.com/questions/16604/why-are-there-only-positive-value-points-on-an-elliptic-curve">elliptic curve points won't be negative</a>. Thank you to everyone who responded on here! I will leave this up in case it helps anyone else.</p>