{
  "category": "Text functions",
  "display_name": "CODE",
  "entry_kind": "built_in_function",
  "eval_api": {
    "method": "POST",
    "url": "https://handbook.dnacalc.com/api/eval"
  },
  "function_id": "FUNC.CODE",
  "implementation_state": {
    "answer_bank": null,
    "detail": "Recorded: 7 workbook cells.",
    "dnacalc_commit": "9e1884369a678158dd82d3d74188daf986995e87",
    "in_catalogue": true,
    "state": "matches",
    "state_label": "Matches every recorded Excel answer",
    "summary": "Matches all 7 recorded answers",
    "workbook_corpus": {
      "cells": 7,
      "matched": 7
    }
  },
  "implementations": [
    {
      "commit": "9e1884369a678158dd82d3d74188daf986995e87",
      "flavour": "excel-compatibility-candidate",
      "language": "Rust",
      "name": "CalcFunc",
      "own_code": [
        "https://github.com/DnaCalc/DnaCalc/blob/9e1884369a678158dd82d3d74188daf986995e87/crates/calc-func/src/catalogue/text_functions.rs#L248-L251",
        "https://github.com/DnaCalc/DnaCalc/blob/9e1884369a678158dd82d3d74188daf986995e87/crates/calc-func/src/ported/text_kernels.rs#L387-L400"
      ],
      "repository": "https://github.com/DnaCalc/DnaCalc",
      "shared_code": [
        "ported/text_kernels::text"
      ],
      "source": "https://github.com/DnaCalc/DnaCalc/blob/9e1884369a678158dd82d3d74188daf986995e87/crates/calc-func/src/catalogue/text_functions.rs#L248-L251",
      "state": "matches",
      "suite": null,
      "summary": "Matches all 7 recorded answers"
    }
  ],
  "json_url": "https://handbook.dnacalc.com/api/functions/FUNC.CODE.json",
  "name": "CODE",
  "notes": [],
  "schema": "efh.function-reference/v1",
  "section_order": [
    "what-it-does",
    "try-it",
    "quirks",
    "how-to-implement",
    "version-differences",
    "alternatives"
  ],
  "sections": {
    "alternatives": "\n### Relationships\n\n\n- **`CHAR`** — the inverse direction, sharing the host-character-set dependency.\n- **`UNICODE`** — the Unicode-explicit counterpart, returning the code point rather than a\n  host-encoding index. Where a formula must be portable across hosts and locales, `UNICODE` is\n  the function that means what it says.\n- **`UNICHAR`** — inverse of `UNICODE`, and thus the modern counterpart of `CHAR`.\n- Neither `CODE` nor `CHAR` is a Compatibility-category function; Microsoft has not superseded\n  either. The Unicode pair was added alongside them, not in place of them.\n\n\n",
    "how-to-implement": "\n### Notes for implementers\n\n\nThree decisions are load-bearing and none of them is settled by the documentation:\n\n1. **Which encoding.** \"The character set used by your computer\" is an instruction to be\n   host-dependent. A single-encoding implementation is making a choice; it should say which.\n2. **First character or first code unit.** These differ exactly for astral characters. An\n   implementation that reads the first UTF-16 code unit will report a lone surrogate value for\n   an emoji; one that decodes first will report either a code point above 0xFFFF (which cannot\n   be a byte-set index) or an error.\n3. **Round-tripping.** `CODE(CHAR(n)) = n` is the property users assume. It holds for whatever\n   subrange the two functions agree on, and any implementation should test the round trip\n   across the entire 1–255 range rather than on ASCII samples, because ASCII is precisely where\n   the interesting disagreements are absent.\n\n",
    "quirks": "\n### Result and edge cases\n\n\nThe return kind is `Number`, and it is a small non-negative integer whenever the call succeeds.\n\n- **Empty text.** `CODE(\"\")` has no first character, so the function has nothing to return.\n  What Excel does here is not stated in Microsoft's article text and has not been checked for\n  this Handbook.\n- **Surrogate pairs.** A character outside the Basic Multilingual Plane is two UTF-16 code\n  units. Whether Excel's `CODE` reports the leading surrogate, some replacement, or an error is\n  an open question, and it is a live one now that Microsoft has begun changing surrogate\n  handling under the Compatibility Version feature — see [`LEN`](FUNC.LEN.md), whose\n  documentation records exactly such a change.\n- Empty cells, omitted arguments and arrays follow the shared call model in\n  [chapter 03](../model/03-call-pipeline.md); nothing here is function-specific.\n\n\n\n### Errors\n\nError inputs propagate rather than convert ([chapter 02](../model/02-coercion-and-lifting.md)).\n\n\n",
    "sources": "- Microsoft, \"CODE function\" —\n  <https://support.microsoft.com/en-us/office/code-function-c32b692b-2ed0-4a04-bdd9-75640144b928>\n  (first-character rule; the \"character set used by your computer\" statement; the\n  Macintosh/Windows-ANSI split). Retrieved for this page.\n- Microsoft, \"LEN, LENB functions\" — cited here only for the Compatibility Version surrogate\n  change that makes the surrogate question live:\n  <https://support.microsoft.com/en-us/office/len-function-29236f94-cedc-429d-affd-b5e33d2c67cb>\n- Handbook `content/model/02-coercion-and-lifting.md` (to-text conversion, error propagation)\n  and `content/model/03-call-pipeline.md` (argument preparation).\n\n\n",
    "version-differences": "No comparison between specific Excel builds is recorded here yet. This does not establish that all builds behave alike.",
    "what-it-does": "\n`CODE` takes a text value, looks at its **first character only**, and returns the numeric code\nof that character in the host's character set. Microsoft's article says exactly that the\nreturned code \"corresponds to the character set used by your computer\", and names the Macintosh\ncharacter set on Macintosh and ANSI on Windows.\n\nEverything after the first character is discarded. `CODE` is therefore not a hash, not a\nchecksum and not a length-sensitive function: `CODE(\"A\")` and `CODE(\"Alphabet\")` are the same\ncall as far as the result is concerned.\n\nThe function is the intended inverse of `CHAR`, and inherits the same open question in the same\nplace: for characters inside ASCII every candidate encoding agrees, and above 127 they do not.\nSee [`CHAR`](FUNC.CHAR.md) for the statement of that question; it is one question, not two.\n\n\n\n### Arguments\n\n\n`text` — required, the text whose first character is to be coded. The argument is a text\nargument, so the ordinary to-text conversion applies: a number arrives as its General-formatted\nrendering and a logical as `TRUE` or `FALSE`, which means `CODE` of a number is the code of the\nfirst *digit character* of that number's rendering, and `CODE(TRUE)` is the code of `T`. This\nis the argument position readers most often misread — the function looks numeric and behaves\ntextually.\n\nThere is no second argument, no index, and no way to ask for the code of the *n*-th character;\ncompose with `MID` for that.\n\n\n"
  },
  "signature": {
    "display": "CODE(text)",
    "parameters": [
      {
        "description": null,
        "name": "text",
        "optional": false,
        "repeats": false
      }
    ],
    "trailing_repeats": false
  },
  "sources": {
    "metadata": {
      "admission": {
        "label": "implemented",
        "reason": null
      },
      "arity": {
        "max": 1,
        "min": 1
      },
      "category": "Text functions",
      "classification": {
        "arg_preparation_profile": "ValuesOnlyPreAdapter",
        "coercion_lift_profile": "None",
        "determinism_class": "Deterministic",
        "error_collapse_profile": "None",
        "fec_dependency_profile": "None",
        "host_interaction_class": "None",
        "kernel_signature_class": "Custom",
        "rich_value_usage": "RichBlind",
        "surface_fec_dependency_profile": "RefOnly",
        "thread_safety_class": "SafePure",
        "volatility_class": "NonVolatile"
      },
      "descriptions": {
        "long": null,
        "short": "Code of the first character of a text in the system character set",
        "short_source": "dna-calc-own"
      },
      "docs": {
        "english_description": "Returns a numeric code for the first character in a text string",
        "microsoft_url": "https://support.microsoft.com/en-us/office/code-function-c32b692b-2ed0-4a04-bdd9-75640144b928"
      },
      "entry_kind": "built_in_function",
      "localized_family_names": null,
      "localized_names": [
        {
          "locale": "ar-SA",
          "name": "CODE"
        },
        {
          "locale": "bg-BG",
          "name": "CODE"
        },
        {
          "locale": "cs-CZ",
          "name": "KÓD"
        },
        {
          "locale": "da-DK",
          "name": "KODE"
        },
        {
          "locale": "de-DE",
          "name": "CODE"
        },
        {
          "locale": "el-GR",
          "name": "CODE"
        },
        {
          "locale": "en-AU",
          "name": "CODE"
        },
        {
          "locale": "en-GB",
          "name": "CODE"
        },
        {
          "locale": "en-US",
          "name": "CODE"
        },
        {
          "locale": "es-ES",
          "name": "CÓDIGO"
        },
        {
          "locale": "et-EE",
          "name": "CODE"
        },
        {
          "locale": "fi-FI",
          "name": "KOODI"
        },
        {
          "locale": "fr-FR",
          "name": "CODE"
        },
        {
          "locale": "he-IL",
          "name": "CODE"
        },
        {
          "locale": "hr-HR",
          "name": "CODE"
        },
        {
          "locale": "hu-HU",
          "name": "KÓD"
        },
        {
          "locale": "id-ID",
          "name": "CODE"
        },
        {
          "locale": "it-IT",
          "name": "CODICE"
        },
        {
          "locale": "ja-JP",
          "name": "CODE"
        },
        {
          "locale": "ko-KR",
          "name": "CODE"
        },
        {
          "locale": "lt-LT",
          "name": "CODE"
        },
        {
          "locale": "lv-LV",
          "name": "CODE"
        },
        {
          "locale": "nb-NO",
          "name": "KODE"
        },
        {
          "locale": "nl-NL",
          "name": "CODE"
        },
        {
          "locale": "pl-PL",
          "name": "KOD"
        },
        {
          "locale": "pt-BR",
          "name": "CÓDIGO"
        },
        {
          "locale": "pt-PT",
          "name": "CÓDIGO"
        },
        {
          "locale": "ro-RO",
          "name": "CODE"
        },
        {
          "locale": "ru-RU",
          "name": "КОД"
        },
        {
          "locale": "sk-SK",
          "name": "CODE"
        },
        {
          "locale": "sl-SI",
          "name": "CODE"
        },
        {
          "locale": "sr-Latn-RS",
          "name": "KÔD"
        },
        {
          "locale": "sv-SE",
          "name": "KOD"
        },
        {
          "locale": "th-TH",
          "name": "CODE"
        },
        {
          "locale": "tr-TR",
          "name": "KOD"
        },
        {
          "locale": "uk-UA",
          "name": "CODE"
        },
        {
          "locale": "vi-VN",
          "name": "CODE"
        },
        {
          "locale": "zh-CN",
          "name": "CODE"
        },
        {
          "locale": "zh-HK",
          "name": "CODE"
        },
        {
          "locale": "zh-TW",
          "name": "CODE"
        }
      ],
      "signature": {
        "display": "CODE(text)",
        "parameters": [
          {
            "description": null,
            "name": "text",
            "optional": false,
            "repeats": false
          }
        ],
        "trailing_repeats": false
      },
      "surface_name": "CODE",
      "version_marker": null,
      "xlcall": {
        "code": "121",
        "symbol": "xlfCode"
      }
    },
    "microsoft": "https://support.microsoft.com/en-us/office/code-function-c32b692b-2ed0-4a04-bdd9-75640144b928"
  },
  "try_it": {
    "formula": "=CODE(\"A\")"
  },
  "url": "https://handbook.dnacalc.com/functions/CODE.html",
  "variants": [],
  "versions": []
}