
#use wml::openssl-macros area=docs page=BN_bn2bin

<title>Documents, BN_bn2bin(3)</title>

<h1>BN_bn2bin(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BN_bn2bin, BN_bin2bn, BN_bn2hex, BN_bn2dec, BN_hex2bn, BN_dec2bn, BN_print,
BN_print_fp, BN_bn2mpi, BN_mpi2bn - format conversions

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bn.h&gt;
</PRE>
<PRE> int BN_bn2bin(const BIGNUM *a, unsigned char *to);
 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
</PRE>
<PRE> char *BN_bn2hex(const BIGNUM *a);
 char *BN_bn2dec(const BIGNUM *a);
 int BN_hex2bn(BIGNUM **a, const char *str);
 int BN_dec2bn(BIGNUM **a, const char *str);
</PRE>
<PRE> int BN_print(BIO *fp, const BIGNUM *a);
 int BN_print_fp(FILE *fp, const BIGNUM *a);
</PRE>
<PRE> int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
 BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BN_bn2bin()</CODE> converts the absolute value of <STRONG>a</STRONG> into big-endian form and stores it at <STRONG>to</STRONG>. <STRONG>to</STRONG> must point to <CODE>BN_num_bytes(</CODE><STRONG>a</STRONG>) bytes of memory.

</P>
<P>
<CODE>BN_bin2bn()</CODE> converts the positive integer in big-endian form
of length
<STRONG>len</STRONG> at <STRONG>s</STRONG> into a <STRONG>BIGNUM</STRONG> and places it in <STRONG>ret</STRONG>. If <STRONG>ret</STRONG> is NULL, a new <STRONG>BIGNUM</STRONG> is created.

</P>
<P>
<CODE>BN_bn2hex()</CODE> and <CODE>BN_bn2dec()</CODE> return printable
strings containing the hexadecimal and decimal encoding of <STRONG>a</STRONG> respectively. For negative numbers, the string is prefaced with a leading
'-'. The string must be freed later using <CODE>OPENSSL_free().</CODE>

</P>
<P>
<CODE>BN_hex2bn()</CODE> converts the string <STRONG>str</STRONG> containing a hexadecimal number to a <STRONG>BIGNUM</STRONG> and stores it in **<STRONG>bn</STRONG>. If *<STRONG>bn</STRONG> is NULL, a new
<STRONG>BIGNUM</STRONG> is created. If <STRONG>bn</STRONG> is NULL, it only computes the number's length in hexadecimal digits. If the
string starts with '-', the number is negative. <CODE>BN_dec2bn()</CODE> is
the same using the decimal system.

</P>
<P>
<CODE>BN_print()</CODE> and <CODE>BN_print_fp()</CODE> write the
hexadecimal encoding of <STRONG>a</STRONG>, with a leading '-' for negative numbers, to the <STRONG>BIO</STRONG> or <STRONG>FILE</STRONG>

<STRONG>fp</STRONG>.

</P>
<P>
<CODE>BN_bn2mpi()</CODE> and <CODE>BN_mpi2bn()</CODE> convert <STRONG>BIGNUM</STRONG>s from and to a format that consists of the number's length in bytes
represented as a 4-byte big-endian number, and the number itself in
big-endian format, where the most significant bit signals a negative number
(the representation of numbers with the MSB set is prefixed with null
byte).

</P>
<P>
<CODE>BN_bn2mpi()</CODE> stores the representation of <STRONG>a</STRONG> at <STRONG>to</STRONG>, where <STRONG>to</STRONG>
must be large enough to hold the result. The size can be determined by
calling <CODE>BN_bn2mpi(</CODE><STRONG>a</STRONG>, NULL).

</P>
<P>
<CODE>BN_mpi2bn()</CODE> converts the <STRONG>len</STRONG> bytes long representation at <STRONG>s</STRONG> to a <STRONG>BIGNUM</STRONG> and stores it at <STRONG>ret</STRONG>, or in a newly allocated <STRONG>BIGNUM</STRONG>
if <STRONG>ret</STRONG> is NULL.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>BN_bn2bin()</CODE> returns the length of the big-endian number placed
at <STRONG>to</STRONG>. <CODE>BN_bin2bn()</CODE> returns the <STRONG>BIGNUM</STRONG>, NULL on error.

</P>
<P>
<CODE>BN_bn2hex()</CODE> and <CODE>BN_bn2dec()</CODE> return a
null-terminated string, or NULL on error. <CODE>BN_hex2bn()</CODE> and
<CODE>BN_dec2bn()</CODE> return the number's length in hexadecimal or
decimal digits, and 0 on error.

</P>
<P>
<CODE>BN_print_fp()</CODE> and <CODE>BN_print()</CODE> return 1 on success,
0 on write errors.

</P>
<P>
<CODE>BN_bn2mpi()</CODE> returns the length of the representation.
<CODE>BN_mpi2bn()</CODE> returns the <STRONG>BIGNUM</STRONG>, and NULL on error.

</P>
<P>
The error codes can be obtained by <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/bn.html#">bn(3)</A>, <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>, <A HREF="../crypto/BN_zero.html#">BN_zero(3)</A>,
<EM>ASN1_INTEGER_to_BN(3)</EM>,
<A HREF="../crypto/BN_num_bytes.html#">BN_num_bytes(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>BN_bn2bin(),</CODE> <CODE>BN_bin2bn(),</CODE>
<CODE>BN_print_fp()</CODE> and <CODE>BN_print()</CODE> are available in all
versions of SSLeay and OpenSSL.

</P>
<P>
<CODE>BN_bn2hex(),</CODE> <CODE>BN_bn2dec(),</CODE>
<CODE>BN_hex2bn(),</CODE> <CODE>BN_dec2bn(),</CODE>
<CODE>BN_bn2mpi()</CODE> and <CODE>BN_mpi2bn()</CODE> were added in SSLeay
0.9.0.

</P>
:}

