社区
C#
帖子详情
急救!如何把字符串转成16进制
liq1979
2005-05-06 04:36:15
string d = "2005-05-01";如何把字符串d,转换成16进制,请给出具体代码!谢谢。100分
...全文
280
5
打赏
收藏
急救!如何把字符串转成16进制
string d = "2005-05-01";如何把字符串d,转换成16进制,请给出具体代码!谢谢。100分
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
louisqiang
2005-05-06
打赏
举报
回复
在java中有一个专门的工具可以转换,当然转换后可以在.net下使用,对汉字一样有用。
工具名native2ascii.exe
dazhu2
2005-05-06
打赏
举报
回复
Convert.tostring(要转化的数,16);
wskyo
2005-05-06
打赏
举报
回复
两个方法,你选择
using System;
using System.Text;
namespace Encoding_Examples
{
using System;
using System.Text;
class EncodingExample
{
public static void Main()
{
string ddd = "JMDZ SZ2";
string d = "",d1 = "";
d = StrToHex(ddd);
byte[] b1 = System.Text.Encoding.Default.GetBytes(ddd);
for(int i = 0;i<b1.Length;i++)
{
d1 += b1[i].ToString("X");
}
string e = ToHexString(b1);
if(d==e)
{
Console.WriteLine("OK");
}
else
{
Console.WriteLine("No");
}
Console.WriteLine(d);
}
static string StrToHex(string str)
{
string strTemp = "";
byte[] bTemp = System.Text.Encoding.Default.GetBytes(str);
for(int i = 0;i<bTemp.Length;i++)
{
strTemp += bTemp[i].ToString("X");
}
return strTemp;
}
public static string ByteToStr(string bStr)
{
string ret = "";
if(bStr == "")
return ret;
byte[] w = System.Convert.FromBase64String(bStr);
ret = System.Text.Encoding.UTF8.GetString(w,0,w.Length);
return ret;
}
public static string AtoX(string asc)
{
int nLines;
int nChars;
int offset;
string hex = "";
// Compute number of hex lines.
if((asc.Length % 16) > 0)
nLines = asc.Length/16+1;
else
nLines = asc.Length/16;
// Convert into hex lines.
for(int i = 0; i < nLines; i++)
{
offset = i * 16;
if((asc.Length - offset) > 16)
nChars = 16;
else
nChars = asc.Length - offset;
hex += HexLine(i, asc.Substring(offset, nChars));
}
return hex;
}
private static string HexLine(int lNum, string asc)
{
string hex = "";
// Copy line to char buffer.
char[] c = new char[16];
asc.CopyTo(0, c, 0, asc.Length);
// Create offset prefix.
hex += String.Format("{0:X8} - {1:X8}", lNum*16, (lNum+1)*16-1);
hex += " ";
// Convert chars to hex representation.
for(int i = 0; i < asc.Length; i++)
{
if((i != 0) && ((i % 4) == 0))
hex += " ";
hex += String.Format("{0:X2}", (int) c[i]);
}
// Add padding.
int nSpaces = 62 - hex.Length;
for(int i = 0; i < nSpaces; i++)
hex += " ";
// Add ASCII to end of line.
for(int i = 0; i < asc.Length; i++)
{
if(((int) c[i] < 32) || ((int) c[i] > 126))
hex += ".";
else
hex += c[i].ToString();
}
// Return hex dump line.
return hex;
}
static char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static string ToHexString(byte[] bytes)
{
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
}
}
LoveCherry
2005-05-06
打赏
举报
回复
你要转ubicode还是什么?这样的转化没有意义
新鲜鱼排
2005-05-06
打赏
举报
回复
string s0 = Console.ReadLine();
int i = Convert.ToInt32(s0, 16);
MySQL5 权威指南第3版中文版_part1
15.5
字符串
、日期、时间、BLOB和NULL 15.6 向关联数据表插入新数据记录 15.7 处理来自HTML表单的输入数据 15.8 分页显示查询结果 15.9 处理层次化数据 15.10 速度优化 15.11 Unicode 15.12 二进制...
MySQL 5权威指南(第3版) 中文版 下载地址
15.5
字符串
、日期、时间、BLOB和NULL 15.6 向关联数据表插入新数据记录 15.7 处理来自HTML表单的输入数据 15.8 分页显示查询结果 15.9 处理层次化数据 15.10 速度优化 15.11 Unicode 15.12 ...
Python实现DES加密
Python实现DES加密 实现功能:输入明文和密钥即可输出密文 所有加密相关的表是直接体现在代码的,也可以改用读取csv等方法导入 实现代码 def plaintext(): M = input("请输入十六进制数明文:") M = list(bin(int(M,...
js正则表达式
正则表达式是对
字符串
操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则
字符串
”,这个“规则
字符串
”用来表达对
字符串
的一种过滤逻辑。 正则表达式通常用来检索、替换...
C#
110,538
社区成员
642,577
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章