473,834 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

short[] to byte[] conversion

Is there a direct way to convert a short array to a byte array?
I dont to use a for and cast every short to a byte.

I want something like the BitConverter class that accpets a short array as
argument.

Bitconverter.Ge tBytes( short[] );

--
Alberto Cardoso

Nov 16 '06 #1
9 16855
I dont think there is. You will need to loop through them.

Ciaran O'Donnell

"Alberto Cardoso" wrote:
Is there a direct way to convert a short array to a byte array?
I dont to use a for and cast every short to a byte.

I want something like the BitConverter class that accpets a short array as
argument.

Bitconverter.Ge tBytes( short[] );

--
Alberto Cardoso
Nov 16 '06 #2
Alberto,
>Is there a direct way to convert a short array to a byte array?
Depends on what kind of behaviour you want.

If you want to make a straight memory copy so that each short results
in two bytes, you can use the System.Buffer.B lockCopy method.

If you want just one byte for each short (assuming the value is in
range), you basically have to use a loop. Either write an explcit loop
or, if you're on .NET 2.0, use a method such as
Array.ConvertAl l<short, byte>().
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '06 #3
Alberto,

There is no direct method call AFAIK, but you can use the Array.ConvertAl l
method for that
short[] arrShort = new short[] { 100, 200, 30 };
byte[] arrByte;

arrByte = Array.ConvertAl l<short, byte>(arrShort, delegate(short
item){return (byte)item;});

--
HTH
Stoitcho Goutsev (100)

"Alberto Cardoso" <Al************ @discussions.mi crosoft.comwrot e in
message news:F9******** *************** ***********@mic rosoft.com...
Is there a direct way to convert a short array to a byte array?
I dont to use a for and cast every short to a byte.

I want something like the BitConverter class that accpets a short array as
argument.

Bitconverter.Ge tBytes( short[] );

--
Alberto Cardoso

Nov 17 '06 #4
There is no direct method call AFAIK, but you can use the
Array.ConvertAl l method for that

short[] arrShort = new short[] { 100, 200, 30 };
byte[] arrByte;
arrByte = Array.ConvertAl l<short, byte>(arrShort, delegate(short
item){return (byte)item;});
Just for the curious, here's what this looks like with lambda expressions
in C# 3.0:

short[] arrShort = new short[] { 100, 200, 30 };
byte[] arrByte = Array.ConvertAl l(arrShort, item =(byte)item);

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 17 '06 #5
Mattias,
If you want to make a straight memory copy so that each short results
in two bytes, you can use the System.Buffer.B lockCopy method.
Dang, I'm glad I ran across this post. That's exactly what I was wondering
about too.

One thing: Is there any support for copying bytes from an array to a
variable and the reverse? I realize I could do this by first creating an
array of the correct type and then assigning an element to a variable. But
is there by chance any way to do that directly?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Nov 17 '06 #6
Hi Jonathan,
>One thing: Is there any support for copying bytes from an array to a
variable and the reverse?
Check out the BitConverter class.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '06 #7
Hi Mattias,

That looks like it should do the trick, thanks!

One other question, if I may: what are the ramifications of using routines
that aren't CLS-compliant? I may want to run this code on a Web server. Is
that going to be a problem?

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:eM******** ******@TK2MSFTN GP06.phx.gbl...
Hi Jonathan,
>>One thing: Is there any support for copying bytes from an array to a
variable and the reverse?

Check out the BitConverter class.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 18 '06 #8
>One other question, if I may: what are the ramifications of using routines
>that aren't CLS-compliant? I may want to run this code on a Web server. Is
that going to be a problem?
CLS is all about language interoperabilit y and defining a lowest
common set of features that all managed languages should be able to
support. It's not related to security. So CLS is really only relevant
for people writing components that may be consumed by other languages.
If you do that, you may want to restrict your public interfaces to CLS
compliant features (or at least provide CLS compiliant alternatives).

Consuming non-CLS compilant stuff in your own code is generally not a
problem. That's an implementation detail that others don't care about.

However, one of the things that aren't CLS compilant are pointer
types, and to enable use of pointer types in C# you have to turn on
unsafe code. And _that_ may affect your code's ability to execute in
partially trusted environments.

