# hexList[[1]] returns a list of decimal numbers

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?

-
Something is missing here. What exactly are hex strings? Your code as shown contains no String objects at all. – Mr.Wizard Sep 25 '12 at 6:19
Sorry. You're right. I fixed the question. – Sponge Bob Sep 25 '12 at 16:51
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. – amr Sep 25 '12 at 17:21

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.

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.

-
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. – Sponge Bob Sep 25 '12 at 16:39
@Keegan - Then please update the question. – stevenvh Sep 25 '12 at 16:50
I just did. I'm sorry about not being clear earlier. – Sponge Bob Sep 25 '12 at 16:52
@Keegan - nema problema. I updated my answer. – stevenvh Sep 25 '12 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.

FullForm[hexList]

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.

## Update

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

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

hexList[[1]]

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


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

InputForm[hexList]

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};

hexList2[[1]]


6116

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


7016

-
Why the HoldComplete? – stevenvh Sep 26 '12 at 11:09
@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). – Mr.Wizard Sep 26 '12 at 11:12
OK, thanks. I took the liberty of adding that to your answer. (Rollback if you don't like it.) – stevenvh Sep 26 '12 at 11:21