如何在JavaScript中获取数字的ASCII码?

时间:2015-10-31 19:44:05

标签: javascript ascii

我知道name.charCodeAt(0)。我发布了以下代码,所以我想在下面找到解决方案。

var number= 2;
var t = number.charCodeAt(0);
console.log(t);

答案需要是ASCII格式。我得到的日志为2而不是ASCII值50.可能是什么问题?

3 个答案:

答案 0 :(得分:2)

ASCII 是一种表示字符串数据的方式。您需要先将 Number 转换为 String ;

还要记住 String 可以有任意长度,因此你需要遍历每个角色。

var x = 2,
    str_x = '' + x,
    chrs = Array.prototype.map.call(str_x, function (e) {return e.charCodeAt(0);});
chrs; // [50]

最后, JavaScript 适用于 UTF-16 / UCS-2 ,而不是普通的 ASCII 。幸运的是,数字的值在两者中是相同的,因此您不必在此进行任何进一步的转换。

答案 1 :(得分:0)

您必须首先将数字转换为字符串才能使用10-31 13:01:52.122 2542-2542/? I/art: Not late-enabling -Xcheck:jni (already on) 10-31 13:01:52.253 2542-2542/? I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.hardware.fingerprint.FingerprintManagerCompatApi23$1> 10-31 13:01:52.253 2542-2542/? I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.hardware.fingerprint.FingerprintManagerCompatApi23$1> 10-31 13:01:52.360 2542-2542/? W/art: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 10-31 13:01:52.516 2542-2558/? D/OpenGLRenderer: Render dirty regions requested: true 10-31 13:01:52.517 2542-2542/? D/: HostConnection::get() New Host Connection established 0xa66e01b0, tid 2542 10-31 13:01:52.522 2542-2542/? D/Atlas: Validating map... 10-31 13:01:52.569 2542-2558/? D/: HostConnection::get() New Host Connection established 0xa66e0260, tid 2558 10-31 13:01:52.578 2542-2558/? I/OpenGLRenderer: Initialized EGL, version 1.4 10-31 13:01:52.592 2542-2558/? D/OpenGLRenderer: Enabling debug mode 0 10-31 13:01:52.602 2542-2558/? W/EGL_emulation: eglSurfaceAttrib not implemented 10-31 13:01:52.602 2542-2558/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa66b4b20, error=EGL_SUCCESS 来获取数字字符代码。

.charCodeAt

答案 2 :(得分:0)

只需将数字转换为String并在其上调用方法。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define g 9.81

float MaxH(float Vi, float theta);
float ToF(float Vi, float theta);
float range(float Vi, float theta);

int main()
{
    float H=0.00, t=0.00, R=0.00, Vi, theta;

    printf("Enter initial velocity in m/s\n");
    scanf("%f", &Vi);

    printf("Enter angle Theta\n");
    scanf("%f", &theta);

    float MaxH(float Vi, float theta);
    printf("The maximum projectile height is %3.2fm\n", H);

    float ToF(float Vi, float theta);
    printf("The time of flight is %3.2fs\n ", t);

    float range(float Vi, float theta);
    printf("The horizontal range of projectile is %3.2fm\n", R);

    return 0;
}


float MaxH(float Vi, float theta)
{
    float H;

    H = (Vi*Vi*sin(theta)*sin(theta))/(2*g);

    return H;
}

float ToF(float Vi, float theta)
{
    float t;

    t = (2*Vi*sin(theta))/g;

    return t;
}

float range(float Vi, float theta)
{
    float R;

    R = (Vi*Vi*sin(2*theta))/g;

    return R;
}