当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


p5.js unchar()用法及代码示例


p5.j​​s中的unchar()函数用于将单字符字符串转换为其相应的整数表示形式。该函数与char()函数相反。

用法:

unchar(value)

参数:此函数接受单个参数值,该值将转换为其相应的整数表示形式。该值将是任何单字符字符串或单字符数组。


返回值:它返回转换后的单字符字符串的整数表示形式。

以下示例程序旨在说明p5.js中的unchar()函数:

范例1:本示例使用unchar()函数将单字符字符串转换为其相应的整数表示形式。

function setup() {  
   
    // Creating Canvas size 
    createCanvas(600, 230);  
}  
   
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing some values 
    let Value1 = "A"; 
    let Value2 = "a"; 
    let Value3 = "B"; 
    let Value4 = "C";  
    let Value5 = "z"; 
     
    // Calling to unchar() function. 
    let A = unchar(Value1); 
    let B = unchar(Value2); 
    let C = unchar(Value3); 
    let D = unchar(Value4); 
    let E = unchar(Value5); 
     
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting integer representation 
    text("Integer representation of alphabet A is: " + A, 50, 30); 
    text("Integer representation of alphabet a is: " + B, 50, 60); 
    text("Integer representation of alphabet B is: " + C, 50, 90); 
    text("Integer representation of alphabet C is: " + D, 50, 110); 
    text("Integer representation of alphabet z is: " + E, 50, 140); 
} 

输出:

范例2:本示例使用unchar()函数将单字符字符串转换为其相应的整数表示形式。

function setup() {  
   
    // Creating Canvas size 
    createCanvas(600, 200);  
}  
   
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Calling to unchar() function. 
    let A = unchar("A"); 
    let B = unchar("b"); 
    let C = unchar("y"); 
    let D = unchar("Y"); 
    let E = unchar("P"); 
     
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting integer representation 
    text("Integer representation of alphabet A is: " + A, 50, 30); 
    text("Integer representation of alphabet b is: " + B, 50, 60); 
    text("Integer representation of alphabet y is: " + C, 50, 90); 
    text("Integer representation of alphabet Y is: " + D, 50, 110); 
    text("Integer representation of alphabet P is: " + E, 50, 140); 
}    

输出:

参考: https://p5js.org/reference/#/p5/unchar



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 p5.js | unchar() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。