Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
module bcopy

// <Snippet2>
open System

// Display the individual bytes in the array in hexadecimal.
let displayArray (arr: 'a []) (name: string) =
Console.WindowWidth <- 120
printf $"%11s{name}:"
for i = 0 to arr.Length - 1 do
let bytes =
match box arr with
| :? array<int64> ->
BitConverter.GetBytes(box arr[i] :?> int64)
| _ ->
BitConverter.GetBytes(box arr[i] :?> int16)
for byteValue in bytes do
printf $" %02X{byteValue}"
printfn ""

// Display the individual array element values in hexadecimal.
let inline displayArrayValues (arr: ^a []) name =
// Get the length of one element in the array.
let elementLength = Buffer.ByteLength arr / arr.Length
printf $"%11s{name}:"
for value in arr do
printf " %0*X" (2 * elementLength) value
printfn ""

// These are the source and destination arrays for BlockCopy.
let src =
[| 258s; 259s; 260s; 261s; 262s; 263s; 264s
265s; 266s; 267s; 268s; 269s; 270s |]

let dest =
[| 17L; 18L; 19L; 20L |]

// Display the initial value of the arrays in memory.
printfn "Initial values of arrays:"
printfn " Array values as Bytes:"
displayArray src "src"
displayArray dest "dest"
printfn " Array values:"
displayArrayValues src "src"
displayArrayValues dest "dest"
printfn ""

// Copy bytes 5-10 from source to index 7 in destination and display the result.
Buffer.BlockCopy(src, 5, dest, 7, 6)
printfn "Buffer.BlockCopy(src, 5, dest, 7, 6 )"
printfn " Array values as Bytes:"
displayArray src "src"
displayArray dest "dest"
printfn " Array values:"
displayArrayValues src "src"
displayArrayValues dest "dest"
printfn ""

// Copy bytes 16-20 from source to index 22 in destination and display the result.
Buffer.BlockCopy(src, 16, dest, 22, 5)
printfn "Buffer.BlockCopy(src, 16, dest, 22, 5)"
printfn " Array values as Bytes:"
displayArray src "src"
displayArray dest "dest"
printfn " Array values:"
displayArrayValues src "src"
displayArrayValues dest "dest"
printfn ""

// Copy overlapping range of bytes 4-10 to index 5 in source.
Buffer.BlockCopy(src, 4, src, 5, 7)
printfn "Buffer.BlockCopy(src, 4, src, 5, 7)"
printfn " Array values as Bytes:"
displayArray src "src"
displayArray dest "dest"
printfn " Array values:"
displayArrayValues src "src"
displayArrayValues dest "dest"
printfn ""

// Copy overlapping range of bytes 16-22 to index 15 in source.
Buffer.BlockCopy(src, 16, src, 15, 7)
printfn "Buffer.BlockCopy(src, 16, src, 15, 7)"
printfn " Array values as Bytes:"
displayArray src "src"
displayArray dest "dest"
printfn " Array values:"
displayArrayValues src "src"
displayArrayValues dest "dest"


// The example displays the following output:
// Initial values of arrays:
// Array values as Bytes:
// src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
// dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
// Array values:
// src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
// dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014
//
// Buffer.BlockCopy(src, 5, dest, 7, 6 )
// Array values as Bytes:
// src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
// dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
// Array values:
// src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
// dest: 0100000000000011 0000000701060105 0000000000000013 0000000000000014
//
// Buffer.BlockCopy(src, 16, dest, 22, 5)
// Array values as Bytes:
// src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
// dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
// Array values:
// src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
// dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B
//
// Buffer.BlockCopy(src, 4, src, 5, 7)
// Array values as Bytes:
// src: 02 01 03 01 04 04 01 05 01 06 01 07 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
// dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
// Array values:
// src: 0102 0103 0404 0501 0601 0701 0108 0109 010A 010B 010C 010D 010E
// dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B
//
// Buffer.BlockCopy(src, 16, src, 15, 7)
// Array values as Bytes:
// src: 02 01 03 01 04 04 01 05 01 06 01 07 08 01 09 0A 01 0B 01 0C 01 0D 0D 01 0E 01
// dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
// Array values:
// src: 0102 0103 0404 0501 0601 0701 0108 0A09 0B01 0C01 0D01 010D 010E
// dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module buffer

//<Snippet1>
open System

// Display the array elements from right to left in hexadecimal.
let displayArray (arr: int16 []) =
printf " arr:"
for i = arr.Length - 1 downto 0 do
printf $" {arr[i]:X4}"
printfn ""

// This array is to be modified and displayed.
let arr =
[| 258s; 259s; 260s; 261s; 262s; 263s; 264s
265s; 266s; 267s; 268s; 269s; 270s; 271s |]

printfn "This example of the Buffer class methods generates the following output.\nNote: The array is displayed from right to left.\n"
printfn "Initial values of array:\n"

// Display the initial array values and ByteLength.
displayArray arr
printfn $"\nBuffer.ByteLength(arr): {Buffer.ByteLength arr}"

// Copy a region of the array; set a byte within the array.
printfn "\nCall these methods: \n Buffer.BlockCopy(arr, 5, arr, 16, 9),\n Buffer.SetByte(arr, 7, 170).\n"

Buffer.BlockCopy(arr, 5, arr, 16, 9)
Buffer.SetByte(arr, 7, 170uy)

// Display the array and a byte within the array.
printfn "Final values of array:\n"
displayArray arr
printfn $"\nBuffer.GetByte(arr, 26): {Buffer.GetByte(arr, 26)}"


// This example of the Buffer class methods generates the following output.
// Note: The array is displayed from right to left.
//
// Initial values of array:
//
// arr: 010F 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
//
// Buffer.ByteLength(arr): 28
//
// Call these methods:
// Buffer.BlockCopy(arr, 5, arr, 16, 9),
// Buffer.SetByte(arr, 7, 170).
//
// Final values of array:
//
// arr: 010F 0101 0801 0701 0601 0501 0109 0108 0107 0106 AA05 0104 0103 0102
//
// Buffer.GetByte(arr, 26): 15
//</Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="buffer.fs" />
<Compile Include="overlap1.fs" />
<Compile Include="bcopy.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module overlap1

open System

let copyUp () =
// <Snippet3>
let intSize = 4
let arr = [| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
Buffer.BlockCopy(arr, 0 * intSize, arr, 3 * intSize, 4 * intSize)
for value in arr do
printf $"{value} "

// The example displays the following output:
// 2 4 6 2 4 6 8 16 18 20
// </Snippet3>

let copyDown () =
// <Snippet4>
let intSize = 4
let arr = [| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
Buffer.BlockCopy(arr, 3 * intSize, arr, 0 * intSize, 4 * intSize)
for value in arr do
printf $"{value} "

// The example displays the following output:
// 8 10 12 14 10 12 14 16 18 20
// </Snippet4>

copyUp ()
printfn ""
copyDown ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module bytelength

//<Snippet1>
open System

let print obj1 obj2 obj3 obj4 =
printfn $"{obj1,10}{obj2,20}{obj3,9}{obj4,12}"

let arrayInfo (arr: Array) name =
let byteLength = Buffer.ByteLength arr

// Display the array name, type, Length, and ByteLength.
print name (arr.GetType()) arr.Length byteLength

let bytes = [| 1uy; 2uy; 3uy; 4uy; 5uy; 6uy; 7uy; 8uy; 9uy; 0uy |]
let bools = [| true; false; true; false; true |]
let chars = [| ' '; '$'; '\"'; 'A'; '{' |]
let shorts = [| 258s; 259s; 260s; 261s; 262s; 263s |]
let singles = [| 1f; 678f; 2.37E33f; 0.00415f; 8.9f |]
let doubles = [| 2E-22; 0.003; 4.4E44; 555E55 |]
let longs = [| 1L; 10L; 100L; 1000L; 10000L; 100000L |]

printfn "This example of the Buffer.ByteLength(Array) \nmethod generates the following output.\n"
print "Array name" "Array type" "Length" "ByteLength"
print "----------" "----------" "------" "----------"

// Display the Length and ByteLength for each array.
arrayInfo bytes "bytes"
arrayInfo bools "bools"
arrayInfo chars "chars"
arrayInfo shorts "shorts"
arrayInfo singles "singles"
arrayInfo doubles "doubles"
arrayInfo longs "longs"


// This example of the Buffer.ByteLength(Array)
// method generates the following output.
//
// Array name Array type Length ByteLength
// ---------- ---------- ------ ----------
// bytes System.Byte[] 10 10
// bools System.Boolean[] 5 5
// chars System.Char[] 5 10
// shorts System.Int16[] 6 12
// singles System.Single[] 5 20
// doubles System.Double[] 4 32
// longs System.Int64[] 6 48
//</Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="bytelength.fs" />
<Compile Include="setbyte.fs" />
<Compile Include="getbyte.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module getbyte

//<Snippet3>
open System

let print obj1 obj2 obj3 obj4 = printfn $"{obj1,10}{obj2,10}{obj3,9} {obj4}"

// Display the array contents in hexadecimal.
let inline displayArray (arr: ^a []) name =
// Get the array element width; format the formatting string.
let elemWidth = Buffer.ByteLength arr / arr.Length

// Display the array elements from right to left.
printf $"{name,5}:"
for i = arr.Length - 1 downto 0 do
printf " %0*X" (2 * elemWidth) arr[i]
printfn ""

let arrayInfo (arr: Array) name index =
let value = Buffer.GetByte(arr, index)

// Display the array name, index, and byte to be viewed.
print name index value $"0x{value:X2}"

// These are the arrays to be viewed with GetByte.
let longs =
[| 333333333333333333L; 666666666666666666L; 999999999999999999L |]
let ints =
[| 111111111; 222222222; 333333333; 444444444; 555555555 |]

printfn "This example of the Buffer.GetByte(Array, int) \nmethod generates the following output.\nNote: The arrays are displayed from right to left.\n"
printfn " Values of arrays:\n"

// Display the values of the arrays.
displayArray longs "longs"
displayArray ints "ints"
printfn ""

print "Array" "index" "value" ""
print "-----" "-----" "-----" "----"

// Display the Length and ByteLength for each array.
arrayInfo ints "ints" 0
arrayInfo ints "ints" 7
arrayInfo ints "ints" 10
arrayInfo ints "ints" 17
arrayInfo longs "longs" 0
arrayInfo longs "longs" 6
arrayInfo longs "longs" 10
arrayInfo longs "longs" 17
arrayInfo longs "longs" 21


// This example of the Buffer.GetByte( Array, int )
// method generates the following output.
// Note: The arrays are displayed from right to left.
//
// Values of arrays:
//
// longs: 0DE0B6B3A763FFFF 094079CD1A42AAAA 04A03CE68D215555
// ints: 211D1AE3 1A7DAF1C 13DE4355 0D3ED78E 069F6BC7
//
// Array index value
// ----- ----- ----- ----
// ints 0 199 0xC7
// ints 7 13 0x0D
// ints 10 222 0xDE
// ints 17 26 0x1A
// longs 0 85 0x55
// longs 6 160 0xA0
// longs 10 66 0x42
// longs 17 255 0xFF
// longs 21 182 0xB6
//</Snippet3>
Loading