Does that answer your question?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 18 '06 #9
Hi Mattias,
One other question, if I may: what are the ramifications of using
routines
that aren't CLS-compliant? I may want to run this code on a Web server. Is
that going to be a problem?

CLS is all about language interoperabilit y and defining a lowest
common set of features that all managed languages should be able to
support. It's not related to security. So CLS is really only relevant
for people writing components that may be consumed by other languages.
If you do that, you may want to restrict your public interfaces to CLS
compliant features (or at least provide CLS compiliant alternatives).

Consuming non-CLS compilant stuff in your own code is generally not a
problem. That's an implementation detail that others don't care about.

However, one of the things that aren't CLS compilant are pointer
types, and to enable use of pointer types in C# you have to turn on
unsafe code. And _that_ may affect your code's ability to execute in
partially trusted environments.

Does that answer your question?
I think so. Basically, it's only an issue (potentially) if I'm writing
components to be used by other .NET languages. And it should have no effect
on being used on a Web server. I think I would avoid the pointer types then
for Web programming.

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Nov 18 '06 #10

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

Similar topics

3
8481
by: mailar | last post by:
HI, Can anyone tell me how is a multi_byte to single byte and vice versa conversion done in DB2. It would be great even if someone can tell me how Oracle does it? Oracle already has functions called to_single_byte and to_multi_byte which convert a given multi byte character to single byte and vice versa respectively. thanks in advance mailar
0
2287
by: Selvin | last post by:
Hi all, 1. if I use BinaryFormatter(for the struct below) I get:  ˙˙˙˙  IMsgProtocol, Version=1.0.1291.23445, Culture=neutral, PublicKeyToken=null msgProtocol.Login uinhashstatus description msgProtocol.eStatus  Y ũ˙˙˙msgProtocol.eStatus value__    asdasda 2. is the easier way to convert Header struct to byte
5
2320
by: Ian | last post by:
I am creating an XML file through the XmlTextWriter. This is output to a MemoryStream which I convert a string through a Byte Array. Everything works correctly except for one BIG issue. My XML file is being truncated somewhere in the process. Large XML files give a truncated result, and small ones rsult in a Byte.Length = 0 . I assume the it is getting stuck in a buffer?? I tried Fluch() on the MemStream and Base Stream without success....
5
3996
by: Igor Natachanny | last post by:
Wich way I can to take the BYTE*-pointer to a string of characters that contain into CString-object?
4
2010
by: Richard506 | last post by:
If you take this Byte Arra Dim bytes() As Byte = { 207, 224, 135, 161, 253, 233, 111, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 and write the byte array to disk FileOpen(2, "f:\aaapicture_album\result.txt", OpenMode.Binary FilePut(2, bytes FileClose(2 then convert it into a string --->
1
1570
by: Tim Wagaman | last post by:
The project that I am working on currently, is a server application that communicates across TCP IP. In this application, I recieve a request in a TCP packet with a 4byte header. This 4 byte header is basically 3 null bytes followed by the byte count in Hex. I need to respond to the request using the same format. 3 null bytes and the byte count in Hex followed by string data. The problem I have is the response is always > 255 bytes. If...
4
1705
by: Lew Barnesson | last post by:
Hi, I have a byte array read from a file using Stream IO, and containing a mixture of 16-bit integers, strings, etc.. The problem: what is the best way to convert two byte sequences into 16-bit integers in a way similar to what I used to do with C or C++ formatted IO? Is there really no direct way to accomplish this without my having to write my own formatted IO routines? Any help appreciated.
2
5041
by: =?Utf-8?B?QmFydE1hbg==?= | last post by:
Greetings, I am trying to copy an unmanaged buffer into a mananged buffer, and trying to use marshalling, and I am not having any success because it doesn't seem to complie. Basically I am using Directshow's sample grabber interface to a filter, and trying to copy a buffer to a managed buffer will little success. I get an error that my paramaters do match. I change the parameters, and still can't seem to get the marshall to compile. ...
9
3482
by: ikbel borcheni | last post by:
how can I convert a char* to a byte array. I'm working on VC++ (with MFC). char* buffer_reponse="01EFF01000011DF";//hex BYTE* byte_buffer=new BYTE; Thanks for help
0
9800
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9651
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10516
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10556
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9339
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6960
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3987
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3085
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.