Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Conversion??

Status
Not open for further replies.

shussai2

Programmer
Aug 28, 2003
42
CA
Binary Conversion??


Hi guys,

Sorry for repeating the same message but I forgot about the subject. PLEASE REPLY TO THIS THREAD AND IGNORE THE BOTTOM ONE. Bear with me, this is my second time posting anything. I am stuck on one more problem here. I want to convert an integer into 8-bit binary format and then show that 8-bit binary on the screen. So for example suppose I want to convert integer 7:

Integer ------ 7
8-binary --- 0000 0111

Display on screen ---- 0000 0111

Please let me know if there exist a command to do so.

Regards,

Sajjad

 
There are a variety of approaches you can take, but the easiest is to use Tcl's binary format and binary scan commands (courtesy of Helmut Giese):

Code:
proc val2bin val {
    set binRep [binary format c $val]
    binary scan $binRep B* binStr
    return $binStr
}

[tt]% val2Bin 7
00000111[/tt]

Check out the Tcl'ers Wiki ( for more information on this topic. I'd suggest starting with "binary," and "Binary representation of numbers,"
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Once again I really like to thank Mr. Jones for excellent answer to this question. It really helped me alot.

Thank you very much.

Regards,

Sajjad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top