3

Is possible to convert double byte integer to single byte using java script ?? I need to convert double byte numbers as single byte,when it is entered into a text box.Is there any method available in jQuery or javascript.

3
  • What do you mean by "convert double byte numbers as single byte..." Leaving aside that JavaScript's numbers are all floating point (that doesn't mean you can't do integer math), if you did that, surely you'd have the problem that double byte (16-bit) integers can store values in the range 0..65,535 (or -32,768..32,767), whereas single byte (8-bit) integers can only store values in the range 0..255 (or -128..127). Commented Mar 12, 2013 at 11:16
  • I want to convert 1234 to 1234,The first is doublebyte Commented Mar 12, 2013 at 11:18
  • @ shn: Digits, you mean? As in numeric characters? Commented Mar 12, 2013 at 11:19

2 Answers 2

4

There's no built-in way to convert 1 (U+FF11) to 1 (U+0031). You could use a regular expression to do it specifically for the characters you mentioned:

var rex = /[\uFF10-\uFF19]/g;
var str = "1234";

console.log("Before: " + str);  
str = str.replace(rex, function(ch) {
    return String.fromCharCode(ch.charCodeAt(0) - 65248);
});

console.log("After: " + str);

Live Example (be sure to open the JavaScript console) | Source

Sign up to request clarification or add additional context in comments.

1 Comment

See my answer on how to do it.
0

Why can't you convert it? 65296 is your magic number, the tables never change so just run up and down it and you can convert numbers this way.

This will convert single byte to double byte and back again.

Example requires Jquery but you could write it without quite easy.

<input type="text" id="one" value="1234567890"><br/>
<input type="text" id="two"><br/>
<input type="text" id="three">

<script>
    symbolsToEntities=function(sText){
        var sNewText = "";
        var iLen = sText.length;
        for (i=0; i<iLen; i++){
        iCode = sText.charCodeAt(i);
            sNewText += (iCode > 256? "&#" + iCode + ";": sText.charAt(i));
        };
        return sNewText;
    };

    ConvertNumber=function(num){
        var O='';
        for (var i = 0, len = num.length; i < len; i++){
            O+='&#'+(parseInt(num[i])+65296)+';';
        };
        return O;
    };

    ConvertNumberagain=function(num){
        var O='';
        var res = num.split(';');
        $.each(res, function( key, value ){
            if (value.length!=0){
                O+=parseInt(value.replace('&#',''))-65296;
            };
        });
        return O;
    };
    var doublebyte=ConvertNumber($('#one').val());
    doublebyte = $('<div>').html(doublebyte).text();
    $('#two').val(doublebyte);
    var singlebyte=symbolsToEntities($('#two').val());
    singlebyte=ConvertNumberagain(singlebyte);
    $('#three').val(singlebyte);
</script>

Live example: http://jsfiddle.net/399EM/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.