Skip to content

Instantly share code, notes, and snippets.

View sorted-bits's full-sized avatar

Wim Haanstra sorted-bits

View GitHub Profile
@sorted-bits
sorted-bits / gist:1302057
Created October 20, 2011 19:26
Validating Int from string
public static bool IsInt(this string text)
{
int integer = 0;
return Int32.TryParse(text, out integer);
}
@sorted-bits
sorted-bits / gist:1302069
Created October 20, 2011 19:30
String to valid filename
static public string ToValidFileName(this string name)
{
string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
string invalidReStr = string.Format(@"[{0}]+", invalidChars);
return Regex.Replace(name, invalidReStr, "_");
}
@sorted-bits
sorted-bits / gist:1302074
Created October 20, 2011 19:31
MD5 and SHA1 hashing
public static string ToSHA1(this string text, Encoding enc)
{
byte[] buffer = enc.GetBytes(text);
SHA1CryptoServiceProvider cryptoTransformSHA1 =
new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(
cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
return hash;
}
@sorted-bits
sorted-bits / gist:1302080
Created October 20, 2011 19:33
Encryption & decryption
public static string Encrypt(this string Message, string passphrase = "")
{
if (passphrase == "")
passphrase = ConfigurationManager.AppSettings["encrpytionPassphrase"];
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
@sorted-bits
sorted-bits / gist:1302088
Created October 20, 2011 19:35
shouldAllowRotateToInterfaceOrientation
+ (BOOL) shouldAllowRotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
NSString* currentOrientation = @"";
switch (interfaceOrientation)
{
case UIInterfaceOrientationLandscapeLeft:
currentOrientation = @"UIInterfaceOrientationLandscapeLeft";
break;
case UIInterfaceOrientationLandscapeRight:
currentOrientation = @"UIInterfaceOrientationLandscapeRight";
@sorted-bits
sorted-bits / gist:1302091
Created October 20, 2011 19:36
shouldAllowRotateToInterfaceOrientation implementation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [WHUtilities shouldAllowRotateToInterfaceOrientation:interfaceOrientation];
}
@sorted-bits
sorted-bits / LocalizationHelper.h
Created October 20, 2011 19:38
LocalizationHelper
//
// LocalizationHelper.h
//
// Created by Wim Haanstra on 9/30/10.
// Copyright 2010 Wim Haanstra. All rights reserved.
//
#import
@interface LocalizationHelper : NSObject { }
@sorted-bits
sorted-bits / LocalizationHelper.h
Created October 20, 2011 19:39
LocalizationHelper
//
// LocalizationHelper.h
//
// Created by Wim Haanstra on 9/30/10.
// Copyright 2010 Wim Haanstra. All rights reserved.
//
#import
@interface LocalizationHelper : NSObject { }
@sorted-bits
sorted-bits / gist:1302242
Created October 20, 2011 20:21
Placing curved text on an UIImage
NSArray* sections = [[NSArray alloc] initWithObjects:@"text1", @"text2", @"text3", @"text4", nil];
- (UIImage*) createMenuRingWithFrame:(CGRect)frame
{
// First fix the frame to make sure it uses the right scaling (iPhone 4 / iPad compatibility).
frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width * scale, frame.size.height * scale);
// Same for the text radius
float scaledTextRadius = textRadius * scale;
float scaledRingWidth = ringWidth;
@sorted-bits
sorted-bits / gist:1302265
Created October 20, 2011 20:28
NSURLConnection in its own thread
NSString* url = @"http://your.url/here";
NSURL* nsurl = [NSURL URLWithString:url];
NSMutableURLRequest* urlReq = [[NSMutableURLRequest alloc] initWithURL:nsurl];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection