如何将char数组转换成16进制字符串

hpvcms 2008-11-30 01:50:53
char array[]="藽";

希望将array转换成"CC 43"
该怎么转换?
...全文
2801 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hpvcms 2008-12-26
  • 打赏
  • 举报
回复
unsigned char : 0~255
char :-128 ~ 127
-------------------------------------------------------------
unsigned char与char的区别
C51初期 /xmm1981 发表于2007-09-24, 18:35
Character values of type unsigned char have a range from 0 to 0xFF hexadecimal. A signed char has range 0x80 to 0x7F. These ranges translate to 0 to 255 decimal, and –128 to +127 decimal, respectively. The /J compiler option changes the default from signed to unsigned.

char 是无符号的
unsigned char 是无符号的,里面全是正数

两者都作为字符用的话是没有区别的,但当整数用时有区别:
char 整数范围为-128到127( 0x80__0x7F),
而unsigned char 整数范围为0到255( 0__0xFF )

多数情况下,char ,signed char 、unsigned char 类型的数据具有相同的特性然而当你把一个单字节的数赋给一个大整型数域时,便会看到它们在符号扩展上的差异。另一个区别表现在当把一个介于128和255之间的数赋给signed char 变量时编译器必须先进行数值转化,同样还会出现警告。若使用十六进制进行赋值使用unsigned char 要方便一些.
根据编译器具体实现情况不同,char要么和signed char等同,要么和unsigned char等同.unsigned char*跟char *是一样的。

功能:统计字符串里面的汉字的个数 (gb2312编码内码大于0xa0)

char szText[]= "12345你好";

l= strlen(szText);
int sum=0;
for (int i=0; i< l; i++)
if (szText[i] > 0xa0)
sum++;
sum/=2;

这样你根本统计出到任何汉字,
因为char是有符号的,打最大就是127,超过就变成复数了。比如7f 是127,那么80就是-1了。
这时候你一定要写成
unsigned char szText[]= "12345你好";

-------------------------------------------------------------
Basically, unsigned char is 8 bit value and char 7 bit value with 1 sign bit (of cause, every body knows this ^_^).

Depending on application, they can be inter-changeable or completely different. The basic rules are
1. they can be assigned to each other without change the value;
2. the sign bit will be used if you assign them to signed data types.

For example:
char c;
uchar uc;
...
uc = c; //the bit map of uc and c are the same.
...
c = uc; //same.
c = 255;
uc = c;
int i = uc, j = c; //i is 255, j is -1.
usigned int ui1 = uc, ui2 = c; //ui1 = 255, ui2 = 0xffffffff;
float f1 = uc, f2 = c; //f1 is 255, f2 is -1.
-------------------------------------------------------------
just to make one thing clear:

char != signed char

'char' could be either 'unsigned' or 'signed', depending on your compiler.

this uncertaincy in c and/or c++ standards is due to some historical reasons.
-------------------------------------------------------------
业界大多数情况char 默认为有符号,unsigned char默认为无符号

当int,short,long没有标明signed or unsigned时编译器认为他们是signed的
但是 char编译器不指定默认是unsigned 或者是signed
具体请见标准,估计是可移植性的问题

此外关于unsigned char和BYTE的关系
通常,注意仅仅是通常我们使用unsigned char 来实现byte,但是鉴于某些特殊架构上BYTE可能不是8BITS(例如可能是9BITS)所以unsigned char只能作为某些情况下的替代品

<Imperfect C++>里面有详细的关于“13.1. May I Have a byte? ””给我一个字节“的讨论,十分有意思
具体可以参考此书,这里只写一下结论:
说到底,C++并不提供所谓的真正一个BYTE的数据类型,只不过unsigned char 被视为了通常的替代品

<Imperfect C++>里的选段:
Unfortunately, there is no byte type in C/C++, and so the common practice in such cases is to use char. This makes sense, since the size of char is always one byte. The (C++-98) standard does not say this directly, but it does say (C++-98: 5.3.3) that "the sizeof operator yields the number of bytes in the object representation of its operand" and "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1." Clearly, then, sizeof(char) == sizeof(byte) must always be true. (Note that a byte is not necessarily 8 bits, just that it is "large enough to fit the basic character set" [C++-98: 3.9.1.1].

很不幸,C++没有提供BYTE类型,所以通常情况下我们使用char来替代。这个是有道理的,因为通常char的大小是一个字节。C++98标准没有直接提到这个,但是在5.3.3里说到:“sizeof操作符获得的是对象需要的内存字节数,而sizeof(char)==sizeof(signed char)==sizeof(unsigned char)==1”,很明显sizeof(char) == sizeof(byte)总应该是对的。
(注意一个自己不总是8比特,只需要它足够大,达到能够存下基本的字符集)[C++-98: 3.9.1.1].
crushor 2008-11-30
  • 打赏
  • 举报
回复
顺序是颠倒的,因为...


#include "stdafx.h"
#include <iostream>
#include <strstream>

using namespace std;

int main(int argc, char* argv[])
{
char array[]="藽";
strstream ss;
ss<<hex<<*(short*)array<<endl;
char buff[10] = {0};
ss>>buff;
cout<<buff<<endl;
return 0;
}
barech 2008-11-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ztz0223 的回复:]
这里有一个编码的问题,不是说能转就能转的问题呵呵
汉字编码理解否?
[/Quote]

如兄台所说,如果要将char数组进行转换,自然是可以的
但是转换出来的东西,你起码得有个对应的字符表
就呆在云上 2008-11-30
  • 打赏
  • 举报
回复
这里有一个编码的问题,不是说能转就能转的问题呵呵
汉字编码理解否?

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