s c h e m a t i c s : c o o k b o o k

/ NumberRecipesAll? / Cookbook.NumberRecipesConvertBinDec

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Converting Between Binary and Decimal

Problem

You would like to display the binary representation of an integer, or the integer representation of a binary value. You might want to do this if you were displaying non-textual data, such as what you get from interacting with certain system programs and functions.

Solution

Use the string->integer and integer->string built in functions. Each of these functions takes an optional radix argument, which can be used to determine what number base to use for the output (or which base to use for conversion to a numerical value).

> (number->string 16 2)
"10000"
> (string->number "0110001110" 2)
398

Discussion

Note that this recipe is not limited to binary numbers. We could just as easily have presented hexadecimal or octal versions:

> (number->string 16 16)
"10"
> (string->number "266" 8)
182

See Also

NumberRecipeConvOctHex

CookbookForm
TopicType: Recipe
ParentTopic: NumberRecipesAll?
Other Parents:
Next Topic: NumberRecipesOpListInts

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest