473,401 Members | 2,068 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,401 software developers and data experts.

BitConverter.ToInt16 Issue

byte []array = {0x00,0x04};
short ashort = BitConverter.ToInt16(array,0);
Console.WriteLine(ashort.ToString());
Why am I wrong to think that this should return a 4 instead of 1024?
Also, is there a way to get a short value of 4 from those 2 bytes
without switching their possition in the byte array?
Thanks for your input,
Michael Davidov

Nov 16 '05 #1
1 2207
Michael Davidov <si*****@gmail.com> wrote:
byte []array = {0x00,0x04};
short ashort = BitConverter.ToInt16(array,0);
Console.WriteLine(ashort.ToString());

Why am I wrong to think that this should return a 4 instead of 1024?
Because BitConverter is little-endian in the MS implementation.
Also, is there a way to get a short value of 4 from those 2 bytes
without switching their possition in the byte array?


Yup - use a class which lets you deal with it in a big-endian way.
Fortunately, I have such a class you can use :)

Look at EndianBitConverter in my MiscUtil library
http://www.pobox.com/~skeet/csharp/miscutil

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

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

Similar topics

4
by: Greg Ennis | last post by:
If I run the following code: Byte bytes = System.Text.Encoding.ASCII.GetBytes("test"); return BitConverter.ToString(bytes); I get the string "74-65-73-74" back. My question is, what is the...
1
by: Gabe Jahn | last post by:
Basically I've used the BitConverter to convert a byte into a string. Now how do I convert it back to a byte? Hopefully without creating my own algorithm to accomplish this? Thanks...
5
by: george r smith | last post by:
Hi, I created a byte array --byte aByteArray = new byte; and set aByteArray and aByteArray to '1'. Because I want to create a ulong (UInt64) with these values I do: UInt64 bb; bb =...
2
by: Opa1 | last post by:
using BitConverter.ToString() in Compact Framework has some bugs. The resulting hex string does not have a consistent format for each byte converted from the input byte array. for example:...
3
by: Timothy V | last post by:
Hi, I have a byte that i want to convert into a string. I use the BitConverter.ToString() method. Now, how do I convert that string back into a byte? Thanks in advance, Tim.
7
by: Lenn | last post by:
This probably something stupid, or I am missing some fundemantal concept, but I can't figure this one out. Consider the following code: byte bd = new byte; bd = 0x00; bd = 0x01; ...
5
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
Hello. I have a problem with getting short value from 2 byte array. I have this code. There are 2 short values in bytes. byte cast = { 18, 152, 00, 80 }; Int32 port = BitConverter.ToInt16(cast,...
1
by: DR | last post by:
mySqlBytes.buffer is getting converted to BigEndian even though both SQL server 2005 and the CLR function are on the same machine which shows BitConverter.IsLittleEndian == true in tsql: select...
5
by: DaveD | last post by:
Can anyone help me get this compiled ? void Write<T>(T val) { byte bytes = BitConverter.GetBytes(val); Array.Reverse(bytes); writer.Write(bytes); } The problem is that for T=bool,...
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
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
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,...
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.