Stream Byte[] 转换

public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    stream.Seek(0, SeekOrigin.Begin);
    return bytes;
}

public Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}
时间: 2024-10-06 23:00:01

Stream Byte[] 转换的相关文章

Stream与byte转换

将 Stream 转成 byte[] /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin);

c# int与byte[]转换

网络上总结的方法汇总: 1: public byte[] intToByte(int i) { byte[] abyte0 = new byte[4]; abyte0[0] = (byte) (0xff & i); abyte0[1] = (byte) ((0xff00 & i) >> 8); abyte0[2] = (byte) ((0xff0000 & i) >> 16); abyte0[3] = (byte) ((0xff000000 & i)

byte数组转float实现与byte转换其它类型时进行&amp;运算原理

下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] & 0xff) << 0; accum = accum|(b[1] & 0xff) << 8; accum = accum|(b[2] & 0xff) << 16; accum = accum|(b[3] & 0xff) << 24;

int 转换 byte[] 或 byte[] 转换 int

byte[] bs = BitConverter.GetBytes(0x1234); Console.WriteLine(bs[0].ToString("X2") + " " + bs[1].ToString("X2")); // output: 34 12 低位在前,高位在后 byte[] bs2 = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)0x1

[Java] java byte数组与int,long,short,byte转换

public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b *            byte * @return convert result */ public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } /** * 将一个单字节的Byte转换成十六进制的数 * * @param b *            byt

byte数组与int,long,short,byte转换 (转载)

byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort(); } public byte[] shortToBytes(short value) { return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(valu

C# byte[] 转换16进制字符串

1.byte[] 转换16进制字符串 1.1 BitConverter方式 1 var str = DateTime.Now.ToString(); 2 var encode = Encoding.UTF8; 3 var bytes = encode.GetBytes(str); 4 var hex = BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower(); 5 Console.WriteLine(he

java中byte转换int时为何与0xff进行与运算

在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) {  String ret = "";  for (int i = 0; i < b.length; i++) {   String hex = Integer.toHexString(b[i] & 0xFF);   if (hex.length() == 1) {    hex = ''0'' + hex;   }   ret += hex.toUp

16进制到byte转换

我们经常会看到这样的语法 (byte) 0xAD 0xAD实际是个16进制,转换成二进制为:10101101,转换成10进制是:173,它是个正数 10101101只是int的简写,int由4个byte字节,即32位bit组成,实际的值是 (00000000 00000000 00000000 )10101101 int由4 byte组成,因此int转byte是会掉位的,直接截取最后一个字节,即: 10101101 符号位是1,因此它是负数,负数的存储方式是补码.因此要先求出补码才能计算值. 求