Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active August 29, 2015 14:26
Show Gist options
  • Save ik5/805f775feb85265c3b67 to your computer and use it in GitHub Desktop.
Save ik5/805f775feb85265c3b67 to your computer and use it in GitHub Desktop.
Calculating how many SMS fragments will a message have
package main
import (
"fmt"
"math"
"os"
"unicode/utf8"
)
func calculateSmsFragments(message string) uint64 {
mb_len := utf8.RuneCountInString(message)
byte_len := len(message)
max_message_length := 160
max_multi_message_length := 153
if mb_len != byte_len {
max_message_length = 70
max_multi_message_length = 67
}
if mb_len > max_message_length {
return uint64(math.Ceil(float64(mb_len) / float64(max_multi_message_length)))
}
return 1
}
func main() {
message := os.Args[1]
fmt.Printf("Message \"%s\" will be sent as #%d fragments\n", message, calculateSmsFragments(message))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment