A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: help with .writeBytes

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2

    help with .writeBytes

    hello,

    i am trying to send Binary data to a socket using flash..

    we have created a device that understands raw binary, although i am having trouble sending it.

    i have a variable chanel_text.text defines what binary code to send when the submit button is pressed. i am getting

    Actionscript Code:
    "Scene 1, Layer 'as', Frame 1, Line 52  1067: Implicit coercion of a value of type String to an unrelated type flash.utils:ByteArray."

    i presume this is because the ****** in s.writeBytes(*******) is not to the format flash expects.

    Actionscript Code:
    function submit(event:MouseEvent):void {
    if (channel_text.text == "1"){
    s.writeBytes("0000011");}
    if (channel_text.text == "2"){
    s.writeBytes("0010011");}
    if (channel_text.text == "3"){
    s.writeBytes("0100011");}
    if (channel_text.text == "4"){
    s.writeBytes("1000011");}
    }

    what i am asking is if i want to send the 7bit binary 0000011 what do i write?

    Kind regards
    -Luke

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here is one way to code and decode a 7 Bit bytearray. I don't know that is the best way to do it.

    import flash.utils.ByteArray;

    var bArray:Array = [0,0,0,0,0,1,1];
    var bytes:ByteArray = new ByteArray();
    for(var i:int=0; i<bArray.length; i++)
    {
    bytes.writeByte(bArray[i]);
    }
    var bb:ByteArray = new ByteArray();
    bb.writeBytes(bytes);
    for(var j:int=0; j<bb.length; j++)
    {
    bb.position = j;
    trace(bb.readByte());
    }
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2

    Smile

    Thankyou, this is extremely useful and works well with my current if statement.

    Actionscript Code:
    function submit(event:MouseEvent):void {
    if (channel_text.text == "1"){
    var bArray:Array = [0,0,0,0,0,1,1];
    }
    if (channel_text.text == "2"){
    var bArray:Array = [0,0,1,0,0,1,1];
    }
    if (channel_text.text == "3"){
    var bArray:Array = [0,1,1,0,0,1,1];
    }
    if (channel_text.text == "4"){
    var bArray:Array = [1,1,1,0,0,1,1];
    }

    var bytes:ByteArray = new ByteArray();
    for(var i:int=0; i<bArray.length; i++)
    {
    bytes.writeByte(bArray[i]);
    }
    var bb:ByteArray = new ByteArray();
    bb.writeBytes(bytes);
    for(var j:int=0; j<bb.length; j++)
    {
    bb.position = j;
    trace(bb.readByte());

    Thankyou

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center