You can also use gob for that.

Here are 2 functions from my library that can help.

import "encoding/gob"

/*
encodeGob encodes a model to gob bytes.
*/
func encodeGob(data any) ([]byte, error) {
var (
buf bytes.Buffer
enc = gob.NewEncoder(&buf) // Will write to network.
)

err := enc.Encode(data)

return buf.Bytes(), errors.Wrap(err, "encodeGob")
}

/*
decodeGob decodes gob bytes to a model.
*/
func decodeGob(decVal []byte, data any) (err error) {
buf := bytes.NewBuffer(decVal)
dec := gob.NewDecoder(buf)
err = dec.Decode(data)

return errors.Wrap(err, "decodeGob")
}

Op dinsdag 14 maart 2023 om 00:06:31 UTC+1 schreef robert engels:

> Am arbitrary byte can be encoded in 2 HEX characters - only a 2x increase 
> in size not 8x.
>
> On Mar 13, 2023, at 2:35 PM, Volker Dobler <dr.volke...@gmail.com> wrote:
>
> On Monday, 13 March 2023 at 17:41:42 UTC+1 Van Fury wrote:
>
> Relating to my previous question, I have been reading but it is still not 
> clear to me what raw binary is, how is it different from
> text formatted binary (fmt.Sprintf("%b", s1s2Byte))?
>
> "text formated binary" takes a stream of bytes  and for each bit in
> this stream output "0" or "1" as letters, i.e. the bytes 48==0x30
> and 49==0x31. An eightfold blowup in size.
> Try understanding how _text_ is encoded as bytes.
>  
> Computers work on streams of bytes.
> The meaning/interpretation of a single byte can vary:
> Not all number can be stored in a single byte and
> text has to be encoded by bytes too.
> Please forget about "raw binary", there is no such thing.
> String variables are a stream of bytes and do not need any encoding,
> especially not something like "raw binary" that doesn't exist.
> A stream of bytes can be _printed_ (encoded) as decimal, hexadecimal,
> octal, base64 or even as bit ('0' and '1'). sometimes this is necessary
> because the underlying protocol cannot transmit possible byte values.
> When dealing with "binary" streams this cannot happen (it happens e.g.
> when using JSON to transmit data).
> You have to make clear what the output/encoding space should be
> (and why!): There is a inconsistency between wanting "binary" and
> a textual bit stream (which is not "binary" but text) and hexadecimal
> encoding (also not binary but text).
> You manoeuvred yourself in a corner by focusing on the solution
> instead of the actual problem.
>
> V.
>  
>
> In my problem above I need to encode s1+s2 to raw binary before sending 
> the result to the server
> which then decodes the raw binary back to s1+s2.
>
>
>
> On Mon, Mar 13, 2023 at 5:39 PM Van Fury <fury...@gmail.com> wrote:
>
> Do you mean encoding should be
> rawdata := fmt.Print("%016d%s%s", len(s1), s1,s2) 
> or
> rawdata := fmt.Printf("%016d%s%s", len(s1), s1,s2) 
>
>
>
> On Monday, March 13, 2023 at 4:36:50 PM UTC+2 Alex Howarth wrote:
>
> You might be looking for strconv.ParseUint() 
> https://pkg.go.dev/strconv#ParseUint
>
> https://go.dev/play/p/qAO9LfLD41D
>
> On Mon, 13 Mar 2023 at 07:24, Van Fury <fury...@gmail.com> wrote:
>
>
> Sorry I did not frame my question properly but what I would like to do is 
> to
> encode concatenated s1 and s2 into raw binary and then decode the raw 
> binary
> back to s1 and s2. 
>
>
> On Friday, March 10, 2023 at 11:36:09 PM UTC+2 Alex Howarth wrote:
>
> If s1 and s2 are a fixed length then you can just slice up the decoded 
> string based on the lengths. If they are of a variable length, you'll need 
> a separator in the input string to later split on when decoded (s3 := s1 + 
> ":" + s2 etc)?
>
> On Fri, 10 Mar 2023 at 10:33, Van Fury <fury...@gmail.com> wrote:
>
> Hi,
>
> I have two hexadecimal string values and would like to concatenate the two 
> strings and
>
>    1. encode the result to binary
>    2. decode the resulting binary back to hexadecimal string
>
> I did the following but I was finding it difficult to decode the result 
> back. I ignore error check in this case.
>
> What i did so far:
>
> s1 := "1d28ed66824aa2593e1f2a4cf740343f" 
>
> s2 := "dee2bd5dde763885944bc9d65419"
>
> s3 := s1 + s2 
>
> s1s2Byte, _ := hex.DecodeString(s3)
>
> randAutnBin := fmt.Sprintf("%b", s1s2Byte)
>
> result:
>
> [11101 101000 11101101 1100110 10000010 1001010 10100010 1011001 111110 
> 11111 101010 1001100 11110111 1000000 110100 111111 11011110 11100010 
> 10111101 1011101 11011110 1110110 111000 10000101 10010100 1001011 11001001 
> 11010110 1010100 11001]
>
> I would like to decode the binary result back the hexadecimal string to 
> get s1 and s2.
>
> Any help?
>
> Van
>
> -- 
>
> 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...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/ad3a981b-cf22-47cd-9fe6-8db83a097b42n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/ad3a981b-cf22-47cd-9fe6-8db83a097b42n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
> -- 
> 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...@googlegroups.com.
>
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/1f66ccdc-6c58-48f0-9017-9614220f88d9n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/1f66ccdc-6c58-48f0-9017-9614220f88d9n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
> -- 
>
> You received this message because you are subscribed to a topic in the 
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/golang-nuts/wCGYXo-r-uo/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> golang-nuts...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/8c1be24c-8056-4b74-ac79-e1f8ae7bf935n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/8c1be24c-8056-4b74-ac79-e1f8ae7bf935n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
> -- 
> 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...@googlegroups.com.
>
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/5cd8a906-5b73-4b86-abb1-8106fd9f73cdn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/5cd8a906-5b73-4b86-abb1-8106fd9f73cdn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3bf5ccfd-e007-4f8c-8d1e-a6e76800c403n%40googlegroups.com.

Reply via email to