/dev/Chiheb-Nexus

Conversion avec Golgan

alt text

Ce script peut convertir:

  • Une chaîne de caractère en sa représentation hexadécimale
  • Hexadécimal en chaîne de caractère
  • Une chaîne de caractère en binaire
  • Un entier en sa représentation binaire
  • Un binaire en entier
  • Un entier en sa représentation hexadécimale
  • Une chaîne de caractère en un entier (représentation: ASCII to int)
  • Un binaire en chaîne de caractère
  • Hexadécimal en entier
package main

import (
    "encoding/hex"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func convertStringToHex(inputStr string) string {
    return fmt.Sprintf("%x", inputStr)
}

/* We can also use this method
func convertStringToHex(i int, prefix bool) string{  // note input (int, bool)
      i64 := int64(i)
      if prefix{
          return "0x" + strconv.FormatInt(i64, 16) // with prefix 0x
    } else {
          return strconv.FormatInt(i64, 16)  // without prefix 0x
    }
}
*/

func convertHexToString(inputStr string) string {
    inputStr = strings.Replace(inputStr, "0x", "", 1)
    bs, err := hex.DecodeString(inputStr)
    if err != nil {
        fmt.Printf("Error occurred during the convertion of %s into a String\n", inputStr)
        os.Exit(1)
    }
    return fmt.Sprintf(string(bs))
}

func convertStringToBinary(inputStr string) string {
    res := ""
    for _, s := range inputStr {
        res = fmt.Sprintf("%.8b", s)
    }
    return res
}

func convertIntToBinary(inputStr int, prefix bool) string {
    i64 := int64(inputStr)
    if prefix {
        return fmt.Sprintf("0b%b", i64)
    } else {
        return fmt.Sprintf("%b", i64)
    }
}

/* We can also use this method
func convertIntToBinary(input_int int, prefix bool) string{
  i64 := int64(input_int)
  if prefix{
    return "0b" + strconv.FormatInt(i64, 2)
  } else {
    return strconv.FormatInt(i64, 2)
  }
}
*/

func convertBinaryToInt(inputStr string) int {
    inputStr = strings.Replace(inputStr, "0b", "", 1)
    result, err := strconv.ParseInt(inputStr, 2, 64)
    if err != nil {
        fmt.Printf("Error occurred during the convertion of %s into Int\n", inputStr)
        os.Exit(1)
    }
    return int(result)
}

func convertIntToHex(inputInt int, prefix bool) string {
    i64 := int64(inputInt)
    result := strconv.FormatInt(i64, 16)
    if prefix {
        return "0x" + result
    } else {
        return result
    }
}

func convertStringToInt(inputStr string) int {
    result, err := strconv.Atoi(inputStr)
    if err != nil {
        fmt.Printf("Error occured during the conversion of %s into Int\n", inputStr)
        os.Exit(1)
    }
    return result
}

func convertBinaryToString(inputStr string) string {
    return convertHexToString(convertIntToHex(convertBinaryToInt(inputStr), false))
}

func convertHexToInt(inputStr string) int {
    result, err := strconv.ParseInt(inputStr, 16, 64)
    if err != nil {
        fmt.Printf("Error occurred during the convertion of  %s into a Int\n", inputStr)
        os.Exit(1)
    }
    return int(result)

}

func main() {
    a := "nexus"
    b := "6e65787573"
    i := 1050
    ii := "1010"
    iii := "0110111001100101011110000111010101110011"
    fmt.Printf("string -> hex: %s\n", convertStringToHex(a))
    fmt.Printf("hex -> string: %s\n", convertHexToString(b))
    fmt.Printf("string -> binary: %s\n", convertStringToBinary(a))
    fmt.Printf("int -> binary: %s\n", convertIntToBinary(i, false))
    fmt.Printf("binary -> int: %d\n", convertBinaryToInt(ii))
    fmt.Printf("int -> hex: %s\n", convertIntToHex(i, true))
    fmt.Printf("binary -> string: %s\n", convertBinaryToString(iii))
    fmt.Printf("hex -> int: %d\n", convertHexToInt("abc"))
    fmt.Printf("string -> int: %d\n", convertStringToInt("200"))
}