473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting sbyte[] to byte[]

I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster method for converting an array of sbytes to an array of bytes?

Thanks,
Darrel
Nov 16 '05 #1
1 12346
Array.Copy will often perform conversions for you. However, in this case, it
doesn't
appear that it will, so that won't be an option. The problem is the conversion
is considered
narrowing, since you'll lose any negative numbers. I guess doing a blind memory
copy
wasn't in their plans.

I think Buffer.BlockCopy will do what you need though. You should check it out
and see
if it works in your scenario...

using System;

public class SByteToByte {
private static void Main(string[] args) {
sbyte[] sbytes = new sbyte[10];
byte[] bytes = new byte[10];

for(int i = 0; i < sbytes.Length; i++) {
sbytes[i] = (sbyte) (i - 5);
Console.WriteLine(sbytes[i]);
}
Console.WriteLine();

Buffer.BlockCopy(sbytes, 0, bytes, 0, bytes.Length);
for(int i = 0; i < bytes.Length; i++) {
Console.WriteLine(bytes[i]);
}
Console.WriteLine();

Buffer.BlockCopy(bytes, 0, sbytes, 0, bytes.Length);
for(int i = 0; i < sbytes.Length; i++) {
Console.WriteLine(sbytes[i]);
}
Console.WriteLine();
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Darrel" <Da****@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array
element in a for loop. Is there a faster method for converting an array of
sbytes to an array of bytes?
Thanks,
Darrel

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick | last post by:
Hi, Problem statement: Calling a java method from Dot Net code that returns a java byte array. Overview: I have a java server from which I need to establish connection. I hva ebeen able to...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
3
by: Steve - DND | last post by:
After downloading and using two different memory profiling applications today, it seems that sbyte, int16, and int32 are all stored using 12b of memory. Someone please tell me I'm reading these...
2
by: Ray Ackley | last post by:
I need to convert a byte to an sbyte - and it has to preserve the original binary meaning of the 8 bits. For example - a 1111 1111, which is interpreted as byte to be 255, should show up in the...
5
by: Pat Ireland | last post by:
I am attempting to pass an SByte to another .NET class that expects the data to be passed as SByte *. I searched around and found the boxing can be used to produce a pointer but I'm not sure if...
7
by: djc | last post by:
I have typically always used one of the VB CType functions (not CType itself but CInt, CString, etc...) to cast/convert an input value to its proper type before passing it as a parameter to a SQL...
3
by: Sharon | last post by:
There is an built-in types table for .NET framework type to C# type which are aliases of predefined types in the System namespace (http://msdn2.microsoft.com/en-US/library/ya5y69ds.aspx). How...
0
by: hillman | last post by:
Hello, Maestros. Following on from the posts on creating structs from a byte. The methods described there work well. But I seem to have struck a snag that some might find interesting. The C/C++...
1
by: =?Utf-8?B?UmljaA==?= | last post by:
Dear All, Does anyone know the algorithm used to convert to an sbyte like follows? It is not a straight hex conversion. sbyte.Parse("B2", System.Globalization.NumberStyles.HexNumber, null); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.