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

Reading bytes from BigEndian

TF
Hi,
I've a byte array with 4 elements in BigEndian format i.e. {0, 0, 0,
12} and i want to convert it to 4-byte integer (Int32). I am trying
Encoding.BigEndianUnicode property but it has no effect on the result.
It always gives me number '201326592' instead of '12'.

Any help??

Here is the code:

Dim a_byte() As Byte = New Byte() {0, 0, 0, 12}

Dim ms As New MemoryStream(a_byte)
Dim br As New BinaryReader(ms, Encoding.BigEndianUnicode)

Dim x As Int32 = br.ReadInt32() ' expecting x = 12

br.Close()
ms.Close()
Jul 21 '05 #1
3 2087
TF <fa****@coned.com> wrote:
I've a byte array with 4 elements in BigEndian format i.e. {0, 0, 0,
12} and i want to convert it to 4-byte integer (Int32). I am trying
Encoding.BigEndianUnicode property but it has no effect on the result.
It always gives me number '201326592' instead of '12'.


The Encoding is only used when you're reading strings. You can't change
the endianness of BinaryReader/Writer, unfortunately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
That number is actually correct. Because it's BigEndian, then Encoding reverses your bytes into 12,0,0,0. COnverting this to binary = 00001100 00000000 00000000 00000000. If you do all the math, it comes out to 201326592. Below is a chart I made up quick in MS excel. If you add the 27th and 28th numbers (since these are set to 1 in the binary stuff above) you get 201326592. If you acutally want the number 12 to come out of your code, your byte array should look like: 12,0,0,0

1
2
4
8
16
32
64
128

256
512
1024
2048
4096
8192
16384
32768

65536
131072
262144
524288
1048576
2097152
4194304
8388608

16777216
33554432
67108864 (this one)
134217728 (and this one)
268435456
536870912
1073741824
2147483648
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Jul 21 '05 #3
<Domenic (dd******@troopers.state.ny.us)> wrote:
That number is actually correct. Because it's BigEndian, then
Encoding reverses your bytes into 12,0,0,0. COnverting this to binary
= 00001100 00000000 00000000 00000000.


No, that would be little endianness. In little endianness, the least
significant byte is stored first, so 12 would be stored as {12, 0, 0,
0} - you'd reverse it to get {0, 0, 0, 12} or binary
00000000 00000000 00000000 00001100

See http://en.wikipedia.org/wiki/Endianness

The reason it's not working is because the encoding used by a
BinaryReader has nothing to do with calls to ReadInt32.

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

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

Similar topics

0
by: Cormac O'Donnell | last post by:
I am implementing an fft algorithm in java and would like to clear this point up. I am reading a 16 bit wav file with format : float sampleRate = 16000; //8000,11025,16000,22050,44100 int...
6
by: Dietrich Epp | last post by:
Are there any good modules for reading a bitstream? Specifically, I have a string and I want to be able to get the next N bits as an integer. Right now I'm using struct.unpack and bit operations,...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
3
by: TF | last post by:
Hi, I've a byte array with 4 elements in BigEndian format i.e. {0, 0, 0, 12} and i want to convert it to 4-byte integer (Int32). I am trying Encoding.BigEndianUnicode property but it has no effect...
6
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char....
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
9
by: fscked | last post by:
Anyone have an idea of what I might do to fix this? I have googled adn can only find some random conversations about it that doesn't make sense to me. I am basically reading in a csv file to...
1
by: albert_k_arhin | last post by:
Hi All, I am new to python and I am using a strip down version of python that does not support struc,pack,etc. I have a binary protocol that is define as follows: PART OffSet Lenght...
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...
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: 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
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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.