十六进制转字符串(如何把16进制的数据转换成字符串)

本文目录
如何把16进制的数据转换成字符串
主要有两个方法,其实都是对现有函数的使用: 方法1: sscanf() 函数名: sscanf 功 能: 从字符串格式化输入 用 法: int sscanf(char *string, char *format[,argument,...]); 以上的 format 为 %x 就是将字符串格式化为 16 进制数 例子: #include 《stdio.h》 void main() { char* p = “0x1a“; int nValude = 0; sscanf(p, “%x“, &nValude); printf(“%d\r\n“, nValude); } 输出: 26 方法2: strtol() 函数名: strtol 功 能: 将字符串转换为长整数 用 法: long strtol(char *str, char **endptr, int base); 上面的base就是我们要转换为几进制数 例子: #include 《stdio.h》 #include 《stdlib.h》 void main() { char* p = “0x1b“; char* str; long i = strtol(p, &str, 16); printf(“%d\r\n“, i); } 输出: 27 其实还有一种方法,就是用一个初始化为0~9~a~f的字符串数组,也就是一个十六进制对应表,用这个对应表即可算出一个十六进制字符串的数值,但这种发发太麻烦了,不推荐使用。
如何将16进制数字转换成字符串 java
使用这个方法可以传进去的16进制的数字组成的字符串转化为utf-8格式的字符串
public
static
String
toStringHex1(String
s)
{
byte
baKeyword
=
new
byte[s.length()
/
2];
for
(int
i
=
0;
i
《
baKeyword.length;
i++)
{
try
{
baKeyword[i]
=
(byte)
(0xff
&
Integer.parseInt(s.substring(
i
*
2,
i
*
2
+
2),
16));
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
try
{
s
=
new
String(baKeyword,
“utf-8“);//
UTF-16le:Not
}
catch
(Exception
e1)
{
e1.printStackTrace();
}
return
s;
}

更多文章:
dropdownlist 绑定(DropDownList 绑定所有项 并 显示指定项)
2026年10月11日 08:50
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00






