PDA

Просмотр полной версии : Работа с массивом байт Bytes.cs [C#]


FreePVP)))
18.12.2011, 16:21
using System;
using System.Collections.Generic;
using System.Text;

namespace PlayerInformation
{
class Bytes
{

public static byte[] WriteWord(ushort value)
{
return BitConverter.GetBytes(value);
}
public static byte[] WriteWord(byte[] bt1,ushort value)
{
return Bytes.WriteBytes(bt1, BitConverter.GetBytes(value));
}
public static byte[] WriteDWord(uint value)
{
return BitConverter.GetBytes(value);
}
public static byte[] WriteDWord(byte[] bt1,uint value)
{
byte[] ret = Bytes.WriteBytes(bt1, BitConverter.GetBytes(value));
return ret;
}
public static byte[] WriteQWord(byte[] bt1,ulong value)
{
return Bytes.WriteBytes(bt1, BitConverter.GetBytes(value));
}
public static byte[] WriteQWord(ulong value)
{
return BitConverter.GetBytes(value);
}
public static string GetByteFormat(byte bt1)
{
string bts1 = Convert.ToString(bt1, 16);
if (bts1.Length == 1) bts1 = "0" + bts1;
else return bts1;
return bts1;
}
public static string UStringRead(byte[] bytes)
{
int textLen = bytes.Length / 2;
string newText = "";
byte[] text = new byte[textLen];
for (int i = 0; i < textLen; i++)
{
int numb = (i * 2);
text[i] = bytes[numb];
newText = Encoding.GetEncoding(1251).GetString(text);
}
return newText;
}
return newText;
}
public static byte[] UStringWrite(string Text)
{
byte[] TextBytes = Encoding.GetEncoding(1251).GetBytes(Text);
byte[] retBytes = new byte[0];
for (int i = 0; i < TextBytes.Length; i++)
{
retBytes = Bytes.WriteBytes(retBytes, TextBytes[i]);
retBytes = Bytes.WriteBytes(retBytes, 0x00);
}
return retBytes;
}
public static byte[] ToBytes(string pack)
{
string[] spack = pack.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
byte[] ret = new byte[spack.Length];
for (int i = 0; i < spack.Length; i++) ret[i] = Convert.ToByte(Convert.ToInt32(spack[i], 16));
return ret;
}
public static byte[] WriteBytes(byte[] bt1, byte bt2)
{
byte[] bt = new byte[bt1.Length + 1];
for (int i = 0; i < bt1.Length; i++ ) bt[i] = bt1[i];
bt[bt1.Length] = bt2;
return bt;
}
public static string ToString(byte[] pack)
{
string packet = "";
for (int i = 0; i < pack.Length; i++) packet += Convert.ToString(Bytes.GetByteFormat(pack[i])) + " ";
return packet;
}
public static byte[] WriteBytes(byte[] bt1, byte[] bt2)
{
byte[] bt = new byte[bt1.Length + bt2.Length];
for (int i = 0; i < bt1.Length; i++) bt[i] = bt1[i];
for (int i = 0; i < bt2.Length; i++) bt[i + bt1.Length] = bt2[i];
return bt;
}
public static byte[] ReadBytes(byte[] bt, int min, int len)
{
byte[] retbt = new byte[len];
for (int i = 0; i < len; i++) retbt[i] = bt[i + min];
return retbt;
}
public static byte[] GetOpcode(byte[] bt, byte opcode)
{
byte[] btret = new byte[bt.Length + 2];
btret[0] = opcode;
btret[1] = Convert.ToByte(bt.Length);
for (int i = 0; i < bt.Length; i++) btret[i + 2] = bt[i];
return btret;
}
}
}

Kitsune
18.12.2011, 19:33
Бред же.

FreePVP)))
18.12.2011, 20:25
Бред же.
Почему?
Удобно же не писать каждый раз лишние строки

Kitsune
19.12.2011, 00:29
Выразился малость резко. Не рационально, не оптимизировано, велосипед. Реализация некоторых методов вообще абсурдна.

FreePVP)))
19.12.2011, 13:21
Выразился малость резко. Не рационально, не оптимизировано, велосипед. Реализация некоторых методов вообще абсурдна.
Всегда можно довести код до ума)

Kitsune
19.12.2011, 13:26
Всегда можно довести код до ума)
Тогда и стоит им делиться.

FreePVP)))
19.12.2011, 17:28
Тогда и стоит им делиться.
А я не хочу сейчас что-то менять, так как все, что мне надо, тут есть:)

Kitsune
19.12.2011, 18:40
Чубарые методы: UStringRead, UStringWrite, GetByteFormat, GetOpcode
В других методах жестокий велосипед.

FreePVP)))
19.12.2011, 20:21
Чубарые методы: UStringRead, UStringWrite, GetByteFormat, GetOpcode
В других методах жестокий велосипед.
UString методы - главное, что работают:)
GetByteFormat - для наглядности
GetOpcode - на тот момент мне не нужны были пакеты с опкодом на 2 байта

[RO]jkpro
20.12.2011, 15:25
UString методы - главное, что работают
Не, неправильный подход, ну и + UString не работает


for (int i = 0; i < textLen; i++)
{
int numb = (i * 2) - 1; тут i = 0, i*0 -1 => numb = -1
byte[] text = new byte[textLen];
text[i] = bytes[numb]; а тут будет экзепшен
newText = Encoding.GetEncoding(1251).GetString(text);
}


да и вообще он что-то не понятное творит, разве Encoding.GetEncoding(1251).GetString(bytes) не прочитает самостоятельно текст?

FreePVP)))
08.01.2012, 19:16
jkpro;2214387"]да и вообще он что-то не понятное творит, разве Encoding.GetEncoding(1251).GetString(bytes) не прочитает самостоятельно текст?
Что-то я правда намудрил с UStingRead
Поправил код)