Skip to main content
Participant
March 2, 2013
Answered

How to write decimal values to hex bytearray?

  • March 2, 2013
  • 1 reply
  • 971 views

Hi,

For controlling liighting from flash I need to send binary data to a usb dongle. The pakkage that the dongle needs to receive looks lik this:

7e 01 02 06 FF 00 FF 00 ... e7

the first value is the start bit

seccond and third some length info

4th is the label

then we can send 512 bits (chanels) of 0-255 Dmx value

at the end a stop bit.

So what i want is to write different channel values to the divice. I first need to go from decimal values to hex values using the folowing code"

function decimalToHex($decimal:int, $padding:int = 2):String {

          var hex:String = Number($decimal).toString(16);

          while (hex.length < $padding)

          {

                    hex = "0" + hex;

          }

          return hex.toUpperCase();

}

var decimal:int = 163;

trace("decimal: " + decimal);

var hex:String = decimalToHex(decimal, 2);

trace("hex: " + hex);

This gives me hex values like in the example packet.

Now i want to write this to a bytearray. I found this:

var len:uint = 512;

var outputByteArr:ByteArray = new ByteArray();

outputByteArr.length = len;

for (var i:int = 0; i < len; i++) {

        outputByteArr.writeByte(hex);

}

trace(outputByteArr);

But it gives an error saying the write.Byte needs a number and not a string.

When i change it to:

  outputByteArr.writeByte(0xff);

for example it does work.

Does anybody know how to correctly change decimal values that i can put them in the Bytearray?

Thanks in advance!!

Greetings,

Piet

This topic has been closed for replies.
Correct answer kglad

you can use the writeMultiByte() method to write a string.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 2, 2013

you can use the writeMultiByte() method to write a string.

pleijstraAuthor
Participant
March 5, 2013

Thnx!

kglad
Community Expert
Community Expert
March 5, 2013

you're welcome.