Javascript pack unpack

is there a JavaScript function like Perls pack and unpack?
LVL 2
mattaustinAsked:
Who is Participating?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.