相同的key每一次加密的密文都不一样.难于破解.整个密文相关.修改其中一段后,以后的数据解密不正确
typedef unsigned int Unit;
std::string encrypt(const std::string &src,Unit key)
{
Unit seed = 0;
const size_t unitlen = sizeof(Unit);
size_t len = (src.length() + unitlen-1) & ~(unitlen-1);
Unit *srcdata = (Unit*)malloc(len);
bzero(srcdata,len);
memcpy(srcdata,src.data(),src.size());
std::string dest;
int i = 0;
srand(time(NULL));
seed = rand()%0xFFFF;
dest.append((char*)&seed,sizeof(Unit));
do
{
seed = *(srcdata+i) ^ seed ^ key;
dest.append((char*)&seed,unitlen);
i++;
}while(i<len/unitlen);
free(srcdata);
return dest;
}
std::string dencrypt(const std::string &src, Unit key)
{
Unit seed = 0;
Unit *srcData = NULL;
std::string dest;
const size_t unitlen = sizeof(Unit);
srcData = (Unit*)src.data();
seed = *((Unit*)srcData);
int i = 1;
while(i<src.size()/unitlen)
{
seed = *(srcData + i) ^ key ^ seed;
dest.append((char*)&seed,unitlen);
seed = *(srcData + i);
i++;
}
return dest;
}
1.定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型
NSString *a = @"1.2.30";
int b= [[a substringWithRange:NSMakeRange(4,2)] intValue];
NSLog(@"a:%@ \n",a );
NSLog(@"b:%d",b );
Output : 2012-07-05 11:49:08.170 Q[4005:207] a:1.2.30
2012-07-05 11:49:08.172 Q[4005:207] b:30
解析如下:substringWithRange: 专门截取字符串的一块肉
NSMakeRange(4,2) 从第4个字符开始截取,长度为2个字符,(字符串都是从第0个字符开始数的哦~!)
b = [a intValue]; 将 a 转换为 整数型
b = [a floatValue]; 将 a 转换为 小数型
b = [a boolValue]; 将 a 转换为 布尔型(true / false)
b = [a integerValue]; 将 a 转换为 整数型
b = [a longLongValue]; 将 a 转换为 长整型
2。 字符串截取到第n位 (substringToIndex: n)(第n 位不算再内)
- (void)viewDidLoad
{
NSString *a = @"i like long dress";
NSString *b = [a substringToIndex:4];
NSLog(@"\n b: %@",b);
}
b: i li
3。字符串从第n 位开始截取,直到最后 (substringFromIndex:n)(包括第 n 位)
- (void)viewDidLoad
{
NSString *a = @"i like long dress";
NSString *b = [a substringFromIndex:4];
NSLog(@"\n b: %@",b);
}
b: ke long dress
。NSMutableString 为可变的字符串
NSString 为不可变的字符串
-(void)viewDidLoad
{
NSMutableString *a = [[NSMutableString alloc]initWithString:@"123456798"];
NSLog(@" \n a: %@\n",a);
[a deleteCharactersInRange:NSMakeRange(1,2)];
NSLog(@" \n a: %@\n",a);
[a release];
}
2012-07-05 20:59:34.169 Q[9069:207] a: 123456798
2012-07-05 20:59:34.171 Q[9069:207] a: 1456798
方法如下,通过Matrix对图片进行处理。
public Bitmap convertBmp(Bitmap bmp){ int w = bmp.getWidth(); int h = bmp.getHeight(); Bitmap convertBmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(convertBmp); Matrix matrix = new Matrix(); matrix.postScale(1, -1); //镜像垂直翻转 // matrix.postScale(-1, 1); //镜像水平翻转 // matrix.postRotate(-90); //旋转-90度 Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, true); cv.drawBitmap(newBmp, new Rect(0, 0,newBmp.getWidth(), newBmp.getHeight()),new Rect(0, 0, w, h), null); return convertBmp; }