Skip to content

Instantly share code, notes, and snippets.

@dmaidon
Last active December 25, 2018 10:21
Show Gist options
  • Save dmaidon/92ac9cc223f1d3dcdb0b8dab43968c79 to your computer and use it in GitHub Desktop.
Save dmaidon/92ac9cc223f1d3dcdb0b8dab43968c79 to your computer and use it in GitHub Desktop.
Generate APRS-Is Passcode using VB.Net
using System;
private static long GenPc(string pc)
{
//strip station designators. ex: "-5" Only the real call sign is used
int stophere = pc.IndexOf("-");
if (stophere > 0)
{
pc = pc.Split('-')[0];
}
//'the Hash must be 29666 (non-negotiable)
long hash = 29666;
for (var j = 1; j <= pc.Length; j++)
{
if (Convert.ToBoolean((j % 2)))
{
hash = hash ^ (Microsoft.VisualBasic.Strings.Asc(pc.ToUpper().Substring(j - 1, 1)) << 8);
}
else
{
hash = hash ^ Microsoft.VisualBasic.Strings.Asc(pc.ToUpper().Substring(j - 1, 1));
}
}
//'mask the high bit so that the result is always positive
return hash & 65535;
}
Private Function GenPC(pc As String) As Long
Dim j = 1
//strip station designators. ex: "-5" Only the real call sign is used
Dim stophere = InStr(pc, "-") - 1
If stophere > 0 Then
pc = pc.Split("-"c)(0)
End If
//the Hash must be 29666 (non-negotiable)
Dim hash As Long = 29666
For j = 1 To pc.Length
If CBool((j Mod 2)) Then
hash = hash Xor (Asc(Mid(pc, j, 1)) << 8)
Else
hash = hash Xor Asc(Mid(pc, j, 1))
End If
Next
//mask the high bit so that the result is always positive
GenPC = hash And 65535
End Function
Private Shared Function GenPc(pc As String) As Long
'strip station designators. ex: "-5" Only the real call sign is used
Dim stophere As Integer = InStr(pc, "-") - 1
If stophere > 0 Then
pc = pc.Split("-"c)(0)
End If
''the Hash must be 29666 (non-negotiable)
Dim hash As Long = 29666
For j = 1 To pc.Length
If CBool(j Mod 2) Then
hash = hash Xor (Asc(Mid(pc.ToUpper(), j, 1)) << 8)
Else
hash = hash Xor Asc(Mid(pc.ToUpper(), j, 1))
End If
Next
''mask the high bit so that the result is always positive
GenPc = hash And 65535
End Function
@dmaidon
Copy link
Author

dmaidon commented Apr 29, 2018

The following code was converted from PHP and Python to VB.Net.
Pass the call sign to the function
ex: Passcode = GenPC("K4DNM") ''pass or convert callsign to upper case
converted from http://blog.eagleflint.com/wp-content/2012/05/APRS-IS_Passcode
and https://github.com/PHP-APRS-PASSCODE

@dmaidon
Copy link
Author

dmaidon commented Dec 22, 2018

Cleaned up the code by removing an unneeded parenthesis and converting the callsign to uppercase within the function.

@dmaidon
Copy link
Author

dmaidon commented Dec 25, 2018

Added C# version of function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment