String to Byte conversion

690 views
Skip to first unread message

Stéphane phenetas

unread,
Aug 6, 2015, 7:01:03 PM8/6/15
to golang-nuts
Hello,

I am trying to convert from String to Byte, here is my code :

ip0:=byte(sliceIPDestination[0])

And the error message :

./main.go:325: cannot convert sliceIPDestination[0] (type string) to type byte


Probably something obvious I have missed ?

Thank you.

Jonathan

unread,
Aug 6, 2015, 7:21:50 PM8/6/15
to golang-nuts
Use []byte, not byte

Stéphane phenetas

unread,
Aug 6, 2015, 7:28:39 PM8/6/15
to golang-nuts
Thank you, but I need it to be byte, not []byte.

Or maybe I could convert to []byte and then make an other conversion to byte ?

Dan Kortschak

unread,
Aug 6, 2015, 7:37:20 PM8/6/15
to Stéphane phenetas, golang-nuts
Do you want the first byte of the string, then s[0]? If you want more
than that can you more clearly explain what you are trying to do?

Maybe you are trying to parse a string representation of a number to a
byte? Then strconv.Atoi or strconv.ParseInt and type convert the
int/int64 to a byte.

Stéphane phenetas

unread,
Aug 6, 2015, 7:48:14 PM8/6/15
to golang-nuts, phen...@gmail.com
Yes I will try to explain more precisely.

I have an IP address stored in a string variable. The IP is like "X.X.X.X" where X are numbers.
I use strings.Split to split the IP and it returns me a []string with 4 elements inside corresponding to the 4 numericals of the IP.

Then I need to send these numericals in an other function who takes byte in entry.

Example :

at the beginning I have variable := "1.2.3.4" which is a string, and at the end I need to send 1 and 2 and 3 and 4 in a function like this myFunction(a byte, b byte, c byte, d byte)

Dan Kortschak

unread,
Aug 6, 2015, 7:52:14 PM8/6/15
to Stéphane phenetas, golang-nuts
Use http://golang.org/pkg/net/#ParseIP?

On Thu, 2015-08-06 at 16:48 -0700, Stéphane phenetas wrote:
> Yes I will try to explain more precisely.
>
> I have an IP address stored in a string variable. The IP is like "X.X.X.X"
> where X are numbers.
> I use strings.Split to split the IP and it returns me a []string with 4
> elements inside corresponding to the 4 numericals of the IP.
>
> Then I need to send these numericals in an other function who takes byte in
> entry.
>
> Example :
>
> at the beginning I have *variable := "1.2.3.4"* which is a string, and at

Stéphane phenetas

unread,
Aug 6, 2015, 8:24:19 PM8/6/15
to golang-nuts, phen...@gmail.com
Thank you for the link, , but are you sure this will output me with a []byte ?

The program compiles now, but the logic does not work, I am not sure the output is what is expected.

For example I do this :

IPDestination := "10.11.204.205"
    
    IPAddress:=net.ParseIP(IPDestination)

    fmt.Printf("%s\n",IPAddress[3])
    fmt.Printf("%s\n",IPAddress)


The output is :

%!s(uint8=0)

10.11.204.205



I am not sure the output has been sliced.

Dave Cheney

unread,
Aug 6, 2015, 8:28:33 PM8/6/15
to golang-nuts, phen...@gmail.com
What you're seeing is the net.IP type has a String method, so it implements fmt.Stringer. Because of this fmt's %s formatter knows how to print this value as a string. 

net.IP is defined as a slice of bytes, so IPAddress[0] evaluates to a type of byte, which is not compatible for the %s verb. byte is also an alias for uint8, so that is why fmt complains.

You could try using the %v verb which knows how to print just about everything.

Thanks

Dave

Sam Mortimer

unread,
Aug 6, 2015, 8:32:23 PM8/6/15
to golang-nuts
You have to To4() it before you can access it that way.

Stéphane phenetas

unread,
Aug 6, 2015, 8:33:24 PM8/6/15
to golang-nuts, phen...@gmail.com
I am afraid that it does not print the correct thing, even if I use %v, or %s. It prints "0", instead of what the IP really is.

Dan Kortschak

unread,
Aug 6, 2015, 8:40:15 PM8/6/15
to Stéphane phenetas, golang-nuts
On Thu, 2015-08-06 at 17:24 -0700, Stéphane phenetas wrote:
> Thank you for the link, , but are you sure this will output me with a
> []byte ? '

net.IP is defined as `type IP []byte` (http://golang.org/pkg/net/#IP) so
it is directly convertible, but if you really want to `myFunction(a, b,
c, d byte)` (which seems a little odd to me, but it's your code - I'd
just have the function take a net.IP) you can just call
`myFunction(ip[0], ip[1], ip[2], ip[3])` without any conversion.

Stéphane phenetas

unread,
Aug 6, 2015, 9:11:45 PM8/6/15
to golang-nuts, phen...@gmail.com
I tried to do so, simplest possible :

func main() {

    IPDestination := "10.11.204.205"

    conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})

       IPAddress:=net.ParseIP(IPDestination)

    _, err = conn.WriteToUDP([]byte("wake"), &net.UDPAddr{IP: net.IP{IPAddress[0], IPAddress[1], IPAddress[2], IPAddress[3]}, Port: 7})

    if err!=nil{
    }


fmt.Printf("%v",IPAddress[0])

    fmt.Printf("%v",IPAddress[1])

    fmt.Printf("%v",IPAddress[2])

    fmt.Printf("%v",IPAddress[3])
}

All outputs are "0", I guess I don't use "ParseIP" correctly.

Stéphane phenetas

unread,
Aug 6, 2015, 9:17:33 PM8/6/15
to golang-nuts, phen...@gmail.com
Of course, I am just trying to print the variables to "see" if it works or not. The all line "
_, err = conn.WriteToUDP([]byte("wake"), &net.UDPAddr{IP: net.IP{IPAddress[0], IPAddress[1], IPAddress[2], IPAddress[3]}, Port: 7})" is supposed to send a UDP message. It does not work after the conversion we are talking about.

Nigel Tao

unread,
Aug 6, 2015, 9:20:17 PM8/6/15
to Stéphane phenetas, golang-nuts

Dan Kortschak

unread,
Aug 6, 2015, 9:24:20 PM8/6/15
to Stéphane phenetas, golang-nuts
Reply all
Reply to author
Forward
0 new messages