當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。