[removed] Need functions to convert a string containing binary to hex, then convert back to binary

前端 未结 4 884
醉梦人生
醉梦人生 2020-11-28 11:40

Lets say I have a string in JavaScript with binary data in it. It may look like this:

var binary = \'00001000010001000101010100001110\';

I

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 12:22

    Well I found an algorithm here which helped explain how to do it. Also this page on Wikipedia helped confirming the 4-bit binary to hexadecimal mappings. I came up with the following code to do it. Other code snippets I found on the web didn't work at all. Let me know if you would make any improvements. You could probably even do a straight lookup table really using that info from Wikipedia which would be faster.

    var tools = {
        /**
         * Converts binary code to hexadecimal string
         * @param {string} binaryString A string containing binary numbers e.g. '01001101'
         * @return {string} A string containing the hexadecimal numbers
         */
        convertBinaryToHexadecimal: function(binaryString)
        {
            var output = '';
    
            // For every 4 bits in the binary string
            for (var i=0; i < binaryString.length; i+=4)
            {
                // Grab a chunk of 4 bits
                var bytes = binaryString.substr(i, 4);
    
                // Convert to decimal then hexadecimal
                var decimal = parseInt(bytes, 2);
                var hex = decimal.toString(16);
    
                // Uppercase all the letters and append to output
                output += hex.toUpperCase();
            }
    
            return output;      
        },
    
        /**
         * Converts hexadecimal code to binary code
         * @param {string} A string containing single digit hexadecimal numbers
         * @return {string} A string containing binary numbers
         */
        convertHexadecimalToBinary: function(hexString)
        {
            var output = '';
    
            // For each hexadecimal character
            for (var i=0; i < hexString.length; i++)
            {
                // Convert to decimal
                var decimal = parseInt(hexString.charAt(i), 16);
    
                // Convert to binary and add 0s onto the left as necessary to make up to 4 bits
                var binary = this.leftPadding(decimal.toString(2), '0', 4);
    
                // Append to string         
                output += binary;
            }
    
            return output;
        },
    
        /**
         * Left pad a string with a certain character to a total number of characters
         * @param {string} inputString The string to be padded
         * @param {string} padCharacter The character that the string should be padded with
         * @param {string} totalCharacters The length of string that's required
         * @returns {string} A string with characters appended to the front of it
         */
        leftPadding: function(inputString, padCharacter, totalCharacters)
        {
            // If the string is already the right length, just return it
            if (!inputString || !padCharacter || inputString.length >= totalCharacters)
            {
                return inputString;
            }
    
            // Work out how many extra characters we need to add to the string
            var charsToAdd = (totalCharacters - inputString.length)/padCharacter.length;
    
            // Add padding onto the string
            for (var i = 0; i < charsToAdd; i++)
            {
                inputString = padCharacter + inputString;
            }
    
            return inputString;
        }
    };
    

提交回复
热议问题