Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6552641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:29:05+00:00 2026-05-25T12:29:05+00:00

I’ve been asked to implement and MD5 hasher ActionScript-3 and as I was in

  • 0

I’ve been asked to implement and MD5 hasher ActionScript-3 and as I was in the middle of debugging how I formatted my input I came across a problem. When I try and output the ByteArray as a binary string using .toString(2), the toString(2) method will perform some short cuts that alter how the binary should look.
For Example

var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.writeUTFBytes("a");
bytes.writeByte(0x0);
var t1:String = bytes[0].toString(2); // is 1100001 when it should be 01100001
var t2:String = bytes[1].toString(2); // is 0 when it should be 00000000

so I guess my question is, might there a way to output a binary String from a ByteArray that will always shows each byte as a 8 bit block?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:29:06+00:00Added an answer on May 25, 2026 at 12:29 pm

    All you need is to pad the output of toString(2) with zeros on the left to make its length equal to 8. Use this function for padding

    function padString(str:String, len:int, char:String, padLeft:Boolean = true):String{
        var padLength:int = len - str.length;
        var str_padding:String = "";
        if(padLength > 0 && char.length == 1)
            for(var i:int = 0; i < padLength; i++)
                str_padding += char;
    
        return (padLeft ? str_padding : "") + str + (!padLeft ? str_padding: "");
    }
    

    With this function the code looks like this and gives the correct output

    var bytes:ByteArray = new ByteArray();
    bytes.endian = Endian.LITTLE_ENDIAN;
    bytes.writeUTFBytes("a");
    bytes.writeByte(0x0);
    var t1:String = padString(bytes[0].toString(2), 8, "0"); // is now 01100001
    var t2:String = padString(bytes[1].toString(2), 8, "0"); // is now 00000000
    

    Update
    If you want to get a string representation of complete byteArray you can use a function which iterates on the byteArray. I have wrote the following function and it seems to work correctly. Give it a try

    // String Padding function
    function padString(str:String, len:int, char:String, padLeft:Boolean = true):String{
        // get no of padding characters needed
        var padLength:int = len - str.length;
    
        // padding string
        var str_padding:String = "";
    
        // loop from 0 to no of padding characters needed
        // Note: this loop will not run if padLength is less than 1 
        // as i < padLength will be false from begining
        for(var i:int = 0; i < padLength; i++)
            str_padding += char;
    
        // return string with padding attached either to left or right depending on the padLeft Boolean
        return (padLeft ? str_padding : "") + str + (!padLeft ? str_padding: "");
    }
    
    // Return a Binary String Representation of a byte Array
    function byteArrayToBinaryString(bArray:ByteArray):String{
        // binary string to return
        var str:String = "";
    
        // store length so that it is not recomputed on every loop
        var aLen = bArray.length;
    
        // loop over all available bytes and concatenate the padded string to return string
        for(var i:int = 0; i < aLen; i++)
            str += padString(bArray[i].toString(2), 8, "0");
    
        // return binary string
        return str;
    }
    

    Now you can simply use the byteArrayToBinaryString() function like this:

    // init byte array and set Endianness
    var bytes:ByteArray = new ByteArray();
    bytes.endian = Endian.LITTLE_ENDIAN;
    
    // write some data to byte array
    bytes.writeUTFBytes("a");
    bytes.writeByte(0x0);
    
    // convert to binaryString
    var byteStr:String = byteArrayToBinaryString(bytes); // returns 0110000100000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.