在c#中获取字符串 [英] Getbytes string in c#
问题描述
现在我从C#发送socket到linux,我有1个功能。
private string decchr( int num, int l)
{
string resuilt = 跨度>;
for ( int i = 0 ; i < l; i ++)
{
resuilt =( char )(num& 255 )+ resuilt;
num = num>> 8 ;
}
return resuilt;
}
我调用函数和getbytes发送给。
string s = ;
s = decchr( 129 , 2 );
byte [] b = System.Text.Encoding.Default.GetBytes(s);
但是当我使用s = decchr(129,2)时,linux recvice是129但是当我改变129->> 128 linux recvice 63时。我不知道为什么??
您拥有的代码显然是尝试从C / C ++转换为C#。
但该代码完全是垃圾;正如已经告诉过你的那样,你可以更好地理解C ++函数的功能,以便能够在C#中进行调整。
基本上,这个功能是什么将整数值的四个字节提取为四个字节的数组,顺序相同。
因此:
private byte [] decchr( int num,< span class =code-keyword> int l){
byte [] result = new byte [l];
for ( int i = 0 ; i < l; i ++){
result [i] =((num>> 8 *(l - 1 - i))& 0xFF);
num>> = 8 ;
}
返回结果;
}
比将所有内容转换为字符串并再次返回的解决方案更加高效和优雅。
如果你真的想要字节数组的字符串表示,那么你可以使用:
byte [] myArray = decchr(myInt32Value, 4 );
string myStringRepresentationOfThisByteArray = myArray.ToHexString();
public static string ToHexString( this byte [] array){
StringBuilder sb = new StringBuilder();
for ( int i = 0 ; i < array.Length; i ++){
sb.AppendFormat( {0:X2},array [i]);
}
return sb.ToString();
}
编辑:更正int到byte []转换以保留原始字节顺序。
为什么(新鲜的地狱)你'将'整数插入一个字符串然后从中提取它们?这没有道理。只需将数字直接插入字节数组。
将第二个参数名称设为字母,而不是像(1/2/3)那样的值
Hi, now i doing socket from C# send to linux, i have 1 fucntion.
private string decchr(int num, int l) { string resuilt = ""; for (int i = 0; i < l; i++) { resuilt = (char)(num & 255) + resuilt; num = num >> 8; } return resuilt; }
and i call function and getbytes send to.
string s = ""; s = decchr(129, 2); byte[] b = System.Text.Encoding.Default.GetBytes(s);
but when i use s = decchr(129, 2) linux recvice is 129 but when i change 129->>128 linux recvice 63. I do not know why??解决方案The code you have is obviously an attempt to translate from C/C++ to C#.
But that code is complete garbage; as it has been told to you, you'd better understand what the C++ function does in order to be able to adapt it in C#.
Basically, what this function does is extract the four bytes of an integer value to an array of four bytes, in the same order.
Thus:
private byte[] decchr(int num, int l) { byte[] result = new byte[l]; for (int i = 0; i < l; i++) { result[i] = ((num >> 8 * (l - 1 - i)) & 0xFF); num >>= 8; } return result; }
Way much more efficient and elegant than the solution consisting of transforming everything to string and back again.
If you really want a string representation of the byte array, you can then use:
byte[] myArray = decchr(myInt32Value, 4); string myStringRepresentationOfThisByteArray = myArray.ToHexString(); public static string ToHexString(this byte[] array) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.Length; i++) { sb.AppendFormat("{0:X2}", array[i]); } return sb.ToString(); }
Edit : corrected int to byte[] translation to preserve the original byte order.
Why (the fresh Hell) are you 'inserting' integers into a string and then extracting them back from it? It makes no sense. Just insert the numbers directly into the byte array.
Make the 2nd param name as a alphabet not a value like (1/2/3)
这篇关于在c#中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!