字符串與16進制Unicode編碼之間的相互轉換(C#蔬啡,Objective-C餐禁,MFC)

c#

  public static String deUnicode(String content)
        {//每4位16進制Unicode編碼轉為一個字符
            String enUnicode = null;
            String deUnicode = null;
            for (int i = 0; i < content.Length; i++)
            {
            
                    enUnicode += content[i];
       
                if (i % 4 == 3)
                {

                    deUnicode += (char)(Convert.ToInt32(enUnicode, 16));

                    
                    enUnicode = null;
                }

            }
            return deUnicode;
        }
        public static String enUnicode(String content)
        {//將字符串的每個字符轉換為4位16進制編碼
            String enUnicode = null;
            for (int i = 0; i < content.Length; i++)
            {
                if (i == 0)
                {
                    int value = Convert.ToInt32(content[i]);
                    enUnicode = getHexString(String.Format("{0:X}", value));
                }
                else
                {
                    int value = Convert.ToInt32(content[i]);
                    enUnicode = enUnicode + getHexString(String.Format("{0:X}", value));
                }
            }
            return enUnicode;
        }
        private static String getHexString(String hexString)
        {
            String hexStr = "";
            for (int i = hexString.Length; i < 4; i++)
            {
                if (i == hexString.Length)
                    hexStr = "0";
                else
                    hexStr = hexStr + "0";
            }
            return hexStr + hexString;
        }

Objective-C

+ (NSString *)hexStringFromString:(NSString *)string
{
  //字符串每個字符轉4位16進制Unicode編碼
   NSString *newHexStr;
   NSMutableString *enUnicode;
   NSMutableString *tempHexStr;
    for (int i=0; i<string.length; i++) {
        int value=[string characterAtIndex:i];
        if(value>65536||value<0)
        {
            return nil;
        }
         newHexStr = [NSString stringWithFormat:@"%x",value];//16進制數
        
        if(newHexStr.length<4)
        {
            NSString *zero=@"0000";
           tempHexStr=[NSMutableString stringWithString:zero];
            
            [tempHexStr replaceCharactersInRange:NSMakeRange(4-newHexStr.length, newHexStr.length) withString:newHexStr];
        }
        else
            tempHexStr=[NSMutableString stringWithString:newHexStr];
        if(i==0)
            enUnicode=[NSMutableString stringWithString:tempHexStr];
        else
        [enUnicode appendString:tempHexStr];
    }
    //NSLog(@"%@",[enUnicode uppercaseString]);
    return [enUnicode uppercaseString];
}



+ (NSString *)stringFromHexString:(NSString *)hexString
{
   //每4位16進制Unicode編碼轉為一個字符
    if(hexString.length%4!=0||hexString==nil)
       return nil;
    NSString *enUnicode;
    NSMutableString *deUnicode;
    for(NSInteger i=0;i<(hexString.length/4);i++)
        {   unsigned int anInt;
            enUnicode=[hexString substringWithRange:NSMakeRange(i*4,4)];
            NSScanner * scanner = [[NSScanner alloc] initWithString:enUnicode];
            [scanner scanHexInt:&anInt];
            enUnicode=[NSString stringWithFormat:@"%C",(unichar)anInt];
            if(i==0)
                deUnicode=[NSMutableString stringWithString:enUnicode];
            else
                [deUnicode appendString:enUnicode];
         }
        
    return deUnicode;
}

C++

CString HexStringConvert::hexStringFromString(CString string)
{      //字符串每個字符轉4位16進制Unicode編碼
    CString newHexStr;
    CString tempStr;
    CString enUnicode;
    for (int i = 0; i < string.GetLength(); i++)
    {
        int value = string.GetAt(i);
        if (value>65536 || value < 0)
            return NULL;
        newHexStr.Format(_T("%x"), value);
        if (newHexStr.GetLength() < 4)
        {
            tempStr = _T("0000");
            for (int j = 0; j < newHexStr.GetLength(); j++)
            {
                tempStr.SetAt((4+j-newHexStr.GetLength()), newHexStr.GetAt(j));
            }
        }
        else
            tempStr = newHexStr;
        enUnicode = enUnicode + tempStr;

    }
    return enUnicode.MakeUpper();
    
}
char* tranformStr(CString st)
{      
    int nLength = st.GetLength();
    int nBytes = WideCharToMultiByte(CP_ACP, 0, st, nLength, NULL, 0, NULL, NULL);
    char* path1 = new char[nBytes + 1];
    memset(path1, 0, nLength + 1);
    WideCharToMultiByte(CP_OEMCP, 0, st, nLength, path1, nBytes, NULL, NULL);
    path1[nBytes] = 0;
    return path1;
}
CString HexStringConvert::StringFromhexString(CString hexStr)
{
    //毎4位16進制Unicode編碼轉字符串
    if (hexStr.GetLength() % 4 != 0 || hexStr.IsEmpty())
    {
        return NULL;
    }
    CString enUnicode;
    CString deUnicode;
    for (int i = 0; i <hexStr.GetLength()/4 ; i++)
    {
        enUnicode = hexStr.Mid(i * 4, 4);
        int value;
        const char* tempChar = tranformStr(enUnicode);
        sscanf_s(tempChar, "%x", &value);
        if (value>65536 || value < 0)
            return NULL;
        deUnicode += (wchar_t)value;
    }
    return deUnicode;
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市跑芳,隨后出現的幾起案子轴总,更是在濱河造成了極大的恐慌,老刑警劉巖博个,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件怀樟,死亡現場離奇詭異,居然都是意外死亡盆佣,警方通過查閱死者的電腦和手機往堡,發(fā)現死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來共耍,“玉大人虑灰,你說我怎么就攤上這事”远担” “怎么了瘩缆?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長佃蚜。 經常有香客問我,道長着绊,這世上最難降的妖魔是什么谐算? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮归露,結果婚禮上洲脂,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好恐锦,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布往果。 她就那樣靜靜地躺著,像睡著了一般一铅。 火紅的嫁衣襯著肌膚如雪陕贮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天潘飘,我揣著相機與錄音肮之,去河邊找鬼。 笑死卜录,一個胖子當著我的面吹牛戈擒,可吹牛的內容都是我干的。 我是一名探鬼主播艰毒,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼筐高,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了丑瞧?” 一聲冷哼從身側響起柑土,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嗦篱,沒想到半個月后冰单,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡灸促,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年诫欠,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浴栽。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡荒叼,死狀恐怖,靈堂內的尸體忽然破棺而出典鸡,到底是詐尸還是另有隱情被廓,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布萝玷,位于F島的核電站嫁乘,受9級特大地震影響,放射性物質發(fā)生泄漏球碉。R本人自食惡果不足惜蜓斧,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望睁冬。 院中可真熱鬧挎春,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至脚线,卻和暖如春搁胆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背殉挽。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工丰涉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人斯碌。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓一死,卻偏偏與公主長得像,于是被迫代替她去往敵國和親傻唾。 傳聞我的和親對象是個殘疾皇子投慈,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內容