Skip to content

Instantly share code, notes, and snippets.

@oscarryz oscarryz/led.go
Created Mar 20, 2012

Embed
What would you like to do?
Led converter in Go
package main
import (
"fmt"
"strings"
"strconv"
)
func main() {
fmt.Printf("%s\n", convertToLCD( 1234567890 ) )
}
// Convert a given number into an "LCD" esque representation:
// ie
// 42 = _
// |_| _|
// ||_
func convertToLCD( n int ) string {
i , result , numbers := 0 , "" , strings.Split( all_numbers, ".\n" )
for _, char := range strconv.Itoa( n ) {
current := numbers[ char - '0' ]
if i == 0 {
result = current
} else {
r1, r2, r3 := 3*i, 6*i+1, 9*i+2
result = result[0 : r1] + current[0:4] +
result[r1+1 : r2] + current[4:8] +
result[r2+1 : r3] + current[8:12]
}
i++
}
return result
}
const all_numbers =
` _
| |
|_|
.
|
|
.
_
_|
|_
.
_
_|
_|
.
|_|
|
.
_
|_
_|
.
_
|_
|_|
.
_
|
|
.
_
|_|
|_|
.
_
|_|
_|
`
@oscarryz

This comment has been minimized.

Copy link
Owner Author

commented Mar 20, 2012

Output:

$ 6g led.go  ; 6l led.6  ; ./6.out 
    _  _     _  _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|| |
  ||_  _|  | _||_|  ||_| _||_|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.