Skip to main content
Glama

utils_convertData

Converts data between utf8, hex, base64, and binary formats for use in blockchain, encryption, or file processing. Requires source and target encoding parameters.

Instructions

Converts data between different encodings (utf8, hex, base64, binary). Useful for transforming data formats when working with blockchain data, encryption, or file processing.

Parameters:

  • data (required): The string to convert

  • from (required): Source encoding format (utf8, hex, base64, or binary)

  • to (required): Target encoding format (utf8, hex, base64, or binary)

Example usage:

  • UTF-8 to hex: {"data": "hello world", "from": "utf8", "to": "hex"} → 68656c6c6f20776f726c64

  • UTF-8 to base64: {"data": "Hello World", "from": "utf8", "to": "base64"} → SGVsbG8gV29ybGQ=

  • base64 to UTF-8: {"data": "SGVsbG8gV29ybGQ=", "from": "base64", "to": "utf8"} → Hello World

  • hex to base64: {"data": "68656c6c6f20776f726c64", "from": "hex", "to": "base64"} → aGVsbG8gd29ybGQ=

Notes:

  • All parameters are required

  • The tool returns the converted data as a string

  • For binary conversion, data is represented as an array of byte values

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
argsYes

Implementation Reference

  • Registration of the MCP tool 'utils_convertData', including detailed description, Zod input schema, and a wrapper async handler that calls the core convertData function and formats the response.
    server.tool( "utils_convertData", "Converts data between different encodings (utf8, hex, base64, binary). Useful for transforming data formats when working with blockchain data, encryption, or file processing.\n\n" + "Parameters:\n" + "- data (required): The string to convert\n" + "- from (required): Source encoding format (utf8, hex, base64, or binary)\n" + "- to (required): Target encoding format (utf8, hex, base64, or binary)\n\n" + "Example usage:\n" + '- UTF-8 to hex: {"data": "hello world", "from": "utf8", "to": "hex"} → 68656c6c6f20776f726c64\n' + '- UTF-8 to base64: {"data": "Hello World", "from": "utf8", "to": "base64"} → SGVsbG8gV29ybGQ=\n' + '- base64 to UTF-8: {"data": "SGVsbG8gV29ybGQ=", "from": "base64", "to": "utf8"} → Hello World\n' + '- hex to base64: {"data": "68656c6c6f20776f726c64", "from": "hex", "to": "base64"} → aGVsbG8gd29ybGQ=\n\n' + "Notes:\n" + "- All parameters are required\n" + "- The tool returns the converted data as a string\n" + "- For binary conversion, data is represented as an array of byte values", { args: z.object({ data: z.string().describe("The data string to be converted"), from: encodingSchema.describe( "Source encoding format (utf8, hex, base64, or binary)", ), to: encodingSchema.describe( "Target encoding format to convert to (utf8, hex, base64, or binary)", ), }), }, async ({ args }) => { try { const result = convertData({ data: args.data, from: args.from, to: args.to, }); return { content: [ { type: "text", text: result, }, ], }; } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); return { content: [ { type: "text", text: `Error: ${msg}`, }, ], isError: true, }; } }, );
  • Core handler function for data conversion between encodings (utf8, hex, base64, binary) using BSV SDK utilities. Validates inputs, converts to/from byte array, and outputs in target format.
    export function convertData({ data, from, to, }: { data: string; from: "hex" | "base64" | "utf8" | "binary"; to: "hex" | "base64" | "utf8" | "binary"; }): string { encodingSchema.parse(from); encodingSchema.parse(to); let arr: number[]; if (from === "binary") { try { arr = JSON.parse(data); if (!Array.isArray(arr) || !arr.every((n) => typeof n === "number")) { throw new Error(); } } catch { throw new Error("Invalid binary input: must be a JSON array of numbers"); } } else { arr = bsvToArray(data, from); } if (to === "binary") { return JSON.stringify(arr); } if (to === "hex") { return bsvToHex(arr); } if (to === "base64") { return bsvToBase64(arr); } if (to === "utf8") { return bsvToUTF8(arr); } throw new Error("Invalid 'to' encoding"); }
  • Zod schema for encoding types used in convertData validation.
    const encodingSchema = z.enum(["utf8", "hex", "base64", "binary"]);
  • Zod schema for encoding types, reused in tool registration.
    const encodingSchema = z.enum(["utf8", "hex", "base64", "binary"]);

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/b-open-io/bsv-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server