字符串与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; i65536||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 65536 || value < 0)
            return NULL;
        deUnicode += (wchar_t)value;
    }
    return deUnicode;
    
}

你可能感兴趣的:(字符串与16进制Unicode编码之间的相互转换(C#,Objective-C,MFC))