473,322 Members | 1,734 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,322 software developers and data experts.

ushort to byte[]

Hello,

I have:
ushort num = 0x0017;

how do I convert this to a byte array?
when I try BitConverter.GetBytes(num); I get {17,00}

I think I have to get {00,17}...
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
4 7770
Dirk,

If you have to get 17, 00, you can call the GetBytes method and then
pass the resulting array to the static Reverse method on the Array class.
This will flip the array around (in-place).

Are you working with sending something to a unix machine (or getting it
from a unix machine)? This is the only reason I can think of to use little
endian.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dirk Reske" <_f*******@gmx.net> wrote in message
news:er**************@TK2MSFTNGP15.phx.gbl...
Hello,

I have:
ushort num = 0x0017;

how do I convert this to a byte array?
when I try BitConverter.GetBytes(num); I get {17,00}

I think I have to get {00,17}...
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
I don't know, on which os the server is running!
but this is for network communication.

some lines from protocol:
this is the packet header:
----------------------
00 17 word SNAC family (0x0017 Authorization/registration service)
00 06 word SNAC subtype
00 00 word SNAC flags
----------------------
output from packet sniffer:00 17 00 06 00 00
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Dirk,

You don't really need to know the server it is running on, just the
protocol you are sending it (what the byte order is in the protocol).

You know how to sort the bytes, does it work when you do that?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dirk Reske" <_f*******@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I don't know, on which os the server is running!
but this is for network communication.

some lines from protocol:
this is the packet header:
----------------------
00 17 word SNAC family (0x0017 Authorization/registration service)
00 06 word SNAC subtype
00 00 word SNAC flags
----------------------
output from packet sniffer:00 17 00 06 00 00
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
Dirk Reske <_f*******@gmx.net> wrote:
I have:
ushort num = 0x0017;

how do I convert this to a byte array?
when I try BitConverter.GetBytes(num); I get {17,00}

I think I have to get {00,17}...


I have a version of BitConverter which allows you to specify the
endianness. See
http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

10
by: Axel Dahmen | last post by:
Hi, I want to start a technical discussion on the fact that C# doesn't define any mathematical operators on other integral types than int. Simple things like the following just aren't possible...
3
by: | last post by:
I want to convert an ushort * to a byte array needed for the Memorystream ushort *pshort=FileImageBuffer16.GetPtr(0,iY); Stream.Write((byte) pshort,0,iWidth*2); The big problem is that...
1
by: AndyM | last post by:
I am trying to figure out how to serialize data over a socket in C#. Every example I have seen shows sending a string by converting it to a byte array and then passing to Send(). Simple enough. But...
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
1
by: Marco [Stinger] | last post by:
COMPACT FRAMEWORK Hi Newbe question: I've got an external (Embedded C+) function that reads a PLC and returns an array of 16 unsigned (PLC people call them WORD, but in C# thay should be...
4
by: Salvatore Di Fazio | last post by:
Hi guys, I've a ushort variable and I need to copy it in an array of byte. I wrote: m_bMsg = (byte) (p_sTrackNumber & 0xff00 >> 8); m_bMsg = (byte) (p_sTrackNumber & 0x00ff >> 16); but I...
1
by: fishspm | last post by:
hello everyone in my source I receive a byte array (for eg: 0xED, 0xFF, 0x01, 0x3D). is there an easy way to convert this byte array to a ushort/uint16 array (0xffed, 0x3d01)? thank you very...
12
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
5
by: stuie_norris | last post by:
Hi Group I am trying to write a method to unpack a byte array into an Uint64. But the results are wrong. public static void UnpackUint64(ref UInt64 dst, byte Data, ref uint Index) { dst =...
4
by: BlueJ | last post by:
My program is to send data using socket.. but I got the error message: "sendto(msg) points to uninitialised byte(s) " This is my source code 1. key_list = emalloc(BUFSIZE - sizeof(Key) -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.