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;
    // Test BitVecToStr function
    bool test_bitvec_to_string(void) {
    printf("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;
    BitVec bv1 = BitVecInit();
    StrReadFmt("10110", "{}", bv1);
    Str result1 = BitVecToStr(&bv1);
    success     = success && (ZstrCompare(result1.data, "10110") == 0);
    printf(
    BitVec bv4 = BitVecInit();
    StrReadFmt("   1101", "{}", bv4);
    Str result4 = BitVecToStr(&bv4);
    success     = success && (ZstrCompare(result4.data, "1101") == 0);
    printf(
    BitVec bv5 = BitVecInit();
    StrReadFmt("0", "{}", bv5);
    Str result5 = BitVecToStr(&bv5);
    success     = success && (ZstrCompare(result5.data, "0") == 0);
    printf(

Share :

Related Posts

BitVecCompare

BitVecCompare Description Compare two bitvectors lexicographically. Comparison is done bit by bit from left to right.

Read More

BitVecIsSorted

BitVecIsSorted Description Check if bits in bitvector are in sorted order. Useful for certain algorithms and data structures.

Read More

BitVecNumericalCompare

BitVecNumericalCompare Description Compare two bitvectors as unsigned integers. Treats bitvectors as unsigned binary numbers (LSB first).

Read More