> For the complete documentation index, see [llms.txt](https://docs.stacks.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacks.co/cookbook/clarity/address-utilities/convert-string-to-principal.md).

# Convert string to principal

{% code fullWidth="false" expandable="true" %}

```clarity
(define-read-only (string-to-principal? (input (string-ascii 82)))
  (let (
    ;; Find the dot separator for contract addresses
    (dot (default-to (len input) (index-of? input ".")))
    ;; Extract address part (skip first char which is version)
    (addr (unwrap! (slice? input u1 dot) ERR_INVALID_LENGTH))
    ;; Decode c32 characters to numbers
    (addressc32 (map unwrap-panic-uint (filter is-some-uint (map c32-index addr))))
    ;; Validate all characters are valid c32
    (isValidChars (asserts! (is-eq (len addr) (len addressc32)) ERR_INVALID_CHAR))
    ;; Extract version and decode address data
    (version (unwrap-panic (element-at? addressc32 u0)))
    (decoded (decode-address addressc32))
    ;; Verify checksum
    (checksum (verify-checksum decoded version))
    )
    ;; Construct principal with or without contract name
    (match (slice? input (+ u1 dot) (len input)) contract
      (principal-construct? (to-byte version) (get-address-bytes decoded) contract)
      (principal-construct? (to-byte version) (get-address-bytes decoded))
    )
  )
)

;; Example usage
(string-to-principal? "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7")
;; Returns (some SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7)

(string-to-principal? "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7.my-contract")
;; Returns (some SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7.my-contract)
```

{% endcode %}

### Description

Parse string addresses into principal types using c32 decoding in Clarity

### Use Cases

* Parsing user input addresses in contracts
* Converting stored string addresses to principals
* Validating address formats before use
* Building dynamic contract calls with string inputs

### Key Concepts

* **c32 alphabet** - `0123456789ABCDEFGHJKMNPQRSTVWXYZ` (no I, L, O, U)
* **Checksum** - Last 4 bytes verify address integrity
* **Version byte** - First character indicates address type
* **Contract addresses** - Include `.contract-name` suffix


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stacks.co/cookbook/clarity/address-utilities/convert-string-to-principal.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
