Click here to Skip to main content
15,901,035 members
Home / Discussions / C#
   

C#

 
Questionconfiguaration file Pin
pavanteja3-Aug-07 20:11
pavanteja3-Aug-07 20:11 
AnswerRe: configuaration file Pin
Christian Graus3-Aug-07 21:29
protectorChristian Graus3-Aug-07 21:29 
AnswerRe: configuaration file Pin
Manoj Kumar Rai4-Aug-07 0:34
professionalManoj Kumar Rai4-Aug-07 0:34 
QuestionCopy into Excel textbox. Not working. Via C#.net Pin
ku3_ku3-Aug-07 18:45
ku3_ku3-Aug-07 18:45 
Questioncall webservice design Pin
arkiboys3-Aug-07 17:10
arkiboys3-Aug-07 17:10 
QuestionBuilding Webcontrols from XML Pin
tranzformerz3-Aug-07 12:12
tranzformerz3-Aug-07 12:12 
AnswerRe: Building Webcontrols from XML Pin
tranzformerz3-Aug-07 12:35
tranzformerz3-Aug-07 12:35 
QuestionConversion errors in convert endian routine Pin
Lilith.C3-Aug-07 11:17
Lilith.C3-Aug-07 11:17 
Rather new to C# so I'm still getting used to the foibles. I'm working on a personal MIDI project and need to convert some ints and shorts to big endian before output. After a few trials I got the following to work for UInt32.


private UInt32 CVTEndian(UInt32 u)
{
byte[] numb = { Convert.ToByte((u >> 24) & 0xff),
Convert.ToByte((u >> 24) & 0xff),
Convert.ToByte((u >> 8) & 0xff),
Convert.ToByte(u & 0xff) };
return Convert.ToUInt32( numb[3] << 24) |
Convert.ToUInt32(numb[2] << 16) |
Convert.ToUInt32(numb[1] << 8) |
Convert.ToUInt32( numb[0]);
}

However, in trying what should be the same but shorter and easier for UInt16 I came up with the following code.

private UInt16 CVTEndian(UInt16 u)
{
UInt16 temp;
byte[] numb = {Convert.ToByte((u >> 8) & 0xff),
Convert.ToByte(u & 0xff)};
temp = (Convert.ToUInt16(numb[1]) << 8) | Convert.ToUInt16(numb[0]);
return temp;
}

Mind you, I originally just returned the expression. Assigning to a temp was an attempt to narrow down the problem and an extra set of parenz is there to make sure I wasn't screwing up on order or precedence.

AAR, the above assignment to temp gives me

Cannot explicitly convert type 'int' to 'ushort' An explicit conversion exists

It's not really any different than the code in the other method. It compiles fine if I leave off the expression on the right side of the or operation but that doesn't give me the proper results.

I'd be happy with an explanation of where I'm going wrong or a better method of converting endian, especially if it's part of the .NET paradigm.



TIA,
Lilith
AnswerRe: Conversion errors in convert endian routine [modified] Pin
Luc Pattyn3-Aug-07 11:45
sitebuilderLuc Pattyn3-Aug-07 11:45 
GeneralRe: Conversion errors in convert endian routine Pin
Lilith.C3-Aug-07 13:22
Lilith.C3-Aug-07 13:22 
AnswerRe: Conversion errors in convert endian routine Pin
MidwestLimey3-Aug-07 11:46
professionalMidwestLimey3-Aug-07 11:46 
GeneralRe: Conversion errors in convert endian routine Pin
Lilith.C3-Aug-07 13:28
Lilith.C3-Aug-07 13:28 
GeneralRe: Conversion errors in convert endian routine Pin
Luc Pattyn3-Aug-07 13:56
sitebuilderLuc Pattyn3-Aug-07 13:56 
GeneralRe: Conversion errors in convert endian routine Pin
Lilith.C3-Aug-07 14:46
Lilith.C3-Aug-07 14:46 
AnswerRe: Conversion errors in convert endian routine Pin
Robert Surtees5-Aug-07 17:03
Robert Surtees5-Aug-07 17:03 
GeneralRe: Conversion errors in convert endian routine Pin
Lilith.C5-Aug-07 18:00
Lilith.C5-Aug-07 18:00 
QuestionUrgent help required on Data grid Pin
ashu.awasthi3-Aug-07 10:24
ashu.awasthi3-Aug-07 10:24 
AnswerRe: Urgent help required on Data grid Pin
T.EDY3-Aug-07 15:28
T.EDY3-Aug-07 15:28 
AnswerRe: Urgent help required on Data grid Pin
Aharon Goldberg3-Aug-07 16:10
Aharon Goldberg3-Aug-07 16:10 
QuestionPrincipalPermissionAttribute not working Pin
Milan20073-Aug-07 10:12
Milan20073-Aug-07 10:12 
QuestionRe: PrincipalPermissionAttribute not working Pin
Milan20074-Aug-07 3:50
Milan20074-Aug-07 3:50 
QuestionCA1033: Interface methods should be callable by child types Pin
Scott Dorman3-Aug-07 9:49
professionalScott Dorman3-Aug-07 9:49 
QuestionFormatting Bar Pin
.NET- India 3-Aug-07 9:24
.NET- India 3-Aug-07 9:24 
AnswerRe: Formatting Bar Pin
Malcolm Smart3-Aug-07 12:36
Malcolm Smart3-Aug-07 12:36 
QuestionPrint Document Pin
RolfTorgersen3-Aug-07 8:51
RolfTorgersen3-Aug-07 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.