Buchempfehlung
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
"Der" Petzold, das über 1000 Seiten starke Standardwerk zum Win32-API - besonders nützlich u. a. bei der GUI-Programmierung in FreeBASIC! [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Code-Beispiel

Code-Beispiele » Kleine Helferlein

Glue und Split Funktionen

Lizenz:Erster Autor:Letzte Bearbeitung:
k. A.MitgliedSannaj 19.05.2012

Die hier vorgestellten Funktionen glue ("klebe"), crop ("stutze") und split("spalte") ermöglichen es dem Programmierer, zwei Integer zu einem Großen zusammen zu schweißen und wieder zu trennen. Dies ist nützlich, wenn man eine Funktion bauen möchte, die zwei Werte zurückgibt. (Z.B. einen Wert und einen Fehlercode). Innerhalb der Funktion werden beide Werte zusammengeschweißt, sodass sie anschließend aufgespaltet werden können.

Es gibt folgende Funktionen:
c = glue(a, b) - "Klebt" zwei gleich große Integer-Variablen zu einer doppelt so großen zusammen. (a ist der high-part, b der low-part)
b = crop(a, c) - Schreibt den high-part von c in die Variable a und gibt den low-part zurück.
a = crop(b, c) - Schreibt den low-part von c in die Variable b und gibt den high-part zurück.

' Glue and Split operators

' These operators could be useful if you want to return e.g. two informations from a function.
' You can glue them together within the function and then split them afterwards.

' 1. glue - Sticks together two datas
' 2. crop - Removes the high-part and returns the lower one
' 3. split - Removes the low-part and returns the higher one

declare function glue overload(uppB as byte, downB as byte) as short
declare function glue(uppS as short, downS as short) as integer
declare function glue(uppI as integer, downI as integer) as longint

declare function crop overload(byref uppB as byte, boxS as short) as byte
declare function crop(byref uppS as short, boxI as integer) as short
declare function crop(byref uppI as integer, boxLS as longint) as integer

declare function split overload(byref downB as byte, boxS as short) as byte
declare function split(byref downS as short, boxI as integer) as short
declare function split(byref downI as integer, boxLS as longint) as integer

function glue(uppB as byte, downB as byte) as short
    return cast(short, uppB) shl 8 or cast(ushort, downB)
end function

function glue(uppS as short, downS as short) as integer
    return cast(integer, uppS) shl 16 or cast(uinteger, downS)
end function

function glue(uppI as integer, downI as integer) as longint
    return cast(longint, uppI) shl 32 or cast(uinteger, downI)
end function

dim as integer a = &h01234567, b = &h89abcdef
dim as longint c = &hBADC0FFEE0DDF00D

print hex(glue(b, a),16)

'-----------------------------------------------------------------------

function crop(byref uppB as byte, boxS as short) as byte
    :function = cast(byte, boxS)
    uppB = cast(byte, boxS shr 8)
end function

function crop(byref uppS as short, boxI as integer) as short
    :function = cast(short, boxI)
    uppS = cast(short, boxI shr 16)
end function

function crop(byref uppI as integer, boxLS as longint) as integer
    :function = cast(integer, boxLS)
    uppI = cast(integer, boxLS shr 32)
end function

'-----------------------------------------------------------------------

function split(byref downB as byte, boxS as short) as byte
    downB = cast(byte, boxS)
    return cast(byte, boxS shr 8)
end function

function split(byref downS as short, boxI as integer) as short
    downS = cast(short, boxI)
    return cast(short, boxI shr 16)
end function

function split(byref downI as integer, boxLS as longint) as integer
    downI = cast(integer, boxLS)
    return cast(integer, boxLS shr 32)
end function

Zusätzliche Informationen und Funktionen
  • Das Code-Beispiel wurde am 15.01.2012 von MitgliedSannaj angelegt.
  • Die aktuellste Version wurde am 19.05.2012 von Redakteurytwinky gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen