2013-07-02 17:54:14 piaol 阅读数 48
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15604 人正在学习 去看看 何山
NSString *dictString = [dict JSONFragment];//组合成的。
用这个就行了。。。
dictString==={"content":"Sadgfdfg","phoneno":"","email":"1049055935@qq.com"}===
 
// 十六进制转换为普通字符串的。
+ (NSString *)stringFromHexString:(NSString *)hexString {  //
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[[NSScanner alloc] initWithString:hexCharStr] autorelease];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    NSLog(@"------字符串=======%@",unicodeString);
    return unicodeString;
    
    
}

//普通字符串转换为十六进制的。

+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

 

 

 

 

2015-12-31 15:55:33 iOSbird 阅读数 471
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15604 人正在学习 去看看 何山

将16进制字符串转换成16进制数,返回NSData类型的数据


- (NSData *)transformSignString:(NSString *)hexString{


    int j=0;

    NSInteger desLength = hexString.length/2;

    if (hexString.length%2 != 0) {

        

        return nil;

    }

    Byte bytes[desLength]; ///3ds keyByte 数组, 128

    for(int i=0;i<[hexString length];i++)

    {

        int int_ch; /// 两位16进制数转化后的10进制数

        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)

        int int_ch1;

        if(hex_char1 >= '0' && hex_char1 <='9')

            int_ch1 = (hex_char1-48)*16;   //// 0 Ascll - 48

        else if(hex_char1 >= 'A' && hex_char1 <='F')

            int_ch1 = (hex_char1-55)*16; //// A Ascll - 65

        else

            int_ch1 = (hex_char1-87)*16; //// a Ascll - 97

        i++;

        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)

        int int_ch2;

        if(hex_char2 >= '0' && hex_char2 <='9')

            int_ch2 = (hex_char2-48); //// 0 Ascll - 48

        else if(hex_char2 >= 'A' && hex_char2 <='F')

            int_ch2 = hex_char2-55; //// A Ascll - 65

        else

            int_ch2 = hex_char2-87; //// a Ascll - 97

        

        

        int_ch = int_ch1+int_ch2;

        NSLog(@"int_ch=%d",int_ch);

        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里

        j++;

    }

    NSData *newData = [[NSData alloc] initWithBytes:bytes length:desLength];

    NSLog(@"newData=%@",newData);

    return newData;

}

2015-12-10 16:00:40 sandy_shell 阅读数 1021
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15604 人正在学习 去看看 何山

//把字符串转换成十六进制字符串

+ (NSString *)hexStringFromString:(NSString *)string{

    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];

    Byte *bytes = (Byte *)[myD bytes];

    //下面是Byte 转换为16进制。

    NSString *hexStr=@"";

    for(int i=0;i<[myD length];i++){

        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数

        if([newHexStr length]==1){

            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];

        }

        else{

            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];

        }

        if (i != [myD length] -1){

            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,@"-"];

        }

    }

    return hexStr; 

}


//把一个十六进制拼成的字符串按分隔符分开,将每一个十六进制的值存到array中,返回长度

+(NSInteger)byteArrayFromHexString:(NSString *)string separater:(NSString *)separater array:(Byte *)array

{

  

    NSArray *hexArray = [string componentsSeparatedByString:separater];

    Byte *mac = array;

    NSInteger count = hexArray.count;

    for (int i = 0; i < count; i++)

    {

        @autoreleasepool {

            NSString *s = [hexArray objectAtIndex:i];

            NSString *hexStr = [NSString stringWithFormat:@"0x%@", s];

            NSScanner *scanner = [NSScanner scannerWithString:hexStr];

            unsigned int m = 0;

            [scanner scanHexInt:&m];

            *mac++ = m;

            s = nil;

            hexStr = nil;

            scanner = nil;

        }

        

        

    }

    hexArray = nil;

    return count;

}


2016-11-24 15:57:17 dkq972958298 阅读数 7416
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15604 人正在学习 去看看 何山

其中将十六进制字符串转换成NSData的代码如下:

- (NSData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
    LEDEBUG(@"hexdata: %@", hexData);
    return hexData;
}

将NSData转换成十六进制的字符串则可使用如下方式:


- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}





2016-11-10 17:58:23 java_exception_souji 阅读数 1416
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15604 人正在学习 去看看 何山
import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入字符串:");
        String str = scanner.next();
        System.out.println(str);
        uploadFile(HexToByte(str), "");
    }

    public static void uploadFile(byte[] fs, String path){
        try{
            path = "D:\\data\\a.png";
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(fs, 0, fs.length);
            imageOutput.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static byte[] HexToByte(String hexString){
        int len = hexString.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
            b[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return b;
    }
}

16进制转byte[]

阅读数 78

没有更多推荐了,返回首页