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

byte array

simple question: i have a byte[] array.. how do i write to the console the
last 2 bytes of the byte[] array? thanks in advance
Oct 26 '06 #1
5 1923
Hi Rain,

You can just read the bytes using the index of the last two positions.
The last two positions can be calculated using Array.Length.

On Thu, 26 Oct 2006 09:14:01 +0200, Rain <Ra**@discussions.microsoft.com>
wrote:
simple question: i have a byte[] array.. how do i write to the console
the
last 2 bytes of the byte[] array? thanks in advance


--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 26 '06 #2
byte[] data = { 14, 242, 11, 34, 5 };
Console.WriteLine(data[data.Length - 2]);
Console.WriteLine(data[data.Length - 1]);

Or did you mean something more complex?

Marc
Oct 26 '06 #3
Hi Rain,

Use BitConverter.ToXXX methods. These methods take the byte[] array and the
start index. For instance to convert the last two bytes of a byte array to
Int16 you can write the following code.

byte[] data;

short i16 = ByteConverter.ToInt16(data,data.Length-3);

Hope this helps.
--
Regards,
Aditya.P
"Rain" wrote:
simple question: i have a byte[] array.. how do i write to the console the
last 2 bytes of the byte[] array? thanks in advance
Oct 26 '06 #4
Hi Rain,

Ignore my previous code snippet. Read the code as below.

byte[] data;

short i16 = BitConverter.ToInt16(data,data.Length-3);

Hope this helps.
--
Regards,
Aditya.P
"Adityanand Pasumarthi" wrote:
Hi Rain,

Use BitConverter.ToXXX methods. These methods take the byte[] array and the
start index. For instance to convert the last two bytes of a byte array to
Int16 you can write the following code.

byte[] data;

short i16 = ByteConverter.ToInt16(data,data.Length-3);

Hope this helps.
--
Regards,
Aditya.P
"Rain" wrote:
simple question: i have a byte[] array.. how do i write to the console the
last 2 bytes of the byte[] array? thanks in advance
Oct 26 '06 #5
Adityanand Pasumarthi <Ad******************@discussions.microsoft.com>
wrote:
Ignore my previous code snippet. Read the code as below.

byte[] data;

short i16 = BitConverter.ToInt16(data,data.Length-3);

Hope this helps.
Not quite - you've got an off by one error. It should be:

short i16 = BitConverter.ToInt16(data,data.Length-2);

(So it'll use bytes with index data.Length-2 and data.Length-1.)

--
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
Oct 26 '06 #6

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

Similar topics

16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
15
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?
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
8
by: frekster | last post by:
Hi. I used to be able to do this easily in vb 6 via looping and preserving the source array data/size etc. How can I do this in vb.net? I've been trying for a while now and this should be...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.