An Efficient Method for converting bits to strings

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <string. h> # include <ARPA/inet. h> # define row_unit 512 # define unit_length 500/* Where row_unit is the bit length used, which is an integer multiple of 64, but the product greater than uiit_length unit_length is the bit length to be converted. Unit_length is the bit length to be converted. It can also be 64, 64, etc. row_unit must be no less than unit_length */unsigned long bits_str [256]; static inline int bits_to_str_init (void) {int I, low, high; int bit_str [16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101, 0x00010000, 0x00010001, 0x00010100, 0x00010101, 0x01000000, 0x01000001, 0x01000100, 0x01000101, 0x01010000, 0x01010001, 0x01010100,}; memset (bits_str, '0', sizeof (bits_str); for (I = 0; I <256; ++ I) {LOW = I & 0x0f; high = I & 0xf0; high> = 4; * (int *) & bits_str [I] + = htonl (bit_str [High]); * (int *) & bits_str [I] + 1) + = htonl (bit_str [low]);} return 0;} static inline int bitstostr (char * STR, unsigned char * bits) {int I; unsigned long * Pos = (unsigned long *) STR; int c = row_unit> 3; for (I = 0; I <C; ++ I) {* Pos = bits_str [(INT) (* bits)]; ++ Pos; ++ bits;} STR [unit_length] = 0; return 0 ;} int main (INT argc, char * argv []) {If (bits_to_str_init () <0) Return-1; unsigned char bits [64]; int I; for (I = 0; I <64; ++ I) {bits [I] = I;} Char STR [512]; bitstostr (STR, BITs ); for (I = 0; I <strlen (STR); ++ I) {printf ("% C", STR [I]); If (I + 1) & 0x7) = 0) printf ("\ n");} return 0 ;}

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.