BitVecToStr

Table of Contents

BitVecToStr

Description

Convert bitvector to string representation. Each bit becomes ‘1’ or ‘0’ character. Caller must free the returned string.

Parameters

NameDirectionDescription
bvinBitvector to convert

Usage example (from documentation)

  Str bit_string = BitVecToString(&flags);
  // ... use string ...
  StrDeinit(&bit_string);

Usage example (Cross-references)

    // Empty BitVec - don't output anything
    } else {
    Str bit_str = BitVecToStr(bv);
    StrMerge(o, &bit_str);
    StrDeinit(&bit_str);
    
    // Conversion functions
    Str BitVecToStr(BitVec *bv) {
    ValidateBitVec(bv);
    
    // Convert bitvector to string for regex matching
    Str  bv_str = BitVecToStr(bv);
    bool result = false;
    z          = "10110";
    StrReadFmt(z, "{}", bv1);
    Str result1 = BitVecToStr(&bv1);
    success     = success && (ZstrCompare(result1.data, "10110") == 0);
    WriteFmt(
    z          = "   1101";
    StrReadFmt(z, "{}", bv4);
    Str result4 = BitVecToStr(&bv4);
    success     = success && (ZstrCompare(result4.data, "1101") == 0);
    WriteFmt(
    z          = "0";
    StrReadFmt(z, "{}", bv5);
    Str result5 = BitVecToStr(&bv5);
    success     = success && (ZstrCompare(result5.data, "0") == 0);
    WriteFmt("Test 5 - Zero: {}, Success: {}\n", result5, (ZstrCompare(result5.data, "0") == 0) ? "true" : "false");
    // Test BitVecToStr function
    bool test_bitvec_to_string(void) {
    WriteFmt("Testing BitVecToStr\n");
    
    BitVec bv = BitVecInit();
    
    // Convert to string
    Str str = BitVecToStr(&bv);
    
    // Check result
    
    // Test converting empty bitvec
    Str str_obj = BitVecToStr(&bv);
    result      = result && (str_obj.length == 0);
    StrDeinit(&str_obj);
    // Test converting single bit
    BitVecPush(&bv, true);
    str_obj = BitVecToStr(&bv);
    result  = result && (str_obj.length == 1);
    result  = result && (StrCmpCstr(&str_obj, "1", 1) == 0);
    BitVecPush(&bv, i % 2 == 0);
    }
    str_obj = BitVecToStr(&bv);
    result  = result && (str_obj.length == 1000);
    StrDeinit(&str_obj);
    for (size_t i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {
    BitVec bv  = BitVecFromStr(patterns[i]);
    Str    str = BitVecToStr(&bv);
    
    // Should get exact same string back
    BitVec empty = BitVecInit();
    
    Str empty_str = BitVecToStr(&empty);
    result        = result && (empty_str.length == 0);
    StrDeinit(&empty_str);
    
    // Test string conversion consistency
    Str str = BitVecToStr(&bv);
    result  = result && (ZstrCompare(str.data, test_cases[i].pattern) == 0);
    StrDeinit(&str);
    
    // All three should produce the same result when converted back
    Str str1 = BitVecToStr(&bv1);
    Str str2 = BitVecToStr(&bv2);
    Str str3 = BitVecToStr(&bv3);
    // All three should produce the same result when converted back
    Str str1 = BitVecToStr(&bv1);
    Str str2 = BitVecToStr(&bv2);
    Str str3 = BitVecToStr(&bv3);
    Str str1 = BitVecToStr(&bv1);
    Str str2 = BitVecToStr(&bv2);
    Str str3 = BitVecToStr(&bv3);
    
    // At least two of them should match (bit order might affect one)
    
    // Test string conversion
    Str large_str = BitVecToStr(&large_bv);
    result        = result && (large_str.length == 1000);
    
    // Test NULL bitvec pointer - should abort
    BitVecToStr(NULL);
    
    return false;

Share :

Related Posts

BitVecCosineSimilarity

BitVecCosineSimilarity Description Calculate cosine similarity between two bitvectors. Treats bitvectors as binary vectors and computes cosine of angle between them.

Read More

BitVecRunLengths

BitVecRunLengths Description Analyze run lengths in a bitvector. A run is a sequence of consecutive identical bits. Results array must be pre-allocated with sufficient space.

Read More

BitVecEntropy

BitVecEntropy Description Calculate information entropy of a bitvector. Entropy measures the randomness/information content of the bit pattern.

Read More