All,
Is there a faster way to do this in C#:
byte[] buffer;
buffer = new byte[43];
for(int i = 0; i < buffer.Length; i++)
buffer[i] = 0;
Dave 10 98745
D. Yates <fo****@hotmail .com> wrote: All,
Is there a faster way to do this in C#:
byte[] buffer; buffer = new byte[43];
for(int i = 0; i < buffer.Length; i++) buffer[i] = 0;
Yes:
byte[] buffer = new byte[43];
It's guaranteed to be all zeroes to start with.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dave,
Yes:
byte[] buffer;
buffer = new byte[43];
By default, elements of arrays of structures are the same as those
structures with the bits zeroed out, which is exactly what you want.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"D. Yates" <fo****@hotmail .com> wrote in message
news:e$******** ******@TK2MSFTN GP14.phx.gbl... All,
Is there a faster way to do this in C#:
byte[] buffer; buffer = new byte[43];
for(int i = 0; i < buffer.Length; i++) buffer[i] = 0;
Dave
Hello,
If you just want a 0 initialized array, you don't have to use any for loop
as the .net framework already make sure that every single value in your
array is 0.
if you want to initialize to some other values than 0, you can do the
following :
byte[] aoe = new byte[] {4, 5, 3, 0, 0, 0, 2, 2};
--
Francois Beaussier
"D. Yates" <fo****@hotmail .com> a écrit dans le message de news:
e$************* *@TK2MSFTNGP14. phx.gbl... All,
Is there a faster way to do this in C#:
byte[] buffer; buffer = new byte[43];
for(int i = 0; i < buffer.Length; i++) buffer[i] = 0;
Dave
Jon,
Assuming the byte array is being reused often, I was looking for a faster
way of doing this:
for(int i = 0; i < buffer.Length; i++)
buffer[i] = 0;
Dave
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... D. Yates <fo****@hotmail .com> wrote: All,
Is there a faster way to do this in C#:
byte[] buffer; buffer = new byte[43];
for(int i = 0; i < buffer.Length; i++) buffer[i] = 0;
Yes:
byte[] buffer = new byte[43];
It's guaranteed to be all zeroes to start with.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
byte[] buffer;
// array is used, values set, etc.
Array.Clear(buf fer, 0, buffer.Length);
// array is now set to all zeros (or default values of whatever type
the array is)
Dave,
You might get faster performance by passing the array to the static
Initialize method on the Array class.
However, the question has to be asked, why not just create a new array
when needed? If you are worried about memory concerns, that's what the GC
is for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"D. Yates" <fo****@hotmail .com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. .. Jon,
Assuming the byte array is being reused often, I was looking for a faster way of doing this:
for(int i = 0; i < buffer.Length; i++) buffer[i] = 0;
Dave
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com... D. Yates <fo****@hotmail .com> wrote: All,
Is there a faster way to do this in C#:
byte[] buffer; buffer = new byte[43];
for(int i = 0; i < buffer.Length; i++) buffer[i] = 0;
Yes:
byte[] buffer = new byte[43];
It's guaranteed to be all zeroes to start with.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Brant,
Thanks, that's what I was looking for. The funny thing is that the Dev
Partner profiler states that my brute force method of clearing an array of
18 bytes was faster than calling the Array.Clear method.
Thanks again,
Dave
Actually, now that I think of it, just simply reinitializing the array
does the same thing, and a little more than twice as performant:
byte[] buffer = new byte[] {1,2,3,4,5,6,7, 8,9};
buffer = new byte[buffer.Length];
Nicholas, However, the question has to be asked, why not just create a new array when needed? If you are worried about memory concerns, that's what the GC is for.
Well, I'm trying to make a method that is called a lot more efficient. I
came up with three version:
Version 1:
public static void StringToFixedSi zeByteArray(str ing sValue, byte[]
buffer)
{
if (buffer != null)
{
if ((sValue != null) && (sValue.Length > 0))
{
for(int i = 0; i < buffer.Length; i++)
{
if (i < sValue.Length)
buffer[i] = (byte) sValue[i];
else buffer[i] = 0;
}
}
else
{
Array.Clear(buf fer, 0, buffer.Length);
// for(int i = 0; i < buffer.Length; i++)
// buffer[i] = 0;
}
}
}
and finally:
Version 2:
public static byte[] StringToFixedSi zeByteArray(str ing sValue, int
iFixedSize)
{
byte[] buffer;
if (iFixedSize > 0)
{
buffer = new byte[iFixedSize];
if ((sValue != null) && (sValue.Length > 0))
{
if (sValue.Length < iFixedSize)
System.Text.Enc oding.UTF8.GetB ytes(
sValue, 0, sValue.Length, buffer, 0);
else System.Text.Enc oding.UTF8.GetB ytes(
sValue, 0, iFixedSize, buffer, 0);
}
}
else buffer = null;
return buffer;
}
Version 3:
public static byte[] StringToFixedSi zeByteArray(str ing sValue, int
iFixedSize)
{
byte[] buffer;
if (iFixedSize > 0)
{
buffer = new byte[iFixedSize];
if ((sValue != null) && (sValue.Length > 0))
{
for(int i = 0; i < buffer.Length; i++)
{
if (i < sValue.Length)
buffer[i] = (byte) sValue[i];
else break;
}
}
}
else buffer = null;
return buffer;
}
Version 3 is the fastest; however, I think I'm going to go with Version 2.
Dave This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Cook |
last post by:
Java's InetAddress class has some methods that use
a byte-array to hold what it describes as a 'raw IP address'.
So, I assume that they mean an array like:
byte ba = new byte;
would hold an IPv4 address.
Ok, yes, in theory, there are enough bits to hold the values.
But, my Java book clearly states that a byte is a SIGNED
quantity, is...
|
by: jeff |
last post by:
how do you convert form byte to Int32 while retaining the binary
value of the byte array
|
by: Dan C |
last post by:
Is there a routine in c# that will transform a string
ie"Hello Mom" into a
Byte array. I have found
char cTmp = pString.ToCharArray();
But I have not been able to figure out how to convert a char into a hex
value and place that value into the byte.
Thanks for your help
|
by: Kueishiong Tu |
last post by:
How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?
|
by: Ole |
last post by:
Hi,
Is there a better / faster way to convert a byte array to a short array than
to use Convert.toInt16(bytearray, i) in a loop?
Thanks
Ole
| |
by: quantumred |
last post by:
I found the following code floating around somewhere and I'd like to
get some comments.
unsigned char a1= { 5,10,15,20};
unsigned char a2= { 25,30,35,40};
*(unsigned int *)a1=*(unsigned int *)a2;
// now a1=a2, a1=a2, etc.
|
by: Gerrit |
last post by:
Hi all,
I'm getting an OutOfMemoryException when I initialize a byte array in
C# like this:
Byte test = new Byte;
I'm using ASP.NET 2.0. In ASP.Net 1.1 it works fine. So what am I doing
wrong?
|
by: trevor.farchild |
last post by:
Hi, long time reader, first time poster
I have an application that will be doing this 5 times a second:
Get a bunch of data from a NetworkStream and convert it into a Bitmap
Therefore, the process looks like:
Read the NetworkStream to find out size of the data
Allocate that amount as a byte array
Read the NetworkStream into the byte...
|
by: active |
last post by:
I have another post asking how to fill in the structure but I now think
GetDIBBits is the way, I just can't get it to work!
I can make the following run but GetDIBBits does not fill the array to
correctly reflect the data from the bitmap.
Any helpful suggestions would be appreciated.
Dim hInBitmap As IntPtr = InBitmap.GetHbitmap()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |