%!TEX pdflatex

\documentclass[14pt]{extarticle}
\usepackage[margin=1cm]{geometry}
\usepackage{xfp}
\usepackage{xstring}
\usepackage{xintexpr}
\usepackage{xintbinhex}
\usepackage{pgffor}
\usepackage{siunitx}
\usepackage{simplekv}
\usepackage{listofitems}
\usepackage{mathtools}
\sisetup{locale=FR}

\begin{document}

\newcommand\convdecbin[1]{%
	\def\resbrut{\xintDecToBin{#1}}
	\StrLen{\resbrut}[\nbchiffres]
	\def\nbgrp{\fpeval{4*ceil(\nbchiffres/4,0)}}
	\ensuremath{\num{#1}=\num[digit-group-size=4,minimum-integer-digits=\nbgrp]{\resbrut}}
}

\defKV[CONVBINHEX]{%
	trait=\def\epcrochet{#1}
}
\setKVdefault[CONVBINHEX]{%
	trait=0.5pt,%
	affbase=true,%
	details=true
}

\newcommand\convbinhex[2][]{%
	\useKVdefault[CONVBINHEX]
	\setKV[CONVBINHEX]{#1}% on paramètres les nouvelles clés et on les simplifie
	\def\chbrut{#2}
	\StrLen{\chbrut}[\nbchiffres] %nb de chiffres du binaire
	\xdef\nbgrp{\fpeval{4*ceil(\nbchiffres/4,0)}} %nb de chiffres avec blocs de 4
	\xdef\nbblocs{\fpeval{\nbgrp/4}} %nb de blocs
	%on rajoute des zeros si besoin := OK
	\xdef\resinter{\chbrut}
	\chbrut\ifboolKV[CONVBINHEX]{affbase}{_{2}}{}=
	\ifboolKV[CONVBINHEX]{details}{%
	\ifnum\nbchiffres<\nbgrp
		\xdef\nbz{\inteval{\nbgrp-\nbchiffres}}
		\xdef\resinter{\strzeros{\nbz}\chbrut}
		\num[digit-group-size=4,minimum-integer-digits=\nbgrp]{\resinter}=
	\fi
	%découpage par blocs et conversion en hexa := OK
	\newcount\cpt
	\cpt0
	\loop\ifnum \cpt<\nbblocs
		\def\iinit{\fpeval{4*\cpt+1}}
		\def\ifinal{\fpeval{4*(\cpt+1)}}
		\StrMid{\resinter}{\iinit}{\ifinal}[\blocinter]
		\convblocbinhex[\epcrochet]{\blocinter}
		\advance\cpt by 1
	\repeat
	=
	}%
	{}
	\xintBinToHex{\chbrut}\ifboolKV[CONVBINHEX]{affbase}{_{16}}{}
}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\strzeros}{m}
{
	\int_compare:nT { #1 > 0 }
	{
		0 \prg_replicate:nn { #1 - 1 } { 0 }
	}
}
\ExplSyntaxOff

\newcommand\convblocbinhex[2][0.5pt]{%
	\underbracket[#1]{#2}_{\xintBinToHex{#2}}
}

%conversion en décimal, écrtiture polynomiale ?
\defKV[CONVTODEC]{%
	basedep=\def\basedepart{#1},%
}
\setKVdefault[CONVTODEC]{%
	basedep=2,%
	affbase=true,%
	details=true,%
	zeros=true
}

\newcommand\convtodec[2][]{%
	\useKVdefault[CONVTODEC]
	\setKV[CONVTODEC]{#1}% on paramètres les nouvelles clés et on les simplifie
	\def\nbdepart{#2}
	\StrLen{\nbdepart}[\nbchiffres]
	\StrChar{\nbdepart}{1}[\chiffre]
	%si on est en base 16
	\xintifboolexpr{\basedepart == 16}%
	{%
		\nbdepart\ifboolKV[CONVTODEC]{affbase}{_{\basedepart}}{} =
		\ifboolKV[CONVTODEC]{details}{%
			\xintHexToDec{\chiffre}\times\basedepart^{\inteval{\nbchiffres-1}}%
			\newcount\cpt
			\cpt2
			\loop\ifnum \cpt<\inteval{\nbchiffres+1}
				\def\puiss{\inteval{\nbchiffres-\cpt}}
				\StrChar{\nbdepart}{\cpt}[\chiffre]
				\ifboolKV[CONVTODEC]{zeros}%
					{%
						+\xintHexToDec{\chiffre}\times\basedepart^{\puiss}%
					}
					{
						\ifnum\chiffre > 0
							+\xintHexToDec{\chiffre}\times\basedepart^{\puiss}%
						\fi
					}
				\advance\cpt by 1
			\repeat
			=
			}
			{}
		\num{\xintHexToDec{\nbdepart}}\ifboolKV[CONVTODEC]{affbase}{_{10}}{}
	}%
	{}
	\xintifboolexpr{\basedepart == 2}%
	{%
		\num[digit-group-size=4]{\nbdepart}\ifboolKV[CONVTODEC]{affbase}{_{\basedepart}}{} =
		\ifboolKV[CONVTODEC]{details}{%
			\chiffre\times\basedepart^{\inteval{\nbchiffres-1}}%
			\newcount\cpt
			\cpt2
			\loop\ifnum \cpt<\inteval{\nbchiffres+1}
				\def\puiss{\inteval{\nbchiffres-\cpt}}
				\StrChar{\nbdepart}{\cpt}[\chiffre]
				\ifboolKV[CONVTODEC]{zeros}%
				{%
					+\chiffre\times\basedepart^{\puiss}%
				}
				{
					\ifnum\chiffre > 0
						+\chiffre\times\basedepart^{\puiss}%
					\fi
				}
			\advance\cpt by 1
			\repeat
			=
			}
			{}
		\num{\xintBinToDec{\nbdepart}}\ifboolKV[CONVTODEC]{affbase}{_{10}}{}
	}%
	{}
}

\verb|$\convbinhex{110011111}$|

\medskip

$\convbinhex{110011111}$

\medskip

\hrule

\medskip

\verb|$\convbinhex[trait=1.5pt]{110011111}$|

\medskip

$\convbinhex[trait=1.5pt]{110011111}$

\medskip

\hrule

\medskip

\verb|$\convbinhex[details=false]{110011111}$|

\medskip

$\convbinhex[details=false]{110011111}$

\medskip

\hrule

\medskip

\verb|$\convbinhex[affbase=false]{110011111}$|

\medskip

$\convbinhex[affbase=false]{110011111}$

\medskip

\hrule

\medskip

\verb|$\convbinhex[affbase=false,details=false]{110011111}$|

\medskip

$\convbinhex[affbase=false,details=false]{110011111}$

\medskip

\hrule

\medskip

\verb|$\convtodec[basedep=16]{19F}$|

\medskip

$\convtodec[basedep=16]{19F}$

\medskip

\hrule

\medskip

\verb|$\convtodec[basedep=2]{110011}$|

\medskip

$\convtodec[basedep=2]{110011}$

\medskip

\verb|$\convtodec[zeros=false,basedep=2]{110011}$|

\medskip

$\convtodec[zeros=false,basedep=2]{110011}$

\medskip

\hrule

\medskip

\verb|$\convtodec[basedep=2,details=false]{110011}$|

\medskip

$\convtodec[basedep=2,details=false]{110011}$

\medskip

\hrule

\medskip

\verb|$\convtodec[basedep=16,details=false]{ACDC}$|

\medskip

$\convtodec[basedep=16,details=false]{ACDC}$



\end{document}