Golang Program to Convert String Value to Byte Value


In this tutorial, we will learn how to convert string values to byte values using golang programming language.

String Value − A string value is just a sequence of characters, like "abc". A string value is always enclosed in quotes. All types of characters are allowed (even digits, as in "abc123"). Strings can contain any number of characters.

Byte value − The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where memory savings matters.

Example-1: Go Program Code to Convert String Value to Byte Value Using []byte(string)

Syntax

[]byte(string)

INPUT = We pass a string to the byte slice.

OUTPUT = the bytes of all the characters of the string is returned

Algorithm

  • STEP 1 − Import the package fmt

  • STEP 2 − Start function main ()

  • STEP 3 − Declare a string value

  • STEP 4 − Convert string value to byte value

  • STEP 5 − Print the slice of byte values on the screen

Example

package main

//import format package
import (
   "fmt"
)

// start the function main ()
// program execution starts here
func main() {
   fmt.Println("GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE")
   //declare string
   s := "SWIFT"
   fmt.Println("String value entered:\n",s)
   
   fmt.Println("After converting to Bytes value:")
   //convert string to bytes
   b := []byte(s)

   //display the slice of bytes using fmt.Println () function
   fmt.Println(b)

}

Output

GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE
String value entered:
   SWIFT
After converting to Bytes value:
[83 87 73 70 84]

Description

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt.

  • Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Declare the string value which is to be converted to a byte value. In code line 16: we declare and initialize a string with the variable names.

  • Convert the string value to the byte value using the []byte(string) function. In code line 19: we pass the string s to the byte slice to convert it into a slice of bytes. We assign this slice of bytes to variable b

  • Finally we print the slice of bytes on the screen using the function fmt.Println () which formats using the default formats for its operands and writes to standard output.

  • In this example we used the byte () function. A byte is an 8-bit unsigned int. The byte () function takes a string as input and returns the array.

Example-2: Go Program Code to Convert String Value to Byte Value Using Make () Function And Copy () Function

Syntax

func make(t Type, size ...IntegerType) Type
func copy(dst, src []Type) int

Algorithm

  • STEP 1 − Import the package fmt

  • STEP 2 − Start function main ()

  • STEP 3 − Declare a string value and initialize it

  • STEP 4 − Convert string value to byte value using make () function

  • STEP 5 − Use the built-in copy () function to copy the elements the to destination slice

  • STEP 6 − Print the slice of byte values on the screen

Example

package main

//import format package

import (
   "fmt"
)

// start the function main ()
// program execution starts here
func main() {
   fmt.Println("GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE")
   // Declare a string variable
   var strToConvert string
   // initialize the string variable to be converted
   strToConvert = "JEEPERS CREEPERS"
   fmt.Println("Entered value =",strToConvert)
   // convert the string value to byte value using
   // the function make ()
   byteString := make([]byte, 10)

   // The built-in copy function copies elements
   // into a destination slice dst from a source slice src
   copy(byteString, strToConvert)
   fmt.Println("After conversion of the string value to byte:")

   // print the result using fmt.Println () function
   fmt.Println(byteString)
}

Output

GOLANG PROGRAM TO CONVERT STRING VALUE TO BYTE VALUE
Entered value = JEEPERS CREEPERS
After conversion of the string value to byte:
[74 69 69 80 69 82 83 32 67 82]

Description

  • In the above program, we first declare the package’s main.

  • We imported the fmt package that includes the files of package fmt.

  • Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Declare the string value to be converted and initialize it.

  • Convert the string value to byte value using the built-in function make (). The make () function is used for slices, maps, or channels. the make () allocates memory on the heap and initializes and puts zero or empty strings into values and make () returns the same type as its argument.

  • Next, we use the built-in function copy () to copy the elements into a destination slice dst from a source slice. It returns the number of elements copied, which will be the minimum of len(dst) and len(src). The result does not depend on whether the arguments overlap.

  • Finally we print the slice of byte values on the screen using the function fmt.Println () which formats using the default formats for its operands and writes to standard output

Conclusion

In the above two examples, we have successfully complied and executed the golang program code to convert the string values to byte values. In the first example, we have used the []byte(string) function to convert the string value to byte values.

In the second example we have used two built-in functions of golang, make () function and copy () function to convert the string values to byte values.

Updated on: 07-Oct-2023

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements