FAQ
How does one properly copy an integer value to the message buffer (see
following example code)?

For example, how does one write 1000 as an integer byte(i.e. hex) value to
message?

The following example doesn't seem to write the right values to message....
On the receiving side, it comes out as integer 232

// piece of code...
socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: broadcast_address,
Port: broadcast_port,
})
if err != nil {
fmt.Println("Unable to start UDP Heartbeat Broadcast")
}

var integer_value int
integer_value = 1000

message := make([]byte,1024)

integer_to_send := [2]byte{}
binary.LittleEndian.PutUint16(integer_to_send[:], uint16(integer_value))

copy(message[0:1], integer_to_send[0]

socket.Write(message)




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Stephen Gutekanst at Dec 15, 2013 at 12:34 am
    One problem I see is that you are only copying into your 'message' slice a
    single byte, when uint16 is explicitly two bytes (16 bits)

    Stephen
    On Saturday, December 14, 2013 5:26:12 PM UTC-7, Curtis Paul wrote:

    How does one properly copy an integer value to the message buffer (see
    following example code)?

    For example, how does one write 1000 as an integer byte(i.e. hex) value to
    message?

    The following example doesn't seem to write the right values to message....
    On the receiving side, it comes out as integer 232

    // piece of code...
    socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
    IP: broadcast_address,
    Port: broadcast_port,
    })
    if err != nil {
    fmt.Println("Unable to start UDP Heartbeat Broadcast")
    }

    var integer_value int
    integer_value = 1000

    message := make([]byte,1024)

    integer_to_send := [2]byte{}
    binary.LittleEndian.PutUint16(integer_to_send[:], uint16(integer_value))

    copy(message[0:1], integer_to_send[0]

    socket.Write(message)



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Curtis Paul at Dec 15, 2013 at 12:58 am
    I agree, that's the main problem, Need to treat it as two bytes.

    On Saturday, December 14, 2013 5:34:45 PM UTC-7, Stephen Gutekanst wrote:

    One problem I see is that you are only copying into your 'message' slice a
    single byte, when uint16 is explicitly two bytes (16 bits)

    Stephen
    On Saturday, December 14, 2013 5:26:12 PM UTC-7, Curtis Paul wrote:

    How does one properly copy an integer value to the message buffer (see
    following example code)?

    For example, how does one write 1000 as an integer byte(i.e. hex) value
    to message?

    The following example doesn't seem to write the right values to
    message....
    On the receiving side, it comes out as integer 232

    // piece of code...
    socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
    IP: broadcast_address,
    Port: broadcast_port,
    })
    if err != nil {
    fmt.Println("Unable to start UDP Heartbeat Broadcast")
    }

    var integer_value int
    integer_value = 1000

    message := make([]byte,1024)

    integer_to_send := [2]byte{}
    binary.LittleEndian.PutUint16(integer_to_send[:], uint16(integer_value))

    copy(message[0:1], integer_to_send[0]

    socket.Write(message)



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 15, '13 at 12:26a
activeDec 15, '13 at 12:58a
posts3
users2
websitegolang.org

2 users in discussion

Curtis Paul: 2 posts Stephen Gutekanst: 1 post

People

Translate

site design / logo © 2021 Grokbase