2015-12-31 15:55:33 iOSbird 阅读数 471

将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-10-07 16:58:05 qiuxuewei2012 阅读数 963

iOS中的UIColor可以使用16进制来选取颜色. 
预先定义如下:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> UIColorFromHex(s)   </span>
    [UIColor colorWithRed:(((s & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xFF0000</span>) >> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>))/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.0</span> 
                    green:(((s & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xFF00</span>) >> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>))/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.0</span> 
                     blue:((s & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xFF</span>))/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.0</span>  alpha:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span>]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

使用方法:

<code class="hljs fix has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">view.backgroundColor </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> UIColorFromHex(0xdcdcdc);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

如果要使用RGB格式呢?

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define RGBCOLOR(r,g,b) </span>
    [UIColor colorWithRed:r/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.f</span> 
                    green:g/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.f</span> 
                     blue:b/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.f</span> 
                    alpha:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.f</span>]
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define RGBCOLOR(r,g,b,a) </span>
    [UIColor colorWithRed:r/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.f</span> 
                    green:g/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.f</span> 
                     blue:b/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.f</span> 
                    alpha:a]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

使用方法:

<code class="hljs fix has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">btn.backgroundColor </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> RGBCOLOR(33, 33, 33);</span></code>
2016-08-18 23:02:25 sinat_20559947 阅读数 2859

创建UIColor的分类

//  UIColor+Addition.h

#import <UIKit/UIKit.h>

@interface UIColor (Addition)

/// 使用 16 进制数字创建颜色,例如 0xFF0000 创建红色
///
/// @param hex 16 进制无符号32位整数
///
/// @return 颜色
+ (instancetype)colorWithHex:(uint32_t)hex;

/// 生成随机颜色
///
/// @return 随机颜色
+ (instancetype)randomColor;

/// 使用 R / G / B 数值创建颜色
///
/// @param red   red
/// @param green green
/// @param blue  blue
///
/// @return 颜色
+ (instancetype)colorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue;

@end
//  UIColor+Addition.m

#import "UIColor+Addition.h"

@implementation UIColor (Addition)

+ (instancetype)colorWithHex:(uint32_t)hex {
    uint8_t r = (hex & 0xff0000) >> 16;
    uint8_t g = (hex & 0x00ff00) >> 8;
    uint8_t b = hex & 0x0000ff;

    return [self colorWithRed:r green:g blue:b];
}

+ (instancetype)randomColor {
    return [UIColor colorWithRed:arc4random_uniform(256) green:arc4random_uniform(256) blue:arc4random_uniform(256)];
}

+ (instancetype)colorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue {
    return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
}

@end
2011-10-19 21:26:19 wqjtq 阅读数 78
	/**
	 * 16进制的字符串表示转成字节数组
	 *
	 * @param hexString
	 *			16进制格式的字符串
	 * @return 转换后的字节数组
	 **/
	public static byte[] hexStr2ByteArray(String hexString) {
		if (StringUtils.isEmpty(hexString))
			throw new IllegalArgumentException("this hexString must not be empty");
 
		hexString = hexString.toLowerCase();
		final byte[] byteArray = new byte[hexString.length() / 2];
		int k = 0;
		for (int i = 0; i < byteArray.length; i++) {
						//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
						//将hex 转换成byte   "&" 操作为了防止负数的自动扩展
						// hex转换成byte 其实只占用了4位,然后把高位进行右移四位
						// 然后“|”操作  低四位 就能得到 两个 16进制数转换成一个byte.
						//
			byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
			byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
			byteArray[i] = (byte) (high << 4 | low);
			k += 2;
		}
		return byteArray;
	}

   /**
    * 16进制字符串转换成byte数组
    * @param 16进制字符串
    * @return 转换后的byte数组
    */
  	public static byte[] hex2Byte(String hex) {
		String digital = "0123456789ABCDEF";
		char[] hex2char = hex.toCharArray();
		byte[] bytes = new byte[hex.length() / 2];
		int temp;
		for (int i = 0; i < bytes.length; i++) {
		// 其实和上面的函数是一样的 multiple 16 就是右移4位 这样就成了高4位了
		// 然后和低四位相加, 相当于 位操作"|" 
		//相加后的数字 进行 位 "&" 操作 防止负数的自动扩展. {0xff byte最大表示数}
			temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte) (temp & 0xff);
		}
		return bytes;
	}

 

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