Skip to content

Instantly share code, notes, and snippets.

View cxa's full-sized avatar
🦥

realazy cxa

🦥
View GitHub Profile
- (NSString *)convertISBN10To13:(NSString *)isbn10
{
NSString *prefix = [NSString stringWithFormat:@"978%@", [isbn10 substringToIndex:9]];
NSInteger sum = 0;
for (int i=0; i<12; i++){
char c = [prefix characterAtIndex:i];
int n = c - '0';
int w = (i % 2) ? 3 : 1;
sum += w * n;
}
@cxa
cxa / gist:48f1be2653fd35843d98
Created February 26, 2015 12:13
Convert an Integer to bytes
func bytes<T: IntegerType>(i: T) -> [UInt8] {
let p = UnsafeMutablePointer<T>.alloc(1)
p.memory = i
let b = unsafeBitCast(p, UnsafePointer<UInt8>.self)
let bytes = reduce(0..<sizeof(T), []) { $0 + [b[$1]] }
p.destroy()
return bytes
}
@cxa
cxa / gist:226a07fa8aad9335c2fb
Last active August 29, 2015 14:13
Keyboard Layout Model with Swift
enum ShiftKeyType {
case None
case Once
case Always
}
enum PunctuationSwitcherType {
case More
case Numeric
}
@cxa
cxa / gist:bb8708a8214fe6d02365
Last active August 29, 2015 14:02
Swift example: Interacting with C APIs
// BridgeHeader.h
#import <unicode/uchar.h>
// UnicodeBlock.swift
import Foundation
class UnicodeBlock {
class var blocks: UnicodeBlock[] {