IOS Verification Code

Source: Internet
Author: User
<span id="Label3"></p>Converts a hexadecimal string to UIImage<p><p>Recently wrote a project, there is a verification code, but the interface returned is not the URL of the Captcha picture, but the returned byte array 16 binary String. This requires the 16 binary string first byte array, followed by the byte array into nsdata, and finally the NSData into Uiimage. The most important of these is how to convert a 16 binary string into a byte array. The first thing to do is to associate the byte array with the 16 binary. A 1-byte (byte) is a 8-bit (bit). A bit represents a 0 or 1 (that is, a binary). 4 binary numbers make up a hexadecimal number. This associates the 16 binary string with the byte array. The sum up is 1 bytes (byte) = 8 bits (bit) = 8 bits binary number = 2 hexadecimal digits. Since the interface is a 16-character string generated from byte data, the string returned by the interface must be a string divisible by 2, so it's much easier to see the Code.</p></p><p><p>Here's the core code:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>Converts a hexadecimal string to Nsdata-(nsdata *) transstrhextodata: (nsstring *) strhex{//bytes index Nsuinteger j = 0; Nsinteger len = strhex.length; If (len% 2 = = 1) {///if not divisible by 2, the 16-binary string does not satisfy the picture Graph. It is hereby stated that if you simply convert the 16 into nsdata, you can kill the if, return nil; }///dynamically allocates memory Byte *bytes = (byte *) malloc ((len/2 + 1) * sizeof (byte)); Initialize memory where memset is the function of populating a block of memory with a given value, which is the quickest way to clear 0 operations on a larger struct or array (bytes, ' memset ', (len/2 + 1) * sizeof (Byte)); The For loop is actually the process of converting a 16 binary string into a byte array for (nsuinteger i = 0; I < strhex.length; i + = 2) {///one byte byt E is a 8 bit (bit) bit one that represents a 0 or 1 (that is, Binary) every 8 bits (bit) of a byte (byte) so every time you take 2 for a character combination into a byte is actually 2 16 binary character is actually 8 bits (bit) is a byte byte nsstring *st r = [strhex Substringwithrange:nsmakerange (i, 2)]; Convert 16 binary string to decimal unsigned long uint_ch = Strtoul ([str utf8string], 0, 16); bytes[j] = uint_ch; Self-increment J + +; }///convert byte array to nsdata nsdata *data = [[nsdatA alloc] initwithbytes:bytes length:len/2]; Release Memory Free (bytes); Return data;}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>All of the 16 binary strings are converted to Nsdata. In the conversion to UIImage must have known ...</p></p><p><p>The following is a 16 binary string of the verification code, which can be tested with the following data</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>89504e470d0a1a0a0000000d494844520000005a000000320806000000dc0fcc3500000a754944415478daed998957145716c6f307cc9c93334b124f2 6c6498c4b0c51a332c6054570478d5bdc22d2b2854541e20682bb44148c10346a44625c70898a062411017103456491455110b0d99a86ea7da9eaee6f de7b0e9e710459ca8c33a6ee39757aa9eaaaaedfbbefbbdf7df51aa4f8afc46b120209b4045a0a09b4045a022d85045a022dc5ef13b4aa59833ab9020 Db54a988c66f0bc403e37a2b2bc0675350af29d891d6736f1d0eb0c10c87e097407c266b3a1ee910287f79cc39e8863880d3f82c8d038acf1dd09bf39 9be03f6723427c76627d402cc2fc63104abe0f5fb1077bb71fc3f1b8f3484bca424d55bd04babdb05a6da87dd480e9c39660ec478b31618027260ef08 263ef4518dd8b6eaeecfdb88f3dd8ab535f37729c3bc6f65b0ce70f6598d0df03319b0efdbe41d36cb55aaded1e575d518b8890fdf84a1681d59e51d8 10b80b5fafdc8750bf684cb5f78543cf85709db00aabbca2584607b945c07d5a28e6392dc77ce7e558b6682b4a0a1ec062b1fc7e4053b845770b117b3 01a172ea7c064367648978bf2ca5090731725f9e5b857f410f78a1fe26aea2dacf28862a003166cc1cf273270e3722172aedd21af05484bcec6992317 716cff79e45e2f86416f7a354153a874Fbf730994d389d72122e6e4ee4f54487ce63112cacf0592d56763e9a9966338f86ba26c47df3131c3e5808994b08327fbd8966a58a15489eec57715a3 4d637a3be4609a58283d1f08a825656d7931b363f055ea3d320f6876f30c4e52396d162c2a033e25c423ad3e269fff043d2f10c7004f47f0e6ecbb52d 16ebab039a6a6e6d5915d68f74c78f81db61369a9eba599d5e07bf500ff41df32eaee75e11752d81647ae62f39983cc89b1441197e883dc31c8ad8596 83693cd642333c3c68a716b03d772ec4b014d2f6cd0e810e71b8e2fdf7286f2517d2b3761c6a7d3fa63e4cc21287d502cfa7a3957ef60f6c840a6d351 6be3515126177dcee6260bf26ff1c8cde651562210d9b13ca798dbc8805ba1d7934d47dfdbc4816ed146aeb6119a46ee19b740f70b8280ec9f52e1d36 d2c320e24b67a0ea3d1881ec3de805bd03c343689cfbebcec12c8268730d0d485941494bf808cb6206aa31eb39cd598335e83dd910634355adbb49f4a 8580eb9966a49c3593222d741d34bdb85ad18cbcf357707ccdb7c83e91fad4b461234bf4afaab00c9b1c3db16d6a40abb68d7e77bfe21e7a3abc8d88e F36b539f53a1a74e06f679560c9bccd1845fcf4d2059b917fa394154371a0ad387ac088a90e2ad875e31020d3e2ce6da14dd065a5021b98e98e1a6c58 a1eb3a6893de88bce42bd83ac10f7e6f8fc35ef7f568782887cd6a7b02b049de801f96462078e05c54e6df6bf506b43a2df61cde854f26f5c5b9d433a2a58a3a919cab4558e9be1d8e7d16c18 378e7ebe979d010b721268c461b2efd6ac6dc096a0ceec1c1f3730dd27f31b77a2c4ff4bcb88047b0bf0e1ffe9583d71c4dd741537b252f29c7c1c06d F07d7b3cc95a2fdc3a77890c8081edd736ab911c7508a143be40465c223bbe35d0f7ca4be11a38072e3267dc2eca110d5aa73520eb523e82bd76b08c9 6b9acc185c46b50367022678a0d85b93c7c176831ea23155ca76a907cdad4c6603f3e76c5975af47b83c3575e3a711aade33448db770aa1f65f600dd9 2eee3d091d014c176d2e1d3c87d0a1ae38b3793f83de961b49387b08f653ed10b26d39e4f58f4483d6a874b89e9687e58bb7b156dc6bc65afc7c2c837 96731b2447f9b9fc363b9b70ea3ecd424b3354822a08dc667e5d0682005398b2799ac455f92d1d15f1bc48116781e85a9d9f89ac847c0fb53703c6c17 945575b89598cee4e2c7659168ae51b47983544fe72f998911330621e96222f406fd0b014d33da6fce468c215edaebb3b5483e718981160471ed76017 11d419e5a221d2acc9da8466ab289cca0d6405b7139cd8c4943d5e8fd270e69e7cde25d47f59dfbd8316339dcfe300c7b64eb5070210bfedd2722724a 20cbf8e7fdb6c56d78af76438352fc6a1ab392a476dcc82c84f78c75cc75cc750cc2af895709e826d1e72e2fe3219ba181dd5b1c164dd3e25a06dfea712693152989267cf03ac740538b270ab 4d54275ba8215bc80f75c881e2f40c8a0f9d8397b25ea1f543ff7b75ad20dee3e180d87594370ecec61e20a78f1a04921e69ad448397d05fe733731e9 904d09c1d98434b65e2d463aa893a03eda6396866974a0bb0e39d75b075d27b760db3a3d03ed43349d6ab6e8864551558b78ffad5842b2d8eb2f8ed8b 5301435a515cfbd29baef6ace658c5f380acb36f8a1f2d1c317d620d5cb1b9178240dae13831968dfcf3722f9642614754dccd38bc9685afce64f52c3 69a09a58363d2acb5b2ff0b410ce72d6a0cf9f39249d328b6f585a2cdc019f702cfee3706c76f26619de1e649acd2bc303f1e1981e484e3bfbc22053c D97573730d0b34606b0b5696aefd293b2454b07b57747e38c7019a682fd7b1cd605e9406ff3f1f6b81bd4ebace0380b297e8fb3d9b1bf8ac9c60b69c1 Cb738a11f55910c2862e424e62060433dfeee09c4c4ec0a7d30762499817caabeeb79b491d0dfaa8aaf27e0de2634e63bef30a8ce9eb8665ae5b9fd83 B31d2515f6bc1a1ef0d98324285017fe3b054a64106f1d577f204dc220e232dc584e8703d269381e8f726874fba73d8fb8d81796ad1a0a9374e894e80 C7eb0e488949004f9a85f66e866af1e8d9f6e83fbe17d2aea5c2cc9b9f6c2d4ba806a3812d3459ac964ec1a12b7165c595f86e6b02c6d9b9b362b8ca3312d99905501337d291070a6d05cdd6a ccb3c9c07710ce4f03e2a2c98ac81db740d663aa931ce5e85b18354f024cd092d805346aa588bdef2ffbb0c9a3799703be932d60d77c38ee94190973e Ecd0d48e3ff13dfa39bf87155b029057940b79dd233cac2e4705d9ca482b7ef77e09f24b6e13ddaee83418ad5a8fbcec526c0f8d83cb601f4c1ae88db 5fe310c342d92620aa1c05b515723e0d84103962cd2b2a2e83d4fcb7cf5e6603d4e1e36a1e40e8f8bc4ca9d3a622499ceb32647f4ea9da2b206e1ce3e F0796b2cf3d2ed49065b2b3618603fc50e23660ec6966fd723fef83e1c3cb91f7b0e7d8b6ddf6dc6a6e83084c7ae47e4de70d4d4c93b0d9a7ae8dcac6 2f640d6a99f8c48870c9161f128b879974c61a1cbd2d1a2c3141cd764c1bd120105a4e0dd2d16d0a8b03e05f4f1ac7d7629b54ba0d58dcd38b5712f96 be3b0927427741abe4dad5594ec511805bd17bf43b184e1a94b1f347b285fe3e8edd31745a7f067fb04b3ff41af50e568707a159d5d4c942480a7323c 79e70aff088c4e44fbe642b78713b7fc283d22ae6af5fd4d3218b607b06ee6fb2f05f947e933525bb89955356d7b59b79f4cfa5133d1e38b137930dbb 713df1f7e16fb266a517016f3ff56306F9fd11ddf0f1b80f70abe02678a1f3be9a3e9aa2cf01a337fe8888e0fd3875e8026a880b1193cd2fe5090bfdb35585f711332f181b1c1633e01df94d4 Dbd9c69337519cb36f8222c7215228854441f882292b185bd8f89df817d4777e3e2d50b50a93bbf0044f55faf3332f9f85f8c4e81d6abb4381e1a8bd5 fd3f47eeb94c56103b025ad9a460454fb008fffadcc83e3771ca274e84b6e08dcd0abcaad161d0b4ddd6abb55011e36fd4e8bb64955ef6f4fdbfc9682 924d012682924d0126809b414126809b414126809b4045a0a09b4045a0a1aff049de45661b57926d90000000049454e44ae426082</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>Finally attach the generated verification code picture</p></p><p><p></p></p><p><p>IOS Verification Code</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.