#import <Foundation/Foundation.h>
@interface NSString (FROAD_NSString_Additionss)
- (Byte * ) getBytes;
+ (NSString * ) stringFromBytes:(Byte *) byte bytesLen:(int)len;
@end
@implementation NSString (FROAD_NSString_Additionss)
- (Byte * ) getBytes
{
NSData * data = [self dataUsingEncoding: NSUTF16BigEndianStringEncoding];
Byte * v_bytes = (Byte*)[data bytes];
return v_bytes;
// NSMutableArray * array = [[[NSMutableArray alloc] init] autorelease];
//
// for (int i = 0 ;i < [data length] ; i++)
// {
// [array addObject:[NSNumber numberWithChar:v_bytes[i]]];
// }
//return [data length];
}
+ (NSString * ) stringFromBytes:(Byte *) byte bytesLen:(int)len
{
NSData * data = [[NSData alloc] initWithBytes:byte length:len];
NSString * s = [[[NSString alloc] initWithData:data encoding:NSUTF16BigEndianStringEncoding] autorelease];
return s ;
}
@end
@interface Hex : NSObject
{
}
@end
@implementation Hex
+ (Byte ) numFromChar : (char ) c
{
if ( c >='0' && c <= '9')
{
return c - '0';
}else if (c >='A' && c <='Z') {
return c - 'A' + 10;
}else if (c >='a' && c <= 'z') {
return c - 'a' + 10;
}
return -1;
}
+ (NSData * ) dataFromHexString : (NSString *) s_t
{
// 4F60 597D
//0100 1111
s_t = [s_t stringByReplacingOccurrencesOfString:@" " withString: @""];
if ([s_t length]%2 !=0)
{
return nil;
}
Byte * retBytes = malloc(sizeof(char) * [s_t length]);
Byte * ori = retBytes;
for ( int i = 0 ; i < [s_t length]; )
{
char highBit = [s_t characterAtIndex:i ++];
char lowBit = [s_t characterAtIndex:i ++];
//to byte
Byte a = [Hex numFromChar:highBit];
Byte b = [Hex numFromChar:lowBit];
*(retBytes ++)= (a<<4) | b;
}
NSData * data = [NSData dataWithBytes:ori length:[s_t length]/2];
return data;
}
+ (NSString * ) stringFromByte:(Byte ) byteVal;
{
NSMutableString *str = [NSMutableString string];
Byte byte1 = byteVal >> 4;
Byte byte2 = byteVal & 0xf;
[str appendFormat:@"%x" , byte1];
[str appendFormat:@"%x" , byte2];
return str;
}
//0000 0000 0000 0000
+ (NSString *) stringFromShort :(short) shortVal
{
NSMutableString *str = [NSMutableString string];
[str appendFormat:@"%x" , shortVal >> 12];
[str appendFormat:@"%x" , (shortVal >> 8 ) & 0xf ];
[str appendFormat:@"%x" , (shortVal >> 4 ) & 0xf];
[str appendFormat:@"%x" , shortVal &0xf];
return str;
}
+ (NSString * ) hexStringfromData:(NSData * )data
{
NSMutableString * str = [NSMutableString string];
Byte * byte = (Byte *)[data bytes];
for ( int i = 0 ;i < [data length] ;i ++)
{
[str appendString:[Hex stringFromByte: *(byte + i)] ];
}
return str;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// String to utf bytes .
// NSString * ori = @"注册成功123";
//
// NSData * s_data = [ori dataUsingEncoding:NSUTF16BigEndianStringEncoding];
//
// NSLog(@"%@" , s_data);
//
// //utf bytes to string .
//
// NSString * s = [[NSString alloc] initWithData:s_data
// encoding:NSUTF16BigEndianStringEncoding];
// NSLog(@"%@" , s);
// hex
NSString * s = [Hex stringFromShort:(short)0x123A ];
NSLog(@"%@" , s);
NSString * v_s = @"4F60 597D";
NSData * data = [Hex dataFromHexString:v_s];
NSLog(@"%@" , data);
NSLog(@"%@" , [Hex hexStringfromData:data]);
[pool drain];
return 0;
}
Object-c Hex 操作
最新推荐文章于 2021-03-07 10:22:30 发布