@@ -2421,6 +2421,66 @@ \subsection{To ASCII}
24212421mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream);
24222422\end {alltt }
24232423
2424+ Also available in that case is a small extension to \texttt {printf(3) } to print
2425+ a big integer in a formated way. Not every formating is supported (e.g.: no
2426+ thousands separator) but normal alignment works well.
2427+
2428+ Modifiers are \texttt {Z } for a big integer \texttt {M } for a \texttt {mp\_ digit } and
2429+ \texttt {N } to print the array \texttt {a->dp } of a \texttt {mp\_ int a }.
2430+
2431+ Specifiers are \texttt {d, x, o, b, @ } for decimal, hexadecimal, octal, binary, and
2432+ base-64 representations respectively. These specifiers are bound to the extensions
2433+ but can be unregistered individually or all together
2434+
2435+ See example below for the necessary details.
2436+
2437+ This functions are not threadsafe!
2438+ \index {mp\_ printf\_ extension\_ init}
2439+ \index {mp\_ printf\_ extension\_ clear}
2440+ \begin {alltt }
2441+ mp_err mp_printf_extension_init(void);
2442+ void mp_printf_extension_clear(void);
2443+ \end {alltt }
2444+
2445+
2446+ Example:
2447+ \begin {alltt }
2448+
2449+ /* Switch on the extension */
2450+ if((err = mp_printf_extension_init()) != MP_OKAY) goto LTM_ERR;
2451+ ...
2452+ /* Do some calculation with big integer a */
2453+ ...
2454+
2455+ printf("Bigint decimal: %Zd \textbackslash{}n", &a);
2456+ printf("Bigint hexadecimal: %Zx \textbackslash{}n", &a);
2457+ printf("Bigint octal: %Zo \textbackslash{}n", &a);
2458+ printf("Bigint binary: %Zb \textbackslash{}n", &a);
2459+ printf("Bigint base-64: %Z@ \textbackslash{}n", &a);
2460+
2461+ printf("Limb decimal: %Md \textbackslash{}n", a.dp[0]);
2462+ printf("Limb hexdecimal: %Mx \textbackslash{}n", a.dp[0]);
2463+ /* and so on */
2464+
2465+ printf("Array decimal: %Nd \textbackslash{}n", &a);
2466+ printf("Array hexadecimal: %Nx \textbackslash{}n", &a);
2467+
2468+ #include <printf.h>
2469+ register_printf_specifier('d', NULL, NULL);
2470+ printf("Bigint number %d: %Zx \textbackslash{}n", 123, &a);
2471+
2472+ /* re-registering is only possible completely */
2473+ if((err = mp_printf_extension_init()) != MP_OKAY) goto LTM_ERR;
2474+ printf("Bigint number %i: %Zd \textbackslash{}n", 123, &a);
2475+
2476+ /* and so on */
2477+
2478+ /* Switch off all extension */
2479+ mp_printf_extension_clear()
2480+
2481+ \end {alltt }
2482+ \textttt {mp\_ printf\_ extension\_ init} returns \texttt {MP\_ VAL } if those functions are not supported.
2483+
24242484\subsection {From ASCII }
24252485\index {mp\_ read\_ radix}
24262486\begin {alltt }
0 commit comments