473,408 Members | 2,477 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,408 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 2090
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...
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?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.