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

Byte Swap Class

Is the following the best way to do byte swapping in C#? Or is there
something like this already built into the framework and I'm just
overlooking it?

class ByteSwap {
public static UInt16 swap(UInt16 input) {
return ((UInt16)(
((0xFF00 & input) >8) |
((0x00FF & input) << 8)));
}

public static UInt32 swap(UInt32 input) {
return ((UInt32)(
((0xFF000000 & input) >24) |
((0x00FF0000 & input) >8) |
((0x0000FF00 & input) << 8) |
((0x000000FF & input) << 24)));
}

public static float swap(float input) {
byte[] tmpIn = BitConverter.GetBytes(input);
byte[] tmpOut = new byte[4];
tmpOut[0] = tmpIn[3];
tmpOut[1] = tmpIn[2];
tmpOut[2] = tmpIn[1];
tmpOut[3] = tmpIn[0];
return BitConverter.ToSingle(tmpOut, 0);
}

public static double swap(double input) {
byte[] tmpIn = BitConverter.GetBytes(input);
byte[] tmpOut = new byte[8];
tmpOut[0] = tmpIn[7];
tmpOut[1] = tmpIn[6];
tmpOut[2] = tmpIn[5];
tmpOut[3] = tmpIn[4];
tmpOut[4] = tmpIn[3];
tmpOut[5] = tmpIn[2];
tmpOut[6] = tmpIn[1];
tmpOut[7] = tmpIn[0];
return BitConverter.ToSingle(tmpOut, 0);
}
}
Oct 27 '06 #1
2 13190
O.B. <fu******@bellsouth.netwrote:
Is the following the best way to do byte swapping in C#? Or is there
something like this already built into the framework and I'm just
overlooking it?
I'm not aware of anything like that in the framework. I've got versions
of BitConverter which allow you to specify the endianness, but they're
not quite the same. See
http://www.pobox.com/~skeet/csharp/miscutil
if you're interested.

--
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 28 '06 #2
for 16, 32 and 64 bit integers, IPAddress.HostToNetworkOrder

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
O.B. <fu******@bellsouth.netwrote:
>Is the following the best way to do byte swapping in C#? Or is there
something like this already built into the framework and I'm just
overlooking it?

I'm not aware of anything like that in the framework. I've got versions
of BitConverter which allow you to specify the endianness, but they're
not quite the same. See
http://www.pobox.com/~skeet/csharp/miscutil
if you're interested.

--
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 28 '06 #3

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

Similar topics

9
by: w3r3w0lf | last post by:
hello! I have a following situation: I have a byte array where at a certain location are stored 4 bytes, and these should be "put" into long variable (or any other 4 byte one). ie: byte...
9
by: Jongmin Lee | last post by:
Hi Everybody, I have very simple code snippet to explain my problem. Class "Swap" is construncted in "Main" with two initial variables. Later, "Swap" class is going to swap those two...
4
by: Salvatore Di Fazio | last post by:
Hi guys, I've a ushort variable and I need to copy it in an array of byte. I wrote: m_bMsg = (byte) (p_sTrackNumber & 0xff00 >> 8); m_bMsg = (byte) (p_sTrackNumber & 0x00ff >> 16); but I...
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
6
by: godavemon | last post by:
I need to take floats and dump out their 4 byte hex representation. This is easy with ints with the built in hex function or even better for my purpose def hex( number, size ): s =...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
3
by: velpur | last post by:
Dear friends, ( Suppose we change byte orders. For example, our program reads a file as a character string and converts the byte order. ) // a simple code #define INTEL_CPU #ifdef...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.