Home > Mobile >  Bluetooth development problems, bluetooth command, hex string bytes problem, see a great god please
Bluetooth development problems, bluetooth command, hex string bytes problem, see a great god please

Time:10-31

//into a hexadecimal string: @ "328 a6102033b516386668561df5f0b7e 
"- (void) getByteWithString: (nsstrings *) string
{
Init NSMutableArray * strMutArr=[[NSMutableArray alloc]].
for (int i=0; i {
Nsstrings * STR=[nsstrings stringWithFormat: @ "0 x % @", [string substringWithRange: NSMakeRange (I, 2)]].
[strMutArr bd_addObject: STR];
}
//strMutArr Printing results: Printing strMutArr: description of the product & lt; __NSArrayM 0 x145db8730 & gt; X61 x8a x32 (0, 0, 0, 0 x02, 0 x03, 0 x3b, 0 x51, 0 x63, x86, 0 0 x66, 0 x85, 0 x61, 0 XDF, 0 x5f, 0 x0b, 0 x7e)
Byte bytes [16].
for (int j=0; J & lt; StrMutArr. Count; J++)
{
Const char * c=[strMutArr UTF8String] [j].
Byte Byte=(Byte) strtol (c, NULL, 16);
Bytes [j]=byte;
}
//results of bytes (Byte [16]) bytes={
//[0]='2'
//[1]='\ x8a'
//[2]='a'
//[3]='\ x02'
//[4]='\ x03'
//[5]='; '
//[6]='Q'
//[7]='c'
//[8]='\ x86'
//[9]='f'
//[10]='\ x85'
//[11]='a'
//[12]='\ XDF'
//[13]='_'
//[14]='\ n'
//[15]='~'
//}
}



Now want to is the result of the and one to one correspondence strMuatle results are as follows:
Results of bytes (Byte [16]) bytes={
[0]='\ x32'
[1]='\ x8a'
[2]='\ x61'
[3]='\ x02'
[4]='\ x03'
[5]='\ x3b'
[6]='\ x51'
[7]='\ x63'
[8]='\ x86'
[9]='\ x66'
[10]='\ x85'
[11]='\ x61'
[12]='\ XDF'
[13]='\ x5f'
[14]='\ x0b'
[15]='\ x7e'
}
But never succeed...

Byte Byte=(Byte) strtol (c, NULL, 16); This method for some hexadecimal number sometimes can be converted to the corresponding byte, sometimes you can't, don't know why, please help me to look at and give some advice, how to transform successfully,

CodePudding user response:

Nsscaner, can turn a string hexadecimal int

CodePudding user response:

Nsscaner

CodePudding user response:

Some of the transition easy ambiguity

CodePudding user response:

- (NSData *) hexToBytes: (nsstrings *) STR {
NSMutableData * data=https://bbs.csdn.net/topics/[NSMutableData data];
Int independence idx;
For (independence idx=0; Independence idx + 2 & lt;=STR. Length; Independence idx +=2) {
NSRange range=NSMakeRange (independence idx, 2);
Nsstrings * hexStr=[STR substringWithRange: range];
NSScanner * scanner=[NSScanner scannerWithString: hexStr];
Unsigned int intValue;
[scanner scanHexInt: & amp; intValue];
[data appendBytes: & amp; intValue length: 1);
}
Return the data.
}
Just two two

CodePudding user response:

NSLog (@ "data % @", [self dataConvertToHexString: @ "328 a6102033b516386668561df5f0b7e"]);

Print the results: the data & lt; 328 a6102 033 b5163 86668561 df5f0b7e & gt;


- (NSData *) dataConvertToHexString: (hexString nsstrings *)
{
Const char * chars=[hexString UTF8String];
int i=0;
NSUInteger len=hexString. Length;
NSMutableData * data=https://bbs.csdn.net/topics/[NSMutableData dataWithCapacity: len/2);
Char byteChars [3]={' \ 0 ', '\ 0', '\ 0'};
Unsigned long wholeByte;
While (I & lt; Len) {

[i++ byteChars [0]=chars];
[i++ byteChars [1]=chars];

WholeByte=strtoul (byteChars, NULL, 16);
[data appendBytes: & amp; wholeByte length: 1);
}

Return the data.
}

CodePudding user response:

NSLog (@ "% @", [self hexStringToByte: @ "328 a6102033b516386668561df5f0b7e"]);

//an array of bytes of the string into a NSdaTa
- (NSData *) hexStringToByte: (nsstrings *) string {

Nsstrings * hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString: @ "-" withString: @ ""].
If ([hexString length] % 2!=0) {
Return nil.
}
NSData * data=https://bbs.csdn.net/topics/[hexString dataUsingEncoding: NSUTF8StringEncoding];
Byte * testByte=(Byte *) [data bytes);
Byte testByteArr [16]={0};
for (int i=0; i<16. I++) {
Int post=I * 2;
TestByteArr [I]=(Byte) (([self toByte: testByte [post]] <4) | ([self toByte: testByte [post + 1]]));
NSLog (@ "======% 1122 02 x", testByteArr [I]);
//NSLog (@ "====1122==% d", testByteArr [I]);
}
Return [NSData dataWithBytes: testByteArr length: 16].
}

The 2019-06-11 10:05:57. 401682 + 0800 UDPSocket [2619-831828]====1122==32
The 2019-06-11 10:05:57. 401710 + 0800 UDPSocket [2619-831828]====1122==8 a
The 2019-06-11 10:05:57. 401718 + 0800 UDPSocket [2619-831828]====1122==61
The 2019-06-11 10:05:57. 401725 + 0800 UDPSocket [2619-831828]======1122 02
The 2019-06-11 10:05:57. 401732 + 0800 UDPSocket [2619-831828]======1122 r
The 2019-06-11 10:05:57. 401739 + 0800 UDPSocket [2619-831828]====1122==3 b
The 2019-06-11 10:05:57. 401746 + 0800 UDPSocket [2619-831828]====1122==51
The 2019-06-11 10:05:57. 401753 + 0800 UDPSocket [2619-831828]====1122==63
The 2019-06-11 10:05:57. 401760 + 0800 UDPSocket [2619-831828]====1122==86
The 2019-06-11 10:05:57. 401766 + 0800 UDPSocket [2619-831828]====1122==66
The 2019-06-11 10:05:57. 401773 + 0800 UDPSocket [2619-831828]====1122==85
nullnullnullnullnullnull
  •  Tags:  
  • iOS
  • Related