Monday, December 16, 2013

Rookie prayer request: How to content \ "1B" in NSString data, converted into 0x1B, and assigned to the unsigned char object?

Rookie prayer request: How to content \ "1B" in NSString data, converted into 0x1B, and assigned to the unsigned char object?

I created a UITextField objects, in which the input hexadecimal data \ "1B 76 \".
Want to enter hexadecimal read out, saved as an unsigned char.

Both need to achieve:
Input: uiTextField.text = \ "1B 76 \";

Output: unsigned char dest [2];
dest [0] = 0x1B;
dest [1] = 0x76;

O (∩ _ ∩) O haha ​​~ gratitude -----
Reply:
A char a char turn, split into sub-string, and then turn into int type, respectively
Reply:
dest [0] = 0x1B;
dest [1] = 0x76;
You are there to be saved in hexadecimal? Can do this?????
Save As hexadecimal string input, you can write their own algorithms to convert decimal ah, this is not difficult, you can point the computer-based writing and then you put out to convert the value to the array inside. Then when you want to display converted to hex on ok ah

Reply:
 
NSString * textString = @ "1B762b76";
NSLog (@ "% @", textString);

int len ​​= textString.length;
int i = 0;
unsigned char * dest = (unsigned char *) malloc (len);
memset (dest, 0, len);
NSRange range;
range.length = 2;
unsigned char * destTemp = dest;
while (i {
range.location = i;
* DestTemp + + = (char) zh ((char *) [[textString substringWithRange: range] UTF8String]);
i + = 2;
}
NSLog (@ "% s", dest);
free (dest);


Conversion / / hex string to decimal integer
int zh (char str [])
{
int i, n;
int value = 0;
int len ​​= strlen (str);
for (i = 0; i {
if (str [i]> = 'A' && str [i] <= 'F')
n = str [i] - 'A' +10;
else if (str [i]> = 'a' && str [i] <= 'f')
n = str [i] - 'a' +10;
else n = str [i] - '0 ';
value = value * 16 + n;
}
NSLog (@ "% d", value);
return value;
}


Reply:
Ladybird gratitude heroes.
Reply:
The reply was deleted at the moderator 2012-02-02 10:11:00

No comments:

Post a Comment