0

I have to convert a string to byte (16 bit) in JavaScript. I can do this in .net in following code but I have to change this for old classic asp App which uses JavaScript.

string strShared_Key = "6fc2e550abc4ea333395346123456789";
int nLength = strShared_Key.Length;
byte[] keyMAC = new byte[nLength / 2];
for (int i = 0; i < nLength; i += 2)
    keyMAC[i / 2] = Convert.ToByte(strShared_Key.Substring(i, 2), 16);

This is the JavaScript function but doesn't return same out put as above .net code.

function String2Bin16bit(inputString) {
        var str = ""; // string 
        var arr = [];       // byte array 
        for (var i = 0; i < inputString.length; i += 2) {
            // get chunk of two characters and parse to number 
            arr.push(parseInt(inputString.substr(i, 2), 16));
        }
        return arr;
    }
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
user228777
  • 3,082
  • 9
  • 33
  • 50
  • possible duplicate of [Javascript/HTML - converting byte array to string](http://stackoverflow.com/questions/3195865/javascript-html-converting-byte-array-to-string) Question/Solution has both ways of doing it. – epascarello Sep 20 '11 at 11:59
  • epascarello - this example doesn not work for 16 bit. – user228777 Sep 20 '11 at 15:28

1 Answers1

1

You want parseInt(x, 16) which will read x as a number and parse it as such bearing in mind that it's in base 16.

var str = "aabbcc"; // string
var arr = [];       // byte array
for(var i = 0; i < str.length; i += 2) {
    arr.push(parseInt(str.substr(i, 2), 16)); // get chunk of two characters and parse to number
}
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • I tried writing following function but did not output the exact value what I get in .net function. function String2Bin16bit(inputString) { //var str = "aabbcc"; // string var arr = []; // byte array for (var i = 0; i < inputString.length; i += 2) { // get chunk of two characters and parse to number arr[i / 2] = arr.push(parseInt(inputString.substr(i, 2), 16)); } return arr; } – user228777 Sep 20 '11 at 13:17
  • Is that JavaScript? You're using `arr[i / 2] = arr.push`, which does not make sense. `arr.push` simply adds the value to the array, so no need for indices. – pimvdb Sep 20 '11 at 13:53
  • Yes it is javascript, still results are not same like .net function. I changed to function String2Bin16bit(inputString) { var str = ""; // string var arr = []; // byte array for (var i = 0; i < inputString.length; i += 2) { // get chunk of two characters and parse to number arr.push(parseInt(inputString.substr(i, 2), 16)); // str = str + arr.push(parseInt(inputString.substr(i, 2), 16)); } return arr; } – user228777 Sep 20 '11 at 14:04
  • @user228777: What about posting it in your question, since it's inscrutable without formatting in comments please. – pimvdb Sep 20 '11 at 14:05
  • I added javascript function in my original post now. – user228777 Sep 20 '11 at 14:41
  • 1
    @user228777: It works fine for me. What differences are there in the results? – pimvdb Sep 20 '11 at 14:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3657/discussion-between-user228777-and-pimvdb) – user228777 Sep 21 '11 at 10:55
  • Eventually I have to write above javascript function ( calculating 16 bit ) in this format: function str2binl(str) { var bin = Array(); var mask = (1 << chrsz) - 1; for(var i = 0; i < str.length * chrsz; i += chrsz) bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); return bin; } – user228777 Sep 21 '11 at 11:01