C語言字符轉換ASCII碼


//函 數 名:CharToHex()
//功能描述:把ASCII字符轉換為16進制
//函數說明:
//調用函數:
//全局變量:
//輸    入:ASCII字符
//返    回:16進制
/////////////////////////////////////////////////////////////////////
unsigned char CharToHex(unsigned char bHex){
    if((bHex>=0)&&(bHex<=9))
        bHex += 0x30;
    else if((bHex>=10)&&(bHex<=15))//大寫字母
        bHex += 0x37;
    else bHex = 0xff;
    return bHex;
}
/////////////////////////////////////////////////////////////////////
//函 數 名:HexToChar()
//功能描述:把16進制轉換為ASCII字符
//函數說明:
//調用函數:
//全局變量:
//輸    入:16進制
//返    回:ASCII字符
/////////////////////////////////////////////////////////////////////
unsigned char HexToChar(unsigned char bChar){
    if((bChar>=0x30)&&(bChar<=0x39))
        bChar -= 0x30;
    else if((bChar>=0x41)&&(bChar<=0x46))//大寫字母
        bChar -= 0x37;
    else if((bChar>=0x61)&&(bChar<=0x66))//小寫字母
        bChar -= 0x57;
    else bChar = 0xff;
    return bChar;

 

字符轉ASCII碼,ASCII碼轉字符

public static int Asc(string character)
  {
   if (character.Length == 1)
   {
    System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
    int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
    return (intAsciiCode);
   }
   else
   {
    throw new Exception("Character is not valid.");
   }

  }

ASCII碼轉字符:

public static string Chr(int asciiCode)
  {
   if (asciiCode >= 0 && asciiCode <= 255)
   {
    System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
    byte[] byteArray = new byte[] { (byte)asciiCode };
    string strCharacter = asciiEncoding.GetString(byteArray);
    return (strCharacter);
   }
   else
   {
    throw new Exception("ASCII Code is not valid.");
   }
  } 

 
 

 JS中把字符轉成ASCII值的函數示例代碼

 

<script>
str="A";
code = str.charCodeAt();
str2 = String.fromCharCode(code);
str3 = String.fromCharCode(0x60+26); document.write(code+'<br />');
document.write(str2+'<br />');
document.write(str3);
</script>

 

一、將字符串轉換成ASCII碼

格式: Asc(x)

功能:返回字符串x中第一個字符的ASCII碼。

說明:

◆ x是一個字符串型數據,函數值返回一個。

例如:

x=Asc''0''                                    'x的值為48

x=Asc''ABC''                                 'x的值為65

二、將ASCII碼轉換成字符

格式:Chr(x)

功能:將ASCII碼值轉換成相應的字符。

說明:

◆ x是一個ASCII碼代碼值,函數值返回一個字符。

例如:

x=Chr(66)                                  'x的值為字符''B''

 

 

toascii將字符轉換為ASCII碼

toascii將字符轉換為ASCII碼

函數原型:inttoascii(intc);
頭文件:消ncludectype.h
是否是標准函數:是
函數功能:將c轉化為相應的ASCII碼。
返回值:返回轉換后的數值,也就是轉換后的ASCII碼。
應用toascii函數將整型數字轉換為相應的ASCII碼。
#includectype.hmain()
ints[]={l,2,3,4,5,6};inti;
for(i=0;i6;i++)
{
printf(-%d-,s[i]);
putchar(toascii(s[i]));
例程說明:
(1)首先在整型數組中存入146個整型數字,並將其顯示在終端屏幕上。
(2)循環地將數組中的每個數字轉換為其對應的ASCII碼,並將其以字符的形式顯示在終端屏幕上。本例程的運行結果為:
123456.
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM