//---------------------------------------------------------------------
//函数:W2C
//功能:将16位wchar_t转换为 8位char[2]
//参数:w_cn为待转换的16位字符,c_cn[]为转换后的8位字符
//备注:wchar_t的高位字节应该存储在char数组的低位字节
//作者:chuchong
//---------------------------------------------------------------------
void StyleConvert::W2C(wchar_t w_cn , char c_cn[])
{
//following code convert wchar to char
c_cn[0] = w_cn >> 8 ;
c_cn[1] = (char)w_cn ;
}
//-------------------------------------------------------------------
//函数:ConvertWCHARToHex
//功能:将16位字符串转换为十六进制字符串
//参数:待转换的字符串,字符串长度
//返回值:转换后的字符串
//作者:chuchong
//-------------------------------------------------------------------
CString StyleConvert::ConvertWCHARToHex(CString Data, long nDataLength)
{
CString sResult("");
for (long nLoop=0; nLoop<nDataLength; nLoop++)
{
wchar_t ch = Data.GetAt(nLoop);
//将wchar_t转换为char[2]
char c_cn[2]={'0'};
W2C(ch,c_cn);
static const char *hex = "0123456789ABCDEF";
for(int i=0;i<2;i++)
{
unsigned char chHexA = hex[((unsigned char)(c_cn[i]) >> 4) & 0x0f];
unsigned char chHexB = hex[(unsigned char)(c_cn[i]) & 0x0f];
sResult += (char)chHexA;
sResult += (char)chHexB;
}
}
return sResult;
}
现在 我想知道的是, 如何实现这个 函数的逆功能...
5 个解决方案
#1
系统有相关的函数不需要自己来实现这样的函数
宽字符 <-> 多字符
宽字符 <-> 多字符
#2
sscanf/sprintf即可实现所需的功能
#3
楼上的 2位能告诉 我 是什么函数吗?
或者 该怎么用了?
或者 该怎么用了?
#4
自己写的两个相互转换的例子,希望可以帮到楼主:
#include<stdio.h>
#include<stdlib.h>
char *hex_to_ascii(char *dest, const char src[], int size)
{//0x12, 0x34 -> "1234"
char *ret = dest;
int i;
for (i = 0; i < size; i++)
{
*dest++ = "0123456789ABCDEF"[(unsigned char)src[i] >> 4];
*dest++ = "0123456789ABCDEF"[src[i] & 0xf];
}
*dest = 0;
return ret;
}
char *ascii_to_string(char *dest, const char src[], int size)
{//"1234" -> 0x12, 0x34
#define char_to_hex(ch) (('0' <= (ch) && (ch) <= '9') ? ((ch) & 0xf) : (((ch) & 0xf) + 9))
char *ret = dest;
int i = 0;
while (i < size)
{
char ch1 = src[i];
char ch2 = src[i + 1];
*dest++ = ((char_to_hex(ch1) << 4) | char_to_hex(ch2));
i += 2;
}
return ret;
}
int main()
{
char src[] = "十六进制字符串";
char dest[100];
char dest2[100];
printf("%s\n", hex_to_ascii(dest, src, sizeof (src) / sizeof (src[0])));
printf("%s\n", ascii_to_string(dest2, dest, sizeof (dest) / sizeof (dest[0])));
system("pause");
return 0;
}
#5
再修改下,避免对0x00做转换:
#include<stdio.h>
#include<stdlib.h>
char *hex_to_ascii(char *dest, const char src[], int size)
{//0x12, 0x34 -> "1234"
char *ret = dest;
int i;
for (i = 0; i < size; i++)
{
*dest++ = "0123456789ABCDEF"[(unsigned char)src[i] >> 4];
*dest++ = "0123456789ABCDEF"[src[i] & 0xf];
}
*dest = 0;
return ret;
}
char *ascii_to_string(char *dest, const char src[])
{//"1234" -> 0x12, 0x34
#define char_to_hex(ch) (('0' <= (ch) && (ch) <= '9') ? ((ch) & 0xf) : (((ch) & 0xf) + 9))
char *ret = dest;
while (*src)
{
char ch1 = *src++;
char ch2 = *src++;
*dest++ = ((char_to_hex(ch1) << 4) | char_to_hex(ch2));
}
return ret;
}
int main()
{
char src[] = "十六进制字符串";
char dest[100];
char dest2[100];
printf("%s\n", hex_to_ascii(dest, src, sizeof (src) / sizeof (src[0])));
printf("%s\n", ascii_to_string(dest2, dest));
system("pause");
return 0;
}
#1
系统有相关的函数不需要自己来实现这样的函数
宽字符 <-> 多字符
宽字符 <-> 多字符
#2
sscanf/sprintf即可实现所需的功能
#3
楼上的 2位能告诉 我 是什么函数吗?
或者 该怎么用了?
或者 该怎么用了?
#4
自己写的两个相互转换的例子,希望可以帮到楼主:
#include<stdio.h>
#include<stdlib.h>
char *hex_to_ascii(char *dest, const char src[], int size)
{//0x12, 0x34 -> "1234"
char *ret = dest;
int i;
for (i = 0; i < size; i++)
{
*dest++ = "0123456789ABCDEF"[(unsigned char)src[i] >> 4];
*dest++ = "0123456789ABCDEF"[src[i] & 0xf];
}
*dest = 0;
return ret;
}
char *ascii_to_string(char *dest, const char src[], int size)
{//"1234" -> 0x12, 0x34
#define char_to_hex(ch) (('0' <= (ch) && (ch) <= '9') ? ((ch) & 0xf) : (((ch) & 0xf) + 9))
char *ret = dest;
int i = 0;
while (i < size)
{
char ch1 = src[i];
char ch2 = src[i + 1];
*dest++ = ((char_to_hex(ch1) << 4) | char_to_hex(ch2));
i += 2;
}
return ret;
}
int main()
{
char src[] = "十六进制字符串";
char dest[100];
char dest2[100];
printf("%s\n", hex_to_ascii(dest, src, sizeof (src) / sizeof (src[0])));
printf("%s\n", ascii_to_string(dest2, dest, sizeof (dest) / sizeof (dest[0])));
system("pause");
return 0;
}
#5
再修改下,避免对0x00做转换:
#include<stdio.h>
#include<stdlib.h>
char *hex_to_ascii(char *dest, const char src[], int size)
{//0x12, 0x34 -> "1234"
char *ret = dest;
int i;
for (i = 0; i < size; i++)
{
*dest++ = "0123456789ABCDEF"[(unsigned char)src[i] >> 4];
*dest++ = "0123456789ABCDEF"[src[i] & 0xf];
}
*dest = 0;
return ret;
}
char *ascii_to_string(char *dest, const char src[])
{//"1234" -> 0x12, 0x34
#define char_to_hex(ch) (('0' <= (ch) && (ch) <= '9') ? ((ch) & 0xf) : (((ch) & 0xf) + 9))
char *ret = dest;
while (*src)
{
char ch1 = *src++;
char ch2 = *src++;
*dest++ = ((char_to_hex(ch1) << 4) | char_to_hex(ch2));
}
return ret;
}
int main()
{
char src[] = "十六进制字符串";
char dest[100];
char dest2[100];
printf("%s\n", hex_to_ascii(dest, src, sizeof (src) / sizeof (src[0])));
printf("%s\n", ascii_to_string(dest2, dest));
system("pause");
return 0;
}