hex2dec — Convert hexadecimal text to decimal numbers in MATLAB and RunMat.
hex2dec(hexStr) converts hexadecimal character vectors, character matrices, string arrays, or cell arrays of text to decimal double values. Character matrix rows and cell text entries produce a column vector; string arrays preserve their shape.
Syntax
hex2dec(hexStr)How hex2dec works
- Character vectors and string scalars produce a scalar double result.
- Each row of a character matrix is converted independently.
- String arrays preserve their input shape in the numeric output.
- Cell arrays of character vectors or strings are converted element-by-element and returned as a column vector.
- Optional
0xor0Xprefixes are accepted. - Unsigned suffixes
u8,u16,u32, andu64are accepted when the digits fit in the requested width. - Signed suffixes
s8,s16,s32, ands64interpret the digits as two's-complement signed values. - Results that exceed RunMat's exact integer range are rejected instead of silently losing precision.
- Hexadecimal letters may be uppercase or lowercase.
Examples
Convert a hexadecimal character vector
d = hex2dec('3FF')Expected output:
d = 1023Convert a string array and preserve its shape
d = hex2dec(["3FF" "7A" "E"])Expected output:
d = [1023 122 14]Accept a hexadecimal prefix
d = hex2dec('0xFF')Expected output:
d = 255Interpret a signed suffix
d = hex2dec('0xFFs8')Expected output:
d = -1Using hex2dec with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how hex2dec changes the result.
Run a small hex2dec example, explain the result, then change one input and compare the output.
FAQ
Can hex2dec parse prefixed hexadecimal literals?⌄
Yes. RunMat accepts 0x and 0X prefixes.
What do signed suffixes mean?⌄
A suffix such as s8 treats the parsed digits as a signed two's-complement integer with that width.
How does RunMat handle very large hexadecimal values?⌄
RunMat returns double values only when the result is exactly representable; larger results raise an error instead of returning rounded values.
Related Comms functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how hex2dec is executed, line by line, in Rust.
- View the source for hex2dec in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.