Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to convert zero-terminated byte array to string in Go

3 Answers

0 votes
package main

import (
	"fmt"
)

func main() {
   byteArray:= [20]byte{'g', 'o', ' ' , '1', '.', '1', '4', '.', '2'} 
        
   s := string(byteArray[:]) 

   fmt.Println(byteArray) 

   fmt.Println(s)   
}


/*
run:

[103 111 32 49 46 49 52 46 50 0 0 0 0 0 0 0 0 0 0 0]
go 1.14.2

*/

 





answered May 23, 2020 by avibootz
0 votes
package main

import (
	"fmt"
)

func main() {
   byteArray:= [20]byte{'g', 'o', ' ' , '1', '.', '1', '4', '.', '2'} 
        
   s := string(byteArray[:len(byteArray)])

   fmt.Println(byteArray) 

   fmt.Println(s)   
}


/*
run:

[103 111 32 49 46 49 52 46 50 0 0 0 0 0 0 0 0 0 0 0]
go 1.14.2

*/

 





answered May 23, 2020 by avibootz
0 votes
package main

import (
	"fmt"
)

func main() {
   byteArray:= [20]byte{'g', 'o', ' ' , '1', '.', '1', '4', '.', '2'} 
        
   s := fmt.Sprintf("%s", byteArray)

   fmt.Println(byteArray) 

   fmt.Println(s)   
}


/*
run:

[103 111 32 49 46 49 52 46 50 0 0 0 0 0 0 0 0 0 0 0]
go 1.14.2

*/

 





answered May 23, 2020 by avibootz

Related questions

2 answers 96 views
96 views asked Aug 6, 2020 by avibootz
1 answer 73 views
73 views asked Aug 6, 2020 by avibootz
1 answer 74 views
2 answers 52 views
1 answer 126 views
...