Autor Beitrag
CASS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 160

WIN XP
D7 Enterprise
BeitragVerfasst: Fr 24.08.07 10:43 
Hallo Leute,

kann mir einer sagen wie ich 2 Bytes in einen Short umwandeln kann?

Hab folgendes Problem das ich ein Byte Array vorliegen habe, aus diesem möchte ich 2 Bytes zusammenfassen und mir dann den Wert der 2 Bytes als short ausgeben.

Hat da jemand ne Lösung ???

Grüße Benny


Moderiert von user profile iconChristian S.: Topic aus Allgemeine .NET-Fragen verschoben am Fr 24.08.2007 um 12:16
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 24.08.07 12:16 
Müsste eigentlich über die BitConverter-Klasse gehen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 24.08.07 14:00 
Oder so:
ausblenden C#-Quelltext
1:
2:
3:
4:
static ushort ToUInt16(byte hi, byte lo)
{
  return (ushort)(hi << 8 | lo);
}
CASS Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 160

WIN XP
D7 Enterprise
BeitragVerfasst: Mo 03.09.07 10:40 
Danke für die Antworten,

der Bitconverter funktioniert wunderbar ;)...

Könnt ihr mir evtl. trotzdem erklären was Khabarakh da genau macht? Da blick ich nicht ganz durch. Hab auch den Operator << in C# noch nicht benutzt.

Grüße und Danke für die Antwort,
Cass
Greenberet
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 339
Erhaltene Danke: 20

Win 10
C# (VS 2012), C++ (VS 2012/GCC), PAWN(Notepad++), Java(NetBeans)
BeitragVerfasst: Mo 03.09.07 12:13 
(x << y)
die bitfelder von x werden um y stellen verschoben
bsp: 7 << 8
00000000 00000111 wird zu 00000111 00000000 = 1792
auf diese weise hat jetzt das niedrigere byte ( lo ) platz in den letzten 8 stellen. durch das arithm. OR( = '|' ) wird dies eingebunden.

(7<<8) | 43
00000111 00000000 wird durch | 43 zu
00000111 00101011 = 1835