Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8915429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:58:33+00:00 2026-06-15T04:58:33+00:00

all. I’m encountering what seems to be a very strange problem. (It could be

  • 0

all. I’m encountering what seems to be a very strange problem. (It could be that it’s far past when I should be asleep, and I’m overlooking something obvious.)

I have a []byte with length 8 as a result of some hex decoding. I need to produce a uint64 in order to use it. I have tried using binary.Uvarint(), from encoding/binary to do so, but it seems to only use the first byte in the array. Consider the following example.

package main

import (
    "encoding/binary"
    "fmt"
)

func main() {
    array := []byte{0x00, 0x01, 0x08, 0x00, 0x08, 0x01, 0xab, 0x01}
    num, _ := binary.Uvarint(array[0:8])
    fmt.Printf("%v, %x\n", array, num)
}

Here it is on play.golang.org.

When that is run, it displays the num as 0, even though, in hex, it should be 000108000801ab01. Furthermore, if one catches the second value from binary.Uvarint(), it is the number of bytes read from the buffer, which, to my knowledge, should be 8, even though it is actually 1.

Am I interpreting this wrong? If so, what should I be using instead?

Thanks, you all. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:58:34+00:00Added an answer on June 15, 2026 at 4:58 am

    You’re decoding using a function whose use isn’t the one you need :

    Varints are a method of encoding integers using one or more bytes;
    numbers with smaller absolute value take a smaller number of bytes.
    For a specification, see
    http://code.google.com/apis/protocolbuffers/docs/encoding.html.

    It’s not the standard encoding but a very specific, variable byte number, encoding. That’s why it stops at the first byte whose value is less than 0x080.

    As pointed by Stephen, binary.BigEndian and binary.LittleEndian provide useful functions to decode directly :

    type ByteOrder interface {
        Uint16([]byte) uint16
        Uint32([]byte) uint32
        Uint64([]byte) uint64
        PutUint16([]byte, uint16)
        PutUint32([]byte, uint32)
        PutUint64([]byte, uint64)
        String() string
    }
    

    So you may use

    package main
    
    import (
        "encoding/binary"
        "fmt"
    )
    
    func main() {
        array := []byte{0x00, 0x01, 0x08, 0x00, 0x08, 0x01, 0xab, 0x01}
        num := binary.LittleEndian.Uint64(array)
        fmt.Printf("%v, %x", array, num)
    }
    

    or (if you want to check errors instead of panicking, thanks jimt for pointing this problem with the direct solution) :

    package main
    
    import (
        "encoding/binary"
        "bytes"
        "fmt"
    )
    
    func main() {
        array := []byte{0x00, 0x01, 0x08, 0x00, 0x08, 0x01, 0xab, 0x01}
        var num uint64
        err := binary.Read(bytes.NewBuffer(array[:]), binary.LittleEndian, &num)
        fmt.Printf("%v, %x", array, num)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I want to do is return the index of the i that is
all. We're trying to get some intersect collisions working, but the problem experience is
All! I found strange code in LinkedBlockingQueue: private E dequeue() { // assert takeLock.isHeldByCurrentThread();
All of a sudden the graph api only seems to be returning old posts.
All, There is a website that doesn't seem legitimate that and keeps showing all
All, I have a stored procedure on SQL Server 2005 that accepts an XML
All time am getting below error message in groovy... Could not understand whats causing
All, We could really do with some advice from SVG gurus. WHAT WE HAVE:
All my PHP pages work fine but there is one PHP page that used
All my research shows that there's no real usage for the @private directive -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.