Is there any method in Objective-C that converts a hex string to bytes? For example @"1156FFCD3430AA22"
to an unsigned char array {0x11, 0x56, 0xFF, ...}
.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
- How to convert hex string into decimal value
Several solution is returned wrong value if the string like this "DBA"
The correct data for "DBA" string is "\x0D\xBA" (int value : 3514)
if you got a data is not like this "\x0D\xBA" it mean you got a wrong byte because the value will be different, for example you got data like this "\xDB\x0A" the int value is 56074
Here is rewrite the solution:
Fastest NSString category implementation that I could think of (cocktail of some examples):
It is close to 8 times faster than wookay's solution. NSScanner is quite slow.
First attempt in Swift 2.2:
Or, as an extension on String:
This is a continuous work-in-progress, but appears to work well so far.
Further optimizations and a more in-depth discussion can be found on Code Review.
Not in the way you are doing it. You'll need to write your own method to take every two characters, interpret them as an int, and store them in an array.
The
scanHexInt:
and similar methods ofNSScanner
might be helpful in doing what you want, but you'd probably need to break the string up into smaller chunks first, in which case doing the translation manually might be simpler than usingNSScanner
.Modified approach,