0

The Problem
Only HEX values of 10 through ff work. I can not assign 00 through 0f as keys; they get dropped from it seems. My understanding is that these values would require quotes to work, but that doesn't seem to be the case. I even read this article and it didn't help. How can I assign these HEX values as array keys?

Background
What I am trying to do is create a substitution array that holds HEX values that will be used to substitute other HEX values. Confusing? Let me show you:

This HEX string comes in (Text that was changed to HEX): 2a2b

My goal is then to use the substitution array to change this into another hex string like so: 1fa9

The fastest way I can think to do this is to assign HEX values to the array keys this way I can look up the key that matches the hex value and substitute it right away for its value like so:

// I'm working with chunks of 25 fyi
for (var i = 0; i < 25; i++) {
  dataChunck[i] = substitueArray[dataChunck[i]];
}

The Rest of My Code
This has been cut down to just what you need. hexValues is an array of HEX values 00 through ff.

// Start at array index 1 because be defined and saved 0 already
for (var x = 1; x < 256; x++) {
  if (counter > 255) {
    break; // We now have all 256 HEX values, stop
  }
  // We want to allow 256 as an option so go 1 higher (257)
  if (sub > 0 && sub < 257) {
    // If the number (HEX value) is not already saved save it
    if (sboxValues.indexOf(hexValues[sub - 1]) < 0) {
      // RIGHT HERE IS THE PROBLEM!!!
      // This assigns a HEX value as the key and saves sub to it
      // sub as you can see gets converted to HEX as its saved
      sboxValues[hexValues[x]] = hexValues[sub - 1];
      counter++;
    } else {
      // Glitch: That number has been saved, redo this round
      // Keep x on track so we don't accidently reuse it
      x--;
    }
    sub += num; // Move on to new number
  } else {
    // Removed. You don't need to see this code
    // Keeps x on track so we don't accidently reuse it
  }
}

1 Answer 1

2

What you are trying to do is have string keys in an array. That is not possible.

What you could do instead is use a plain object as a map (instead of an array) or else just convert the hex strings you would like to use as keys into their decimal representations with parseInt, e.g.:

sboxValues[parseInt(hexValues[x], 16)] = hexValues[sub - 1];
Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking of going the decimal route as well. I am not strong in Javascript though, PHP is more my thing. So the code you put is saving a decimal key of the HEX value correct?
@Blizzardengle: Yes, but the best way to know is to try it ;-)
Of course. Thanks so much for this answer. I was originally testing changing whole chunks of my code to manage decimals instead of HEX and never thought of doing it so simply. This saved me so much work.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.