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

/ StringRecipes / Cookbook.StringRecipeDecodingBinaryMessages

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

Decoding Binary Messages

Problem

You have a string containing the characters 0 and 1, representing, in binary, the ASCII values of some text. You want to convert this string into normal, readable, ASCII.

Solution

The solution has four steps:

  1. Chop the string into its component parts. ASCII values are 8 bits long, so we chop the string into sub-strings that are 8 characters long
  2. Convert each substring into the number it represents
  3. Convert each number into the ASCII character it represents
  4. Combine all the ASCII characters into a single string

; compose : ('b -> 'c) ('a -> 'b) -> ('a -> 'c)
;  sequence the functions a and b, returning the result of applying
;  a to the result of applying b to x
;  E.g.  ((compose string->list number->string) 4) = (#\4)
(define (compose a b)
  (lambda (x)
     (a (b x))))

; string->byte-strings : string -> list-of-strings
;  chop the string s into pieces of length 8
;  E.g. (string->byte-strings "0000000011111111") = (list "00000000" "11111111")
(define (string->byte-strings s)
  (string-chop s 8))

; string-chop : string non-negative-integer -> list-of-strings
;  chop the string s into pieces of length length-of-pieces
(define (string-chop s length-of-pieces)
  (let ([len (string-length s)])
    (do ([i  0   (+ i length-of-pieces)]
         [bs '() (cons (substring s i 
                                  (min (+ i length-of-pieces) len))
                       bs)])
      [(>= i len) (reverse bs)])))
                           
; binary-string->integer : string-of-zeros-and-ones -> integer
;  converts a string of binary digits into an integer
;  E.g. (binary-string->integer "101") = 5
(define (binary-string->integer b)
  (string->number b 2))


; decode : string -> string
;  converts a message written in binary to a normal string
(define (decode message)
  (apply string 
         (map (compose integer->char binary-string->integer)
              (string->byte-strings message))))

;; The following message appeared on a ThinkGeek tshirt
;; on the first of april 2004.

(define thinkgeek-tshirt-message
  (string-append "010010010010000001110011011010000110111"
                 "101110000011100000110010101100100001000" 
                 "000110000101110100001000000101010001101" 
                 "000011010010110111001101011010001110110" 
                 "010101100101011010110010000001101111011"
                 "011100010000001000001011100000111001001" 
                 "101001011011000010000001000110011011110" 
                 "110111101101100011100110010000001000100" 
                 "011000010111100100101100001000000110000" 
                 "101101110011001000010000001100001011011" 
                 "000110110000100000010010010010000001100" 
                 "111011011110111010000100000011101110110" 
                 "000101110011001000000111010001101000011" 

                 "010010111001100100000011011000110111101" 
                 "110101011100110111100100100000011100110" 
                 "110100001101001011100100111010000100001"))

(decode thinkgeek-tshirt-message)

Discussion

RnRS defines procedures string->number and number->string to convert between a string containing the external (i.e. printed) representation of a number and number itself. For example:

> (string->number "100")
100
> (number->string 100)
"100"

Both procedures take an optional second argument, which specifies the radix ("base") to use. The default is 10, but values of 2, 8, 10 or 16 may also be used. In the recipe above the radix 2 specifies that we are converting to numbers from a base 2 representation. If, for example, the numbers were represented in base 16 (hexadecimal) it would be a simple matter to replace binary-string->integer with the procedure:

; hexadecimal-string->integer : string-of-0-to-9-and-A-to-F -> integer
;  converts a string of binary digits into an integer
;  E.g. (hexadecimal-string->integer "A9FF") = 43519
(define (hexadecimal-string->integer b)
  (string->number b 16))

Contributors

-- JensAxelSoegaard - 01 Apr 2004

-- GordonWeakliem - 06 Apr 2004

-- NoelWelsh - 14 Apr 2004

I made some fairly major changes. Hope you like. -- NoelWelsh - 14 Apr

CookbookForm
TopicType: Recipe
ParentTopic: StringRecipes
Other Parents:
Next Topic: StringCSV

 
 
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