Numbers are translated according to the encoding supported by the architecture, usually 2's complement for signed integers, and regular unsigned binary encoding for unsigned integers in modern computers. There exist deterministic algorithms for translating a decimal number into binary form and back.
The compiler will perform these algorithms on the character constants it reads from the source code (most likely in a text encoding), and then use some sort of procedure or function to translate them into binary form. Compilers will also usually reduce expressions to constants if possible as a part of their optimizations (e.g. 10 >> 2 becomes 2).
One possible function for, say, compilers written in C might be the atoi()
function from the C Standard Library, which translates a string of ASCII characters into an int
. This int
will have an implementation-specific encoding, but is usually 2's complement these days.
Since the translation of text to binary encoding is implementation-specific, the terms of how it actually does it (whether in software or hardware) is not guaranteed. However, usually the translation will just take place as a procedure written in software to run on top of the more basic hardware instructions (e.g. a direct ASCII --> binary integer representation may not exist on something as simple as 32-bit MIPS, but there will be an atoi()
function for a C Standard Library on MIPS).