Sorry for not being clear about this question earlier. I appreciate the answers I got, but the edits I made should make the question more understandable.

I have been working with hex in Mathematica, and for some reason I can't get a list of hex strings to work properly. I have a list of hex strings - for example:

toCharachterHex[inChar_] := Return[IntegerString[ToCharacterCode[inChar], 16]];

When I call the function like this:

hexList = toCharachterHex["ajdv*3"]

Mathematica returns

{"61", "6a", "64", "76", "2a", "33"}

Now, when I type hexList[[1]], Mathematica gives me

{"97", "106", "100", "118", "42", "51"}

Why would Mathematica do this? Shouldn't hexList[[1]] give me 61?

  • 1
    $\begingroup$ Something is missing here. What exactly are hex strings? Your code as shown contains no String objects at all. $\endgroup$
    – Mr.Wizard
    Commented Sep 25, 2012 at 6:19
  • $\begingroup$ Sorry. You're right. I fixed the question. $\endgroup$
    – Sponge Bob
    Commented Sep 25, 2012 at 16:51
  • 1
    $\begingroup$ When I type hexList[[1]], it properly gives me 61. So you're still missing something. Do FullForm[hexList] and see what it gives you. $\endgroup$
    – amr
    Commented Sep 25, 2012 at 17:21

2 Answers 2


Well, you may know that they're hexadecimal numbers, but Mathematica doesn't since you didn't tell it. When you enter

hexList = {12, 10, 6b, 3f, 92}

you'll notice that the "b" and the "f" are in blue, while the numbers are black. Also notice a small space between "6" and "b", and between "3" and "f". The blue means they're undefined symbols. Mathematica sees "6b" as the expression "6 $\times$ b", no hex there.

Internally there isn't even hexadecimal, there's only numbers (binary) and symbols, and the hexadecimal only is relevant to the number's representation.

To enter hexadecimal numbers:

hexList = {16^^12, 16^^10, 16^^6b, 16^^3f, 16^^92}

which Mathematica accepts as

{18, 16, 107, 63, 146}

represented in the standard decimal form. If you want the output in hex:

BaseForm[hexList, 16]

will give you

$ \{ 12_{16}, 10_{16}, 6b_{16}, 3f_{16}, 92_{16} \} $

edit after your update of the question
My answer still stands: Mathematica doesn't store hexadecimal numbers, only binary, because that's what the microprocessor uses as well. You tell Mathematica what the input format is, but once the numbers are stored, that will be forgotten; Mathematica doesn't store information about which input format was used. When you ask the information back, Mathematica will give it to you in the default decimal format, unless otherwise specified.

  • 1
    $\begingroup$ I typed it into the stack exchange wrong. I am using BaseFom to get the list. And that is the list I am talking about in the question. $\endgroup$
    – Sponge Bob
    Commented Sep 25, 2012 at 16:39
  • $\begingroup$ @Keegan - Then please update the question. $\endgroup$
    – stevenvh
    Commented Sep 25, 2012 at 16:50
  • $\begingroup$ I just did. I'm sorry about not being clear earlier. $\endgroup$
    – Sponge Bob
    Commented Sep 25, 2012 at 16:52
  • $\begingroup$ @Keegan - nema problema. I updated my answer. $\endgroup$
    – stevenvh
    Commented Sep 25, 2012 at 17:43

The code in your question as presently shown does not produce the result that you describe. All I can figure is that you are entering input like this:

hexList = {16^^12, 16^^10, 16^^6b, 16^^3f, 16^^92};

If that is the case you need to understand that these numbers are immediately and transparently converted on parsing; this is merely an input shorthand.

List[18, 16, 107, 63, 146]

To put it another way:

HoldComplete[{16^^12, 16^^10, 16^^6b, 16^^3f, 16^^92}]
HoldComplete[{18, 16, 107, 63, 146}]

So not even HoldComplete preserves the input format.


Based on the comments under Steven's answer I believe you are doing this:

hexList = BaseForm[{97, 106, 100, 118, 42, 51}, 16];

{97, 106, 100, 118, 42, 51}

This happens because BaseForm is merely a formatting wrapper. Observe:

BaseForm[{97, 106, 100, 118, 42, 51}, 16]

The first part of that expression is the decimal list. If formatting alone is adequate you would get a better result by mapping the wrapper across the list:

hexList2 = BaseForm[#, 16] & /@ {97, 106, 100, 118, 42, 51};



You still cannot perform numeric operations on this object:

hexList2[[1]] + 15

15 + 6116

Instead you could create your own head hex with certain properties, including formatting. As a limited example (addition only):

Format[hex[n_]] := BaseForm[n, 16]
hex[n_?NumberQ] + m_?NumberQ ^:= hex[n + m]
hex[n_?NumberQ] + hex[m_?NumberQ] ^:= hex[n + m]

hexList3 = hex /@ {97, 106, 100, 118, 42, 51};

hexList3[[1]] + 15


  • $\begingroup$ Why the HoldComplete? $\endgroup$
    – stevenvh
    Commented Sep 26, 2012 at 11:09
  • $\begingroup$ @stevenvh It's just a way to demonstrate that even with the most powerful Hold function available the transformation takes place (because it's done during parsing). $\endgroup$
    – Mr.Wizard
    Commented Sep 26, 2012 at 11:12
  • $\begingroup$ OK, thanks. I took the liberty of adding that to your answer. (Rollback if you don't like it.) $\endgroup$
    – stevenvh
    Commented Sep 26, 2012 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.