Javascript pack unpack

is there a JavaScript function like Perls pack and unpack?
LVL 2
mattaustinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

archrajanCommented:
There isnt any similar to that in javascript!
0
AnarchonCommented:
Sorry, sport, but I am WAY not a Perl kind o' guy. Tell me, et al., what pack and unpack do.
0
ZvonkoSystems architectCommented:
You have to tell us your reqirments, and then we can help you.

E.g.:
<script>
function packBin(theNum){
  return theNum.toString(2)
}

alert(packBin(123));
</script>

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mattaustinAuthor Commented:
unpack("a4nNnN2a*",$body); is my accual perl code
0
archrajanCommented:
Pack and unpack make variables into bitstrings of binary data.
0
ZvonkoSystems architectCommented:
Or this:

<script>
function packBin(theNum){
  return theNum.toString(2)
}
function unpackBin(theString){
  return parseInt(theString,2)
}

alert(packBin(123));
alert(unpackBin(packBin(123)));


</script>

0
ZvonkoSystems architectCommented:
Anything with formatstrings will become very complicated, because there are NO formating functions in JavaScript and you have to build them by hand.
0
ZvonkoSystems architectCommented:
Sorry, not build, you have to parse the formatstring individualy. Your upper output format will become hundred of lines of i-then-elses.
0
AnarchonCommented:
These will do text to/from hex packing/unpacking. May provide you the model for whatever it is you have in mind or at least provide more of a target for your question. I confess to remaining a bit fuzzy 'bout your need.

      function pack(txt) {
         // convert text to hexadecimal (string/byte-pairs) equivalents
         var last = txt.length;
         var bytes = Array(last);
         var hexPack = "";
         for (var i = 0; i < last; i++) {
            bytes[i] = txt.charCodeAt(i);
            hexPack += decToHex(bytes[i]);
         }
         return hexPack;
      }

      function unpack(hexPack) {
         // convert hexadecimal (string/byte-pairs) to text equivalents
         var last = hexPack.length/2;
         var bytes = Array(last);
         var txt = "";
         var code = 0;
         var low = "";
         var high = "";
         for (var i = 0; i < last; i++) {
            low = hexPack.charAt(1 + 2*i);
            high = hexPack.charAt(2*i);
            code = hexToDec(high + low);
            txt += String.fromCharCode(code);
         }
         return txt;
      }

      function decToHex(dec) {
         var hexChars = "0123456789ABCDEF";
         var low = dec % 16;
         var high = (dec - low)/16;
         hex = "" + hexChars.charAt(high) + hexChars.charAt(low);
         return hex;
      }

      function hexToDec(hex) {
         var hexChars = "0123456789ABCDEF";
         var low = hexChars.indexOf(hex.charAt(1));
         var high = 16*hexChars.indexOf(hex.charAt(0));
         hex = low + high;
         return hex;
      }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.