Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

Create a Byte Array in adobe live cycle designer form

Avatar

Former Community Member

I need to convert a string to a byte array.

for example ,

if var str="string I need to convert";

var bytArray= new ByteArray(str.length);

The above code , doesn't work .

Please note , that it doesn't throw any scripting errors but still doesn't work.

Please help me how do I proceed..........

1 Reply

Avatar

Level 6

Perhaps you are looking for following code.....

try {
var str="string I need to convert";
//xfa.host.messageBox(""+stringToBytes(str));
var bytArray = stringToBytes(str).split(",");
} catch (e) {
xfa.host.messageBox(""+e)
}

function stringToBytes(str) {
  var ch, st, re = [];
  for (var i = 0; i < str.length; i++ ) {
    ch = str.charCodeAt(i);  // get char 
    st = [];                 // set up "stack"
    do {
      st.push( ch & 0xFF );  // push byte to stack
      ch = ch >> 8;          // shift value down by 1 byte
    }  
    while ( ch );
    // add stack contents to result
    // done because chars have "wrong" endianness
    re = re.concat( st.reverse() );
  }
  // return an array of bytes
  return re;
}

Good Luck,