Since a hexadecimal escape sequence can have a variable number of hex digits, the string literal "\x123" contains a single character with hex value 123. To create a string containing the two characters with hex values 0012 and 0003, respectively, one could write "\x00120003" or "\x0012" + "\x0003" instead.
解决方案 »
- 关于值类型的转换和实现借口
- C#中私有变量的应用
- C#项目中,怎么使组合框的下拉值随着另一个组合框当前的Text改变而改变?
- EXE的权限问题
- 复制
- .net 2005 如何添加 .ocx 控件
- Infragistics中的WinForm的UltraGrid控件能否自动显示行序号?
- DataTable还有以前ADO一定的Filter的功能吗?
- winfrom当前文本框清空【用递归】
- 如何代码实现将一个listBox1框中的多选内容项,移到listBox2框中
- 我用readline遍历文本时,每回总是少了第行的头个字母
- SqlDataAdapter的Fill(dataSet,startRecord,maxRecords,tableName)方法能否得到这次查询的总条数
首先,C#中的char是双字节长,和C中就不一样。
其次,string类型是字符串*常量*不能更改,能操作内存也没用。
再次,在C#中根本没必要对内存操作。
举个例子:
如果想到得到字符串中的每个字符,String类有个Chars属性,可以得到,但为只读,不可写。
也可用string的ToCharArray()方法得到一个字符数组。
如果到从字符数组生成string,就要用System.Text下的StringBuilder类,用Append()方法,将字符加到StringBuilder上,最后调用ToString()方法得到string。不知道楼主想要做什么,可否说得清楚一点儿呢?
所以输入参数szSrc= "80101080";实际上我就是要把它认为是一个一个的二进制数。
我想转换为:
szDest的内存显示为:
0x80 0x10 0x10 0x80。
就这样。
C++中是这么写的:
BYTE asciitohex(char t)
{
char buf[17] = "0123456789ABCDEF";
for(int i=0;i<16;i++)
{
if(buf[i]==t )
return i;
} return 0;
}int main(int argc, char* argv[])
{
printf("Hello World!\n"); char szBuf[9] = "F01010F4";
char szBuf2[9] = "80101080";
int len = strlen(szBuf);
BYTE temp;
for(int i=0;i<len/2;i++)
{
temp = asciitohex(szBuf[i*2])*16
+ asciitohex(szBuf[i*2+1]);
szBuf2[i]=(char)temp;
}
szBuf2[i] = '\0'; printf("%s", szBuf2); return 0;
}您运行一下这个C++程序即可,察看一下szBuf2的内存表示。这一切C#如何做?
private byte asciitohex(char t)
{
char[] buf = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
for(int i=0;i<16;i++)
{
if(buf[i]==t )
return (byte)i;
} return 0;
} // 函数:转换ascii字符串为二进制
// 比如,输入“80101080”字符串,我们认为是十六进制的
// 那么输出字符串的内存处写:0x80 0x10 0x10 0x80
private void a2h(string szSrc, char[] szDest)
{
int nLen = szSrc.Length;
byte temp;
int i = 0;
for(i = 0;i < nLen / 2;i++)
{
temp = (byte)(asciitohex(szSrc[i*2]) * 16 );
temp = (byte)(temp + asciitohex(szSrc[i*2+1]));
szDest[i] = (char)temp;
}
szDest[i] = '\0';
}
也差不多。只不过,对于80及其以上的十六进制,转换出来后,szDest内存中显示却是3F。好像是受了char的限制,每个字符最大不能超过63。
我给你实现一个。
private byte[] a2h(string s)
{
byte[] bs = new bs[s.Length/2];
for(int i = 0 ; i < bs.Count ; ++i)
{
string t = s.Substring(i * 2, 2);
bs[i] = byte.Parse(t, System.Globalization.NumberStyles.HexNumber);
}
return bs;
}两点建议:
C#的char相当于C++的w_char,而byte相当于char。
.net 和 C++ 不一样,随便怎么 new 都无所谓。楼主看看行不行,不行我们再讨论。
我觉得没问题了。
虽然没什么用了,但写出来仅供参考。
private byte asciitohex(char t)
{
if(char.IsDigit(t))
{
return (byte)(t - '0');
}
else
{
t = char.ToUpper(t);
if(t >= 'A' && t <= 'F')
{
return (byte)(t - 'A' + 10);
}
}
return (byte)-1;
}
在c#中不可能实现象c中的直接通过操作类型(如char等)来强转换,但是
在.net中是不可能的,因为.net不是直接工作在os平台上,而是通过clr托管的,因此
如果楼主想实现10进制和二进制或者16进制互换(应该是这个目的吗?),那么要自己写独立方法,这里同意zhengyun_ustc(郑昀)的方法,ps;我以前做过一份,要的话发mail给我
[email protected]
wish u good luck
Greatsft
我直接这么做:
int discarded;
byte[] byteArray = Utility.HexEncoding.GetBytes(Content, out discarded);
就可以实现把Content的字符串转换为byte[],但是当我把byteArray强制转成char时(因为我要把这个byte[]往后面传递,那个参数必须是string的),当给赋进去0x80或者0x90等,真实的内存却是0x3f。这就是我想问的。
你们可以试试看:
char sz = '\x80';
string szDest = new string(sz);
看看这个szDest的内存,就会发现不是你认为的80,而是3f。
我不明白的地方在这里。所以我认为是不是有其他方式。如果解决不了,我就不在C#上费劲了,直接把逻辑后移到C++中得了。下面是那个cs的做法,和 milkbb(李宇杰)说的差不多:
http://www.codeproject.com/csharp/hexencoding.asp
Converting Hexadecimal String to/from Byte Array in C#
By neilck
e.g. string “01FFA0” is equivalent to byte[] { 1, 255, 160 } /// <summary>
/// Creates a byte array from the hexadecimal string. Each two characters are combined
/// to create one byte. First two hexadecimal characters become first byte in returned array.
/// Non-hexadecimal characters are ignored.
/// </summary>
/// <param name="hexString">string to convert to byte array</param>
/// <param name="discarded">number of characters in string ignored</param>
/// <returns>byte array, in the same left-to-right order as the hexString</returns>
public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
string newString = "";
char c;
// remove all none A-F, 0-9, characters
for (int i=0; i<hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
newString += c;
else
discarded++;
}
// if odd number of characters, discard last character
if (newString.Length % 2 != 0)
{
discarded++;
newString = newString.Substring(0, newString.Length-1);
} int byteLength = newString.Length / 2;
byte[] bytes = new byte[byteLength];
string hex;
int j = 0;
for (int i=0; i<bytes.Length; i++)
{
hex = new String(new Char[] {newString[j], newString[j+1]});
bytes[i] = HexToByte(hex);
j = j+2;
}
return bytes;
} /// <summary>
/// Converts 1 or 2 character string into equivalant byte value
/// </summary>
/// <param name="hex">1 or 2 character string</param>
/// <returns>byte</returns>
private static byte HexToByte(string hex)
{
if (hex.Length > 2 || hex.Length <= 0)
throw new ArgumentException("hex must be 1 or 2 characters in length");
byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
return newByte;
}
例如 ASCII 值 为0x8c的字符, 定义成char时, 被转换成十六进制的整数0xff8c 。 这是因当 ASCII码值大于0x80时, 该字节的最高位为1, 计算机会认为该数为负数, 对于 0x8c表示的数实际上是-74(8c的各位取反再加1), 而-74 转换成两字节整型数并 在计算机中表示时就是0xff8c( 对0074 各位取反再加1) 。 因此只有定义为 unsigned char 0x8c转换成整型数时才是8c。这一点在处理大于0x80的ASCII码 字符时(例如汉字码)要特别注意。一般汉字均定义为unsigned char(在以后的程 序中会经常碰到)。