github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/common/bech32/example_test.go (about)

     1  package bech32
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  )
     7  
     8  // This example demonstrates how to decode a bech32 encoded string.
     9  func ExampleBech32Decode() {
    10  	encoded := "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx"
    11  	hrp, decoded, err := Bech32Decode(encoded)
    12  	if err != nil {
    13  		fmt.Println("Error:", err)
    14  	}
    15  
    16  	// Show the decoded data.
    17  	fmt.Println("Decoded human-readable part:", hrp)
    18  	fmt.Println("Decoded Data:", hex.EncodeToString(decoded))
    19  
    20  	// Output:
    21  	// Decoded human-readable part: bc
    22  	// Decoded Data: 010e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e160e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16
    23  }
    24  
    25  // This example demonstrates how to encode data into a bech32 string.
    26  func ExampleBech23Encode() {
    27  	data := []byte("Test data")
    28  	// Convert test data to base32:
    29  	conv, err := ConvertBits(data, 8, 5, true)
    30  	if err != nil {
    31  		fmt.Println("Error:", err)
    32  	}
    33  	encoded, err := Bech32Encode("customHrp!11111q", conv)
    34  	if err != nil {
    35  		fmt.Println("Error:", err)
    36  	}
    37  
    38  	// Show the encoded data.
    39  	fmt.Println("Encoded Data:", encoded)
    40  
    41  	// Output:
    42  	// Encoded Data: customHrp!11111q123jhxapqv3shgcgumastr
    43  }