# Read string from binary

**URL:** <https://forum.golangbridge.org/t/read-string-from-binary/21550>\
**Category:** Getting Help\
**Created:** [December 2, 2020, 12:18pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550 "2020-12-02T12:18:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![devdavid](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@devdavid](https://forum.golangbridge.org/u/devdavid)\
**Post date:** [December 2, 2020, 12:18pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/1 "2020-12-02T12:18:27Z")

</div>

Hi, all!  
I am trying to read my binary file to grab string from different positions:  
like this this

 ![print](https://forum.golangbridge.org/uploads/default/original/2X/3/3950015176f91e314721c9e2a7ec5af8e0546aaa.png)

```
func main() {
f, err := os.Open("binaryfile.bin")
if err != nil {
	log.Fatalln(err)
}
defer f.Close()
var val float64
err = binary.Read(f, binary.LittleEndian, &val)
if err != nil {
	log.Fatalln(err)
}
fmt.Println(pi)}

```

I’m completely lost

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [December 2, 2020, 12:50pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/2 "2020-12-02T12:50:33Z")

</div>

What you have looks like it will read the first 8 bytes of a file and interpret it as a float64. Based on your screenshot, it looks like you actually want the data at offset 14 (if I’m counting the dots correctly on my phone screen right now). Before you call `binary.Read`, you should call `f.Seek` to set the position of the file to the offset of the beginning of the data you want to read, and _then_ call `binary.Read`.

You’ll need to do the same seeking for the strings you want, too, but you won’t need `binary.Read`, you could just slice the bytes that you read and then convert to a string after. Are the strings always the same length or do you need to inspect the file at some other offset to determine the string’s length?

---

<div class="post-metadata">

**Author:** ![devdavid](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@devdavid](https://forum.golangbridge.org/u/devdavid)\
**Post date:** [December 2, 2020, 2:50pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/3 "2020-12-02T14:50:37Z")

</div>

Thank you for answer,  
My idea is to find the first ip and read the data after that because the length between is the same like this [pastebin hex](https://pastebin.com/ZEMf0YCf)

I’am beginner and newbie with Golang

---

<div class="post-metadata">

**Author:** ![devdavid](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@devdavid](https://forum.golangbridge.org/u/devdavid)\
**Post date:** [December 2, 2020, 4:55pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/4 "2020-12-02T16:55:13Z")

</div>

my new code

```
func main() {
	file, err := os.Open("data.bin")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	o2, err := file.Seek(110, io.SeekCurrent) <---- First occurrence 
	byteSlice := make([]byte, 32)
	bytesRead, err := file.Read(byteSlice)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("IP: %s\n", byteSlice)
}
```

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [December 4, 2020, 3:28pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/5 "2020-12-04T15:28:16Z")

</div>

Is this working for you or do you still need help?

---

<div class="post-metadata">

**Author:** ![devdavid](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@devdavid](https://forum.golangbridge.org/u/devdavid)\
**Post date:** [December 4, 2020, 8:47pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/6 "2020-12-04T20:47:12Z")

</div>

Its working but my code its not clean 🙁

```
    file, err := os.Open("data.bin")
check(err)
defer file.Close()

o1, err := file.Seek(110, 0) <--- first position
check(err)
_ip := make([]byte, 15)
ipRead, err := file.Read(_ip)
clean_ip := strings.ReplaceAll(string(_ip[:]), " ", " ")

o2, err := file.Seek(21, io.SeekCurrent)
check(err)
_user := make([]byte, 15)
userRead, err := file.Read(_user)
clean_user := strings.ReplaceAll(string(_user[:]), " ", " ")

o3, err := file.Seek(532, 0) <---- Second position
check(err)
_pwd := make([]byte, 20)
pwdRead, err := file.Read(_pwd)
clean_pwd := strings.ReplaceAll(string(_pwd[:]), " ", " ")
```

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [December 4, 2020, 9:20pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/7 "2020-12-04T21:20:15Z")

</div>

You could put the repetitive parts into a function: [https://play.golang.org/p/iflkAKaXbuh](https://play.golang.org/p/iflkAKaXbuh)

I’m not sure what you’re going for with `strings.ReplaceAll(string(_ip[:]), " ", " ")`: This replaces all the spaces with spaces, so essentially a no-op.

---

<div class="post-metadata">

**Author:** ![petrus](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@petrus](https://forum.golangbridge.org/u/petrus)\
**Post date:** [December 4, 2020, 10:14pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/8 "2020-12-04T22:14:00Z")

</div>

> [@skillian](#):
>
> I’m not sure what you’re [[devdavid](https://forum.golangbridge.org/u/devdavid)] going for with `strings.ReplaceAll(string(_ip[:]), " ", " ")` : This replaces all the spaces with spaces, so essentially a no-op.

My guess, from looking at the file dump, is that they are C strings. Therefore, it’s an attempt to replace NUL by space.

A better solution would be a CToGoString function:

```
package main

import "fmt"

func CToGoString(c []byte) string {
	n := -1
	for i, b := range c {
		if b == 0 {
			break
		}
		n = i
	}
	return string(c[:n+1])
}

func main() {
	// IPV4
	c := []byte("\x34\x31\x2E\x31\x39\x31\x2E\x39\x37\x2E\x36\x32\x00\x00\x00")
	fmt.Printf("%d %q\n", len(c), c)
	g := CToGoString(c)
	fmt.Printf("%d %q\n", len(g), g)
	c = []byte("1.1.1.1\x00\x00\x00\x00\x00\x00\x00\x00")
	fmt.Printf("%d %q\n", len(c), c)
	g = CToGoString(c)
	fmt.Printf("%d %q\n", len(g), g)
	c = []byte("111.111.111.111")
	fmt.Printf("%d %q\n", len(c), c)
	g = CToGoString(c)
	fmt.Printf("%d %q\n", len(g), g)
	c = make([]byte, 15)
	fmt.Printf("%d %q\n", len(c), c)
	g = CToGoString(c)
	fmt.Printf("%d %q\n", len(g), g)
}

```

> **[Go Playground - The Go Programming Language](https://go.dev/play/p/eOROw_ZIUj9)**

```
15 "41.191.97.62\x00\x00\x00"
12 "41.191.97.62"
15 "1.1.1.1\x00\x00\x00\x00\x00\x00\x00\x00"
7 "1.1.1.1"
15 "111.111.111.111"
15 "111.111.111.111"
15 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
0 ""

```

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [March 4, 2021, 10:14pm UTC](https://forum.golangbridge.org/t/read-string-from-binary/21550/9 "2021-03-04T22:14:04Z")

</div>

Closed on March 4, 2021, 10:14pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
