I have seen some tools which converts SAM to BAM format. I wanted to know how they are converted. SAM file contains nucleotides (A, T, G, C) and how these data are converted to binaries?
It is not only that the sequences are made binary, it is the whole file which is converted into a binary file and not any more human readable. When converting from SAM to BAM you can choose a compression level which makes the BAM much smaller and saves a lot of disk space (the computational cost for high compression is low in my opinion).
There is a tool available called samtools view.Using this tool you will get BAM output.In this tool -S indicates input which is in SAM format where as "b" specifies BAM format which you would like to get.
The way BAM files are used, e.g. as input for genome viewers, it is common to generate a sorted and indexed BAM file (this also can be done with samtools). The exact BAM specification can be found here: http://samtools.github.io/hts-specs/SAMv1.pdf#section.4