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!
AnarchonCommented:
Sorry, sport, but I am WAY not a Perl kind o' guy. Tell me, et al., what pack and unpack do.
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>

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mattaustinAuthor Commented:
unpack("a4nNnN2a*",$body); is my accual perl code
archrajanCommented:
Pack and unpack make variables into bitstrings of binary data.
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>

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

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.