print hexadecimal representation of 16 bit word

I keep messing it up
I've got a 16 bit word that's supposed to be the hexadecimal representation of a number.
How do I print that hexadecimal representation?

Answers (1)

Somewhat dependent upon the context, but some options include
V=65535;
fprintf('%s\n',dec2hex(V)) % convert to internal string
FFFF
fprintf('%X\n',V) % use builtin format conversion
FFFF
If this doesn't resolve whatever issues you're having, show us the context of the problem and what you're trying to do, specifically.

4 Comments

I found out what my problem was. I was printing it out to a .csv file and view it in Excel. Excel saw the leading zero and put it into scientific notation. I had to print some kind of character besides 0 as the leading character. The recommendation I got was a single quotation mark. I tried inserting a NULL character or a space to keep there from being any additional characters in the string, but that didn't work, so a single quotation it will have to be, unless someone out there has a better idea.
Excel doesn't know anything about base other than decimal so you have only a couple of alternatives depending upon what you want/need to do with the values.
If you want it to be displayed as a hex number with the leading zeros to fill out a fixed number of digits, then displaying it as a text value is only way; interpreting the input field as text is what the leading single quote character does.
If you need to be able to compute inside the spreadsheet, then you'll have to either accept the decimal representation of the hex value as the value or convert the text with =HEX2DEC() analogous to hex2dec() in MATLAB. Both of them will then convert the result to a double for internal calculation.
Thanks for the help, you've been great. I'll have to get with the engineers and see how they want to handle it.
The alternative would be to store =DEC2HEX(value) in the cell of interest(*), but you might be forced to actually write to Excel directly for it to recognize and interpret/evaluate the formula on opening a .csv file; I've not ever tried that. I write formulas all the time, but use writecell with the 'UseExcel',1 parameter.
(*) But, that also converts that cell to text so to calculate with it, formulas referencing the cell will have to do the conversion back as well just as if stored the explicit text representation.

Sign in to comment.

Categories

Products

Release

R2024a

Asked:

on 10 Nov 2025

Commented:

dpb
on 10 Nov 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!