views:

65

answers:

1

Hi,

How do I convert a byte array into a string?

I have found these functions that do the reverse:

function string2Bin(s) {
    var b = new Array();
    var last = s.length;

    for (var i = 0; i < last; i++) {
        var d = s.charCodeAt(i);
        if (d < 128)
            b[i] = dec2Bin(d);
        else {
            var c = s.charAt(i);
            alert(c + ' is NOT an ASCII character');
            b[i] = -1;
        }
    }
    return b;
}

function dec2Bin(d) {
    var b = '';

    for (var i = 0; i < 8; i++) {
        b = (d%2) + b;
        d = Math.floor(d/2);
    }

    return b;
}

But how do I get the functions working the other way?

Thanks.

Shao

+2  A: 

You need to parse each octet back to number, and use that value to get a character, something like this:

function bin2String(array) {
  var result = "";
  for (var i = 0; i < array.length; i++) {
    result += String.fromCharCode(parseInt(array[i], 2));
  }
  return result;
}

bin2String(["01100110", "01101111", "01101111"]); // "foo"

// Using your string2Bin function to test:
bin2String(string2Bin("hello world")) === "hello world";

Edit: Yes, your current string2Bin can be written more shortly:

function string2Bin(str) {
  var result = [];
  for (var i = 0; i < str.length; i++) {
    result.push(str.charCodeAt(i).toString(2));
  }
  return result;
}

But by looking at the documentation you linked, I think that the setBytesParameter method expects that the blob array contains the decimal numbers, not a bit string, so you could write something like this:

function string2Bin(str) {
  var result = [];
  for (var i = 0; i < str.length; i++) {
    result.push(str.charCodeAt(i));
  }
  return result;
}

function bin2String(array) {
  return String.fromCharCode.apply(String, array);
}

string2Bin('foo'); // [102, 111, 111]
bin2String(string2Bin('foo')) === 'foo'; // true
CMS
Thanks for the super speedy response.Couple of questions...1) Your bin2String function is impressive - only 5 lines of code. Can the string2bin function be changed to make use of more Javascript functions to shorten the function and sub-function?.....
2) The reason I need these conversions is because I am capturing a signature and I have to convert it to populate a BLOB field in the database.Problem is, whilst these 2 functions work, something else is going wrong. The main thing is that when I retrieve a BLOB from the database it goes into a bytes array object. However, when I am writing the BLOB to the database after running it through the original function it is not a bytes array object. This might be what's causing the problem. Any ideas?
http://dcx.sybase.com/index.html#1101en/ulmbus_en11/ag-apiref-setbytesparameter.htmlThis is the syntax I use to set the data.
@shaochan: Give a look to my edit.
CMS
Hi Christian. You're an absolute genius! :) Thanks for that - it works perfectly! Many thanks!
@shaochan: I'm glad it worked for you, welcome to StackOverflow!. If an answer solved your problem, you can mark it as [*accepted*](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work). ;)
CMS