title: How can I access the nth byte of a binary scalar in Perl?
body: <p>Thanks to everyone in advance.</p>
  
  <p>I'd like to access the nth byte of a binary scalar. For example you could get all the file data in one scalar variable...</p>
  
  <p>Imagine that the binary data is collected into scalar... </p>
  
  <pre><code>open(SOURCE, "&lt;", "wl.jpg"); 
  my $thisByteData = undef; 
  while(&lt;SOURCE&gt;){$thisByteData .= $_;} 
  close SOURCE; 
  </code></pre>
  
  <p>$thisByteData is raw binary data. When I use length($thisByteData) I get the byte count back, so Perl does know how big it is. My question is how can I access the Nth byte?</p>
  
  <p>Side note: My function is going to receive this binary scalar, its in my function that I want to access the Nth byte. The help regarding how to collect this data is appreciated but not what I'm looking for. Whichever way the other programmer wants to collect the binary data is up to them, my job is to get the Nth byte when its passed to me :)</p>
  
  <p>Again thanks so much for the help to all!</p>
  
  <hr>
  
  <p>Thanks to @muteW who has gotten me further than ever. I guess I'm not understanding unpack(...) correctly.</p>
  
  <pre><code>print(unpack("N1", $thisByteData));
  print(unpack("x N1", $thisByteData));
  print(unpack("x0 N1", $thisByteData));
  </code></pre>
  
  <p>Is returning the following:</p>
  
  <pre><code>4292411360
  3640647680
  4292411360
  </code></pre>
  
  <p>I would assume those 3 lines would all access the same (first) byte. Using no "x" just an "x" and "x$pos" is giving unexpected results.</p>
  
  <p>I also tried this...</p>
  
  <pre><code>print(unpack("x0 N1", $thisByteData));
  print(unpack("x1 N1", $thisByteData));
  print(unpack("x2 N1", $thisByteData));
  </code></pre>
  
  <p>Which returns... the same thing as the last test...</p>
  
  <pre><code>4292411360
  3640647680
  4292411360
  </code></pre>
  
  <p>I'm definatly missing something about how unpack works.</p>
  
  <hr>
  
  <p>If I do this...</p>
  
  <pre><code>print(oct("0x". unpack("x0 H2", $thisByteData)));
  print(oct("0x". unpack("x1 H2", $thisByteData)));
  print(oct("0x". unpack("x2 H2", $thisByteData)));
  </code></pre>
  
  <p>I get what I was expecting...</p>
  
  <pre><code>255
  216
  255
  </code></pre>
  
  <p>Can't unpack give this to me itself without having to use oct()?</p>
  
  <hr>
  
  <p>As a side note: I think I'm  getting the 2's complement of these byte integers when using "x$pos N1". I'm expecting these as the first 3 bytes.</p>
  
  <pre><code>255
  216
  255
  </code></pre>
  
  <p>Thanks again for the help to all.</p>
  
  <hr>
  
  <p>Special thanks to @brian d foy and @muteW ... I now know how to access the Nth byte of my binary scalar using unpack(...). I have a new problem to solve now, which isn't related to this question. Again thanks for all the help guys!</p>
  
  <p>This gave me the desired result...</p>
  
  <pre><code>print(unpack("x0 C1", $thisByteData));
  print(unpack("x1 C1", $thisByteData));
  print(unpack("x2 C1", $thisByteData));
  </code></pre>
  
  <p>unpack(...) has a ton of options so I recommend that anyone else who reads this read the pack/unpack documentation to get the byte data result of their choice. I also didn't try using the Tie options @brian mentioned, I wanted to keep the code as simple as possible.</p>