Decodes the string from utf. - Node.js String

Node.js examples for String:Unicode

Description

Decodes the string from utf.

Demo Code

// Hive Colony Framework
// Copyright (C) 2008-2015 Hive Solutions Lda.
////from   w w w  .  j  a v  a  2s  .c o m
// This file is part of Hive Colony Framework.
//
// Hive Colony Framework is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Hive Colony Framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Hive Colony Framework. If not, see <http://www.gnu.org/licenses/>.

// __author__    = Jo?o Magalh?es <joamag@hive.pt>
// __version__   = 1.0.0
// __revision__  = $LastChangedRevision$
// __date__      = $LastChangedDate$
// __copyright__ = Copyright (c) 2008-2015 Hive Solutions Lda.
// __license__   = GNU General Public License (GPL), Version 3


/**
 * Decodes the string from utf.
 *
 * @return {String} The decoded string.
 */
String.prototype.decodeUtf = function() {
    var string = "";
    var index = 0;
    var character = 0;
    var character1 = 0;
    var character2 = 0;

    while (index < this.length) {
        character = this.charCodeAt(index);

        if (character < 128) {
            string += String.fromCharCode(character);
            index++;
        } else if ((character > 191) && (character < 224)) {
            character1 = this.charCodeAt(index + 1);
            string += String.fromCharCode(((character & 31) << 6)
                    | (character1 & 63));
            index += 2;
        } else {
            character1 = this.charCodeAt(index + 1);
            character2 = this.charCodeAt(index + 2);
            string += String.fromCharCode(((character & 15) << 12)
                    | ((character1 & 63) << 6) | (character2 & 63));
            index += 3;
        }
    }

    return string;
}

Related Tutorials