我正在尝试按顺序发送与数字1-1000对应的字节的UDP数据包.如何将每个数字(1,2,3,4,...,998,999,1000)转换为所需的最小字节数,并将它们放入我可以作为UDP数据包发送的序列中?
我试过以下但没有成功.任何帮助将不胜感激!
List<byte> byteList = new List<byte>();
for (int i = 1; i <= 255; i++)
{
byte[] nByte = BitConverter.GetBytes((byte)i);
foreach (byte b in nByte)
{
byteList.Add(b);
}
}
for (int g = 256; g <= 1000; g++)
{
UInt16 st = Convert.ToUInt16(g);
byte[] xByte = BitConverter.GetBytes(st);
foreach (byte c in xByte)
{
byteList.Add(c);
}
}
byte[] sendMsg = byteList.ToArray();
Run Code Online (Sandbox Code Playgroud)
谢谢.
我期望下面的数组初始化的大小为32. 1字节字符,列表中的每个项目为2个字节,16个项目.... = 32.但是它是128个字节.为什么?
char* cmds[] = {"AQ", "BD", "LS", "TW", "AS", "CP", "TR", "CO", "BF", "MS", "SR", "TL", "WT", "PM", "TE", "TC"};
printf("%li\n", sizeof(cmds));
//result is 128
//size of list is 16
//8 bytes per item in the list
//why?
Run Code Online (Sandbox Code Playgroud) 我有一个webservice,它将二进制数据作为字符串返回.使用C#代码如何将其存储在字节数组中?这是正确的方法吗?
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(inputString);
Run Code Online (Sandbox Code Playgroud)
实际上,这不起作用.让我解释一下:Web服务代码使用utf8编码将字符串(包含XSLFO数据)转换为字节数组.在我的Web服务响应中,我只看到类似的数据"PGZvOnJvb3QgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdCIgeG1sbnM6eGY9Imh0dHA6Ly93d3cuZWNyaW9uLmNvbS94Zi8xLjAiIHhtbG5zOm1zeHNsPSJ1c=="
.实际上我希望在服务中将原始字符串值转换为byte [].不确定是否可能?
请查看以下机器代码
0111001101110100011100100110010101110011011100110110010101100100
这意味着什么.我需要将其转换为字符串.当我使用Integer.parseInt()将上面的字符串作为字符串而2作为基数(将其转换为字节)时,它会给出数字格式异常.
我相信我必须把它分成8件(如01110011,10111010等).我对么?
请帮我把它正确转换成字符串.
谢谢
目前,我正在与asp.net and c#
将图像存储到MySql
(使用Blob数据类型)。我已经成功地将其存储到数据库中,但是现在的问题是how can i retrieve that byte[] to image format ?
功能: code to convert byte[] to image
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); --> here gives me error as `parameter is not valid`
return returnImage;
}
Run Code Online (Sandbox Code Playgroud)
改写为数据表...
if (dt1.Rows.Count > 0)
{
byteArrayToImage((byte[]) dt1.Rows[0]["PortfolioSlideImages"]);
//MemoryStream ms = new MemoryStream((byte[])dt1.Rows[0]["PortfolioSlideImages"]);
//Image returnImage = Image.FromStream(ms);
//return returnImage;
}
Run Code Online (Sandbox Code Playgroud) 你会如何很好地表示字节数组及其大小?我想存储(在主存储器或文件中)原始字节数组(无符号字符),其中前2/4字节将表示其大小.但是对这种阵列的操作看起来不太好:
void func(unsigned char *bytearray)
{
int size;
memcpy(&size, bytearray, sizeof(int));
//rest of operation when we know bytearray size
}
Run Code Online (Sandbox Code Playgroud)
我怎么能避免这种情况?我想一个简单的结构:
struct bytearray
{
int size;
unsigned char *data;
};
bytearray *b = reinterpret_cast<bytearray*>(new unsigned char[10]);
b->data = reinterpret_cast<unsigned char*>(&(b->size) + 1);
Run Code Online (Sandbox Code Playgroud)
我可以访问bytearray的大小和数据部分.但它看起来仍然很难看.你能推荐另一种方法吗?
如果我做的事情
int keyLength = 160; // because SHA1 generates 160-bit hashes
int iterations = 20 * 1000; //standard is 2000 but let's be more secure here
KeySpec spec = new PBEKeySpec(password.toCharArray(), generateSalt(), iterations, keyLength);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = keyFactory.generateSecret(spec).getEncoded();
Run Code Online (Sandbox Code Playgroud)
如何将此哈希值转换为字符串,以便将其保存到数据库中?我尝试过,new String(hash, "UTF-8");
但这样会产生格式错误的字符l??0\?w?c??Q?
.
给定一个字节组和一个新的字节组,其中我需要覆盖其上的原始内容bytearray
,但开始从特定位置/偏移(A)如下面的图像中(我们可以说B是长度新数组).如果覆盖超过原始数组的实际长度,也处理新长度.
(这是.WAV文件覆盖在不同位置所需的).
这是我到目前为止尝试但没有运气.
public byte[] ByteArrayAppender(byte[] firstData, byte[] newData, byte[] remainData, int Position) {
byte[] editedByteArray = new byte[firstData.length + newData.length + remainData.length];
System.arraycopy(firstData, 0, editedByteArray, 0, Position);
System.arraycopy(newData, 0, editedByteArray, firstData.length, newData.length);
System.arraycopy(remainData, 0, editedByteArray, newData.length, remainData.length);
return editedByteArray;
}
Run Code Online (Sandbox Code Playgroud) 我已经看到很多情况,其中声明了一个字节,但是来自intToByte或StringToByte之类的方法的值 被转换为一个字节,因为程序员提供的是十六进制 - 值,整数 - 或字符串 -值.
我试图为变量分配一个实际的字节值,而不需要任何转换或方法来解析,如下所示:
public class ByteTest {
/**
* This array will be used to hold three characters, together forming a string.
*/
private static byte[] string;
/**
* The main method of the program, where the byte-array is coming to use.
*/
public static void main(String args[]) {
//Construct the array with a limit to three bytes.
string = new byte[3];
/*
* Fill the three …
Run Code Online (Sandbox Code Playgroud) 我想知道是否使用bytearray来构造类似的字符串.
def build_string(pairs):
data = ''
for key, value in pairs.iteritems():
data = data + '\r\n' + '%s:%s' % (key, value)
data = data + '\r\n\r\n'
return data
Run Code Online (Sandbox Code Playgroud)
会慢一些.
def build_string(pairs):
data = bytearray()
for key, value in pairs.iteritems():
data.extend('%s:%s\r\n' % (key, value))
data.extend('\r\n')
return data
Run Code Online (Sandbox Code Playgroud)