3

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Sponge Bob
  • 765
  • 1
  • 8
  • 17

2 Answers2

5

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.

stevenvh
  • 6,866
  • 5
  • 41
  • 64
3

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371