How does operating system display decimal numbers which are stored as binary? [duplicate]

What kind of magic happen with bytes in memory?

marked as duplicate by David Richerby, D.W.♦Mar 6 at 21:46

• There is nothing magical about base-conversion, and it doesn't need to be done specifically by the operating system. How things are put on the screen is far too much to explain in a Stack Exchange answer, but I guess that's not really the focus of your question. – David Richerby Mar 6 at 15:08
• I think you have misunderstood my question. Let me be more precise. How os knows that it needs to write 4 on my calculator screen if in memory it is stored as binary? – Kondjo Mar 6 at 17:47
• @Kondjo doesn't know. we ask it to write in that way. It is all programmed. – kelalaka Mar 6 at 19:05
• Welcome to CS.SE! This question is so short that it's hard to tell what you already understand and what specifically you are unsure about. A comprehensive answer would require writing more than we can reasonably expect on this site. I would suggest reading the marked duplicates, reading about base conversion, possibly studying a computer architecture textbook, and then if it's still unclear, edit the question or ask a new question where you can tell us more specifically what part of the process you have a question about. – D.W. Mar 6 at 21:48

It depends on the operating system, as for Unix it does not have a system call which displays decimal numbers. The usual way of "displaying" anything is via the write syscall:

 ssize_t write(int fd, const void *buf, size_t nbytes);


Where fd is the file descriptor, specifying which file to write to (eg. stdout), buf a pointer, pointing to the start address of a byte array and nbytes tells the OS how many bytes it shall write.

Here the buf is crucial, it is a character array, so you will have to convert the number you want to "display" to the decimal representation's characters yourself1, it is not the job of the OS itself.

1: Probably you will use an existing implementation, for example stdio.h from the C standard library for which you can find the source code here.

• I don't see how this answers the question. The question is how to display decimal representations of the binary data in memory and your answer is that you have to somehow figure out what the decimal representation is, yourself, and then tell the OS to display that representation. You're assuming as a given the two things that the asker wants to know, and pointing out a misunderstanding in the question that IMO isn't all that significant (it's not the OS that does the actual binary-to-decimal conversion, as the asker assumes. Well, at least for Unix.) – David Richerby Mar 6 at 15:23
• @DavidRicherby: "How does operating system display decimal numbers?" is answered. They also clarified [...] How os knows that it needs to write 4 on my calculator screen if in memory it is stored as binary?", so I'm not sure what else to tell them. There are way too many (even for restricting the scope to only Unix) different implementations, to explain them all. If they don't know how base conversion works or some particular implementation does, they can ask another question (probably on a different sub-stack). – KVN Mar 6 at 22:16
• No, it's not answered. "Call the write syscall" is how you tell the operating system to display the characters, not how the operating system does it. It's like you've answered the question "How do restaurants cook omelettes" with "The customer asks the waiter to bring her an omelette." But I totally agree about being not sure what to tell the asker; that's why I voted to close the question. – David Richerby Mar 6 at 22:25
• It does say "you will have to convert the number you want to "display" to the decimal representation's characters yourself" and points out their misunderstanding, am I missing something? Short question ⇒ short answer, if they want to know "How do I convert bytes to decimal" they should ask that (after reading up). – KVN Mar 6 at 22:34
• I already covered that in my first comment. – David Richerby Mar 6 at 22:35