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

From ushot to byte[]

Hi guys,
I've a ushort variable and I need to copy it in an array of byte.
I wrote:

m_bMsg[0] = (byte) (p_sTrackNumber & 0xff00 >> 8);
m_bMsg[1] = (byte) (p_sTrackNumber & 0x00ff >> 16);

but I would like to know if exist a better way by a C# class.
Tnx

Nov 17 '05 #1
4 2264
Hi,

Check out BitConverter.GetBytes(). This just gets the bytes as an array.

"Salvatore Di Fazio" <sa***************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi guys,
I've a ushort variable and I need to copy it in an array of byte.
I wrote:

m_bMsg[0] = (byte) (p_sTrackNumber & 0xff00 >> 8);
m_bMsg[1] = (byte) (p_sTrackNumber & 0x00ff >> 16);

but I would like to know if exist a better way by a C# class.
Tnx
Nov 17 '05 #2
Hello

I am not sure about the better way, but your sample is not the best one,
because it's totaly wrong.
More corectly will be:

m_bMsg[0] = (byte)(sTrackNumber & 0xFF); // low byte
m_bMsg[1] = (byte)((sTrackNumber >> 8) & 0xFF); // hi byte

Of course we can swap bytes order. But you need not to shift anything by 16
bits.
--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Salvatore Di Fazio" <sa***************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi guys,
I've a ushort variable and I need to copy it in an array of byte.
I wrote:

m_bMsg[0] = (byte) (p_sTrackNumber & 0xff00 >> 8);
m_bMsg[1] = (byte) (p_sTrackNumber & 0x00ff >> 16);

but I would like to know if exist a better way by a C# class.
Tnx

Nov 17 '05 #3
Hi,
byte[] a = System.encoding.ascii.getstring("strTobeConverted" );
search for System.encoding.ascii.* for ushort variables. This gonna helps
you.
"Salvatore Di Fazio" <sa***************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi guys,
I've a ushort variable and I need to copy it in an array of byte.
I wrote:

m_bMsg[0] = (byte) (p_sTrackNumber & 0xff00 >> 8);
m_bMsg[1] = (byte) (p_sTrackNumber & 0x00ff >> 16);

but I would like to know if exist a better way by a C# class.
Tnx

Nov 17 '05 #4
Hasan O <ho********@gmail.com> wrote:
byte[] a = System.encoding.ascii.getstring("strTobeConverted" );
search for System.encoding.ascii.* for ushort variables. This gonna helps
you.


No, Encodings are for *text* - there's no text involved here as far as
I can tell, just a ushort and bytes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~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

1
by: Michael John | last post by:
Dear Oracle Developers, my task is to make up a Oracle View from a Pivot table in MS Access. Given are two tables to join: T_FIRM: FIRM_ABBR VARCHAR2(3 BYTE), FIRM_LONG ...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
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...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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...

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.