如何将BYTE缓冲区(从0到255)转换为浮点缓冲区(从0.0到1.0)?当然,这两个值之间应该存在关系,例如:字节缓冲区中的0将是浮点缓冲区中的.0.f,字节缓冲区中的128将是浮点缓冲区中的.5f,字节缓冲区中的255将是1.f in浮动缓冲区.
实际上这是我的代码:
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
float* floatpixel = floatbuffer + (y * width + x) * 4;
BYTE* bytepixel = (bytebuffer + (y * width + x) * 4);
floatpixel[0] = bytepixel[0]/255.f;
floatpixel[1] = bytepixel[1]/255.f;
floatpixel[2] = bytepixel[2]/255.f;
floatpixel[3] = 1.0f; // A
}
}
Run Code Online (Sandbox Code Playgroud)
这很慢.我的一个朋友建议我使用转换表,但我想知道其他人是否可以给我另一种方法.
我不是套接字编程的老手,所以在分析我在数据库API中找到的代码时,我遇到了这段代码
public static void WriteInt(int i, NetworkStream bufOutputStream)
{
byte[] buffer = new byte[IntSize];
WriteInt(i, buffer, 0);
bufOutputStream.Write(buffer, 0, buffer.Length);
}
public static void WriteInt(int i, byte[] byte_array, int pos)
{
byte_array[pos] =(byte)( 0xff & (i >> 24)); byte_array[pos+1] = (byte)(0xff & (i >> 16)); byte_array[pos+2] = (byte)(0xff & (i >> 8)); byte_array[pos+3] = (byte)(0xff & i);
}
Run Code Online (Sandbox Code Playgroud)
我理解我不理解的位移是'缓冲'var如何在args中没有ref或者没有返回时继续得到值.位移是以某种方式编辑缓冲区的实际值?
我为这篇冗长的帖子道歉.我仍然尽可能小,同时仍然传达问题.
好的,这让我发疯了.我有一个客户端和一个服务器程序,都在C#中.服务器通过Socket.Send()将数据发送到客户端.客户端通过Socket.BeginReceive和Socket.Receive接收数据.我的伪协议如下:服务器发送一个双向(短)值,表示实际数据的长度,紧接着是实际数据.客户端异步读取前两个字节,将字节转换为short,并立即同步从套接字读取多个字节.
现在每隔几秒左右就可以正常工作一个周期,但是当我提高速度时,事情会变得奇怪.似乎客户端在尝试从两个字节长度读取时会随机读取实际数据.然后它尝试将这些任意两个字节转换为short,这会导致完全不正确的值,从而导致崩溃.以下代码来自我的程序,但修剪为仅显示重要的行.
用于发送数据的服务器端方法:
private static object myLock = new object();
private static bool sendData(Socket sock, String prefix, byte[] data)
{
lock(myLock){
try
{
// prefix is always a 4-bytes string
// encoder is an ASCIIEncoding object
byte[] prefixBytes = encoder.GetBytes(prefix);
short length = (short)(prefixBytes.Length + data.Length);
sock.Send(BitConverter.GetBytes(length));
sock.Send(prefixBytes);
sock.Send(data);
return true;
}
catch(Exception e){/*blah blah blah*/}
}
}
Run Code Online (Sandbox Code Playgroud)
用于接收数据的客户端方法:
private static object myLock = new object();
private void receiveData(IAsyncResult result)
{
lock(myLock){
byte[] buffer = new byte[1024];
Socket sock …Run Code Online (Sandbox Code Playgroud) 我的代码中有一个小错误,我不能为我的生活弄清楚.
我有一个字符串数组,它们是二进制数据的表示形式(在从十六进制转换之后),例如:一个索引是1011,另一个是11100.我遍历数组并用0填充每个索引,以便每个索引是8个字节.当我尝试将这些表示转换为实际字节时,当我尝试解析'11111111'时出现错误我得到的错误是:
java.lang.NumberFormatException: Value out of range. Value:"11111111" Radix:2
Run Code Online (Sandbox Code Playgroud)
这是一个片段:
String source = a.get("image block");
int val;
byte imageData[] = new byte[source.length()/2];
try {
f.createNewFile();
FileOutputStream output = new FileOutputStream(f);
for (int i=0; i<source.length(); i+=2) {
val = Integer.parseInt(source.substring(i, i+2), 16);
String temp = Integer.toBinaryString(val);
while (temp.length() != 8) {
temp = "0" + temp;
}
imageData[i/2] = Byte.parseByte(temp, 2);
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法,它采用十六进制值并将其指定为明文但类型的字节
byte plainText = 0xd7;
Run Code Online (Sandbox Code Playgroud)
我想从文本框中获取此值,例如,用户将d7键入文本框,ı将分配给它
byte plaintText = 0xd7
Run Code Online (Sandbox Code Playgroud)
我无法实现这一点.
我正在读取id3标签,其中每个帧的大小以3个字节指定.我怎样才能将此值用作int?
刚刚在7天内学习了Python 3,我觉得我对字节字符串的理解有点漏洞.在Python 3中,假设我有一个字节字符串b'1234'.它的迭代器返回整数:
Python 3.2.3 (default, May 26 2012, 18:49:27)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> for z in b'1234':
... print(type(z))
...
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
Run Code Online (Sandbox Code Playgroud)
我可以在字节字符串中找到一个整数(定义in是它搜索相等):
>>> 0x32 in b'1234'
True
Run Code Online (Sandbox Code Playgroud)
但是,我想在字节字符串中找到给定整数的索引.bytes.index需要一个子字符串:
>>> b'1234'.index(b'2')
1
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一个x我想要找到的变量,这是我提出的最好的变量:
>>> x = 0x32
>>> b'1234'.index(bytes([x]))
1
Run Code Online (Sandbox Code Playgroud)
我知道Python比这更优雅.我显然遗漏了一些明显的东西.除了创建单个整数的序列之外,有关更简单的方法吗?或者是真的吗?
我正在尝试编写一个拍摄图像的方法,并保存该图像的100 x 100缩略图.然而,当我保存文件时,它出来作为一个不可读0字节的图像(与错误"错误地解释了JPEG图像文件(不正确的呼叫状态200 JPEG库)")在Ubuntu的图像浏览器.我的代码如下:
public boolean scale(){
String file = filename.substring(filename.lastIndexOf(File.separator)+1);
File out = new File("data"+File.separator+"thumbnails"+File.separator+file);
if( out.exists() ) return false;
BufferedImage bi;
try{
bi = ImageIO.read(new File(filename));
}
catch(IOException e){
return false;
}
Dimension imgSize = new Dimension(bi.getWidth(), bi.getHeight());
Dimension bounds = new Dimension(100, 100);
int newHeight = imgSize.height;
int newWidth = imgSize.width;
if( imgSize.width > bounds.width ){
newWidth = bounds.width;
newHeight = (newWidth*imgSize.height)/imgSize.width;
}
if( imgSize.height > bounds.width ){
newHeight = bounds.height;
newWidth = (newHeight*imgSize.width)/imgSize.height;
}
Image img …Run Code Online (Sandbox Code Playgroud) 我有一个问题,希望对您来说很简单。(Windows窗体应用程序,C#,Framework 3.5,SQL Server 2008 R2)
我不知道如何通过OleDB打开Excel(以byte []类型加载)。
所以,我做了什么:我已经通过表格上传了Excel(.xls(x))文件,并将其保存为varbinary(max)在数据库中。现在,我需要通过oleDB读取该Excel文件。我已经设法从数据库加载该文件并将其保存到byte []变量中。如何在oleDB中打开byte []?当我第一次上传文件时(在将其保存到数据库之前),我已经通过OleDB打开了文件路径。当Excel的数据已经作为byte []存储在内存中时,我该如何访问?