无符号整数到字节
Unsigned int to byte
我正在尝试创建自定义输入流。我的问题是,read() 方法 returns 一个从 0-255 的整数,但我需要将它转换为一个字节,对其进行解密,然后将其转换回整数。怎么样?
我需要这样的东西:
InputStream in = ...;
OutputStream out = ...;
int unsigned = in.read();
byte signed = unsignedIntToSignedByte(unsigned); // from -128 to 127
... // Editing it here
outputstream.write(signedByteToUnsignedInt(signed)); // from 0 - 255
注意到创建自己的加密是不安全的,并且假设您正在这样做 "just for fun" 并且不以任何方式认为您正在做的事情是安全的,您真的不需要任何东西特别...
int i = in.read();
byte b = (byte) i;
byte e = encrypt(b);
out.write(e);
将是基本方法,假设 byte encrypt(byte b)
方法执行 "encryption"。检查流结束、异常处理、性能考虑(您不想一次执行 1 个字节)等已从该示例中删除。
我正在尝试创建自定义输入流。我的问题是,read() 方法 returns 一个从 0-255 的整数,但我需要将它转换为一个字节,对其进行解密,然后将其转换回整数。怎么样?
我需要这样的东西:
InputStream in = ...;
OutputStream out = ...;
int unsigned = in.read();
byte signed = unsignedIntToSignedByte(unsigned); // from -128 to 127
... // Editing it here
outputstream.write(signedByteToUnsignedInt(signed)); // from 0 - 255
注意到创建自己的加密是不安全的,并且假设您正在这样做 "just for fun" 并且不以任何方式认为您正在做的事情是安全的,您真的不需要任何东西特别...
int i = in.read();
byte b = (byte) i;
byte e = encrypt(b);
out.write(e);
将是基本方法,假设 byte encrypt(byte b)
方法执行 "encryption"。检查流结束、异常处理、性能考虑(您不想一次执行 1 个字节)等已从该示例中删除。