parent
dd4f25988f
commit
e6b7a93a33
@ -1,5 +1,6 @@
|
|||||||
/locbackup
|
/locbackup
|
||||||
/numberstation
|
/numberstation
|
||||||
|
/nsencode
|
||||||
/build
|
/build
|
||||||
/bin
|
/bin
|
||||||
|
|
||||||
|
@ -0,0 +1,90 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Define the shift and hex flags
|
||||||
|
shift := flag.Int("s", 0, "Shift value for alphabet positions (can be negative)")
|
||||||
|
useHex := flag.Bool("x", false, "Encode numbers in hexadecimal")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Check if input is being piped or passed as an argument
|
||||||
|
stat, _ := os.Stdin.Stat()
|
||||||
|
if (stat.Mode() & os.ModeCharDevice) != 0 {
|
||||||
|
// No input, display help text
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read input from stdin
|
||||||
|
inputBytes, err := ioutil.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading input:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
input := strings.TrimSpace(string(inputBytes))
|
||||||
|
|
||||||
|
// Convert input to uppercase and prepare the result slice
|
||||||
|
input = strings.ToUpper(input)
|
||||||
|
var result []string
|
||||||
|
var currentNumber string
|
||||||
|
|
||||||
|
for _, char := range input {
|
||||||
|
if unicode.IsLetter(char) {
|
||||||
|
// If there was a number being processed, add it to the result first
|
||||||
|
if currentNumber != "" {
|
||||||
|
appendNumber(&result, currentNumber, *useHex)
|
||||||
|
currentNumber = ""
|
||||||
|
}
|
||||||
|
// Get the position of the character in the alphabet (A = 1, B = 2, ..., Z = 26)
|
||||||
|
position := int(char - 'A' + 1)
|
||||||
|
// Apply the shift, wrapping around the alphabet
|
||||||
|
shiftedPosition := (position + *shift - 1) % 26
|
||||||
|
if shiftedPosition < 0 {
|
||||||
|
shiftedPosition += 26
|
||||||
|
}
|
||||||
|
shiftedPosition += 1
|
||||||
|
|
||||||
|
// Append the shifted position to the result slice
|
||||||
|
result = append(result, fmt.Sprintf("%02d", shiftedPosition))
|
||||||
|
} else if unicode.IsDigit(char) {
|
||||||
|
// Collect digits to process as a whole number
|
||||||
|
currentNumber += string(char)
|
||||||
|
} else {
|
||||||
|
// If there was a number being processed and now a non-digit/non-letter appears, append it
|
||||||
|
if currentNumber != "" {
|
||||||
|
appendNumber(&result, currentNumber, *useHex)
|
||||||
|
currentNumber = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's any remaining number at the end of the input, append it
|
||||||
|
if currentNumber != "" {
|
||||||
|
appendNumber(&result, currentNumber, *useHex)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join the result slice into a space-separated string and print it
|
||||||
|
fmt.Println(strings.Join(result, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendNumber appends a formatted number to the result slice
|
||||||
|
func appendNumber(result *[]string, number string, useHex bool) {
|
||||||
|
if useHex {
|
||||||
|
num, err := strconv.Atoi(number)
|
||||||
|
if err == nil {
|
||||||
|
*result = append(*result, fmt.Sprintf("0x%X", num))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*result = append(*result, "0d"+number)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue