急救!如何把字符串转成16进制

liq1979 2005-05-06 04:36:15
string d = "2005-05-01";如何把字符串d,转换成16进制,请给出具体代码!谢谢。100分
...全文
280 5 打赏 收藏 转发到动态 举报
写回复
用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);

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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