How to convert byte array to string in Go [duplicate] - go

This question already has answers here:
How do I convert [Size]byte to string in Go?
(8 answers)
Closed 2 years ago.
[]byte to string raises an error.
string([]byte[:n]) raises an error too.
By the way, for example, sha1 value to string for filename.
Does it need utf-8 or any other encoding set explicitly?
Thanks!

The easiest method I use to convert byte to string is:
myString := string(myBytes[:])

The easiest way to convert []byte to string in Go:
myString := string(myBytes)
Note: to convert a "sha1 value to string" like you're asking, it needs to be encoded first, since a hash is binary. The traditional encoding for SHA hashes is hex (import "encoding/hex"):
myString := hex.EncodeToString(sha1bytes)

In Go you convert a byte array (utf-8) to a string by doing string(bytes) so in your example, it should be string(byte[:n]) assuming byte is a slice of bytes.

I am not sure that i understand question correctly, but may be:
var ab20 [20]byte = sha1.Sum([]byte("filename.txt"))
var sx16 string = fmt.Sprintf("%x", ab20)
fmt.Print(sx16)
https://play.golang.org/p/haChjjsH0-

ToBe := [6]byte{65, 66, 67, 226, 130, 172}
s:=ToBe[:3]
// this will work
fmt.Printf("%s",string(s))
// this will not
fmt.Printf("%s",string(ToBe))
Difference : ToBe is an array whereas s is a slice.

First you're getting all these negatives reviews because you didn't provided any code.
Second, without a good example. This is what i'd do
var Buf bytes.Buffer
Buf.Write([]byte)
myString := Buf.String()
Buf.Reset() // Reset the buffer to reuse later
or better yet
myString := string(someByteArray[:n])
see here also see #JimB's comment
That being said if you help that targets your program, please provide and example of what you've tried, the expect results, and error.

We can just guess what is wrong with your code because no meaningful example is provided. But first what I see that string([]byte[:n]) is not valid at all. []byte[:n] is not a valid expression because no memory allocated for the array. Since byte array could be converted to string directly I assume that you have just a syntax error.
Shortest valid is fmt.Println(string([]byte{'g', 'o'}))

Related

Converting Integer to String in url.Query().Set using String function [duplicate]

This question already has answers here:
How to convert an int value to string in Go?
(10 answers)
Closed last year.
this is my day 1 using goLang, I am currently trying to consume a data but I encounter an error, and that's converting integer to string
func createLink(title string, page int) string {
url := url.URL{
Scheme: "https",
Host: "jsonmock.hackerrank.com",
Path: "/api/movies/search/",
}
query := url.Query()
query.Set("page", string(page))
query.Set("title", title)
url.RawQuery = query.Encode()
return url.String()
}
you can try that code, and the result is
actual result :
https://jsonmock.hackerrank.com/api/movies/search/?page=%01&title=spiderman
expected result :
https://jsonmock.hackerrank.com/api/movies/search/?page=1&title=spiderman
There's %01 , an that's something that I do not want. i believe that I made a mistake in converting an integer to string
You should use strconv.Itoa() method to format your integers as strings. This is better explained in the linked answer. For the sake of completeness, here's how you end up with %01 in your result:
first, int 1 gets "plain-converted" to string by following this conversion rule:
Converting a signed or unsigned integer value to a string type yields
a string containing the UTF-8 representation of the integer. Values
outside the range of valid Unicode code points are converted to
"\uFFFD".
then the resulting string (with character of unicode code point equal to 1) gets URL-encoded, ending up with %01 as its representation.
As a sidenote, you're warned about this if you run go vet over your code:
hello.go:19:20: conversion from int to string yields a string of one
rune, not a string of digits (did you mean fmt.Sprint(x)?)
While this doesn't always give you absolutely the best advice on how to fix your error, it at least pushed you into the right direction. And it's strongly recommended to get used to the idea of running this (or similar) kind of checks from day 1 of learning language.

GO encoding/decoding

I'm workin with python. But now, I need to fix Go bug.
I have string like:
<!-- \\xd0\\xbf\\xd0\\xbb\\xd0\\xb0\\xd1\\x82\\xd0\\xb5\\xd0\\xb6\\xd0\\xb5\\xd0\\xb9-->\\n \\n \\n <guarantees>\\n
How do make it correct and readable?
If it were a Python, I would use decode('unicode-escape'). But what should I use in Go?
Update
I've edited description. There are double backslashes
Update 1
I followed the advice from the answer https://stackoverflow.com/a/67172057/11029221, and repaired the part of the code that is doing the encoding in such a wrong way.
But I found out that in GO you can fix such text like this:
a := `\\xd0\\xb5\\xd0\\xb6\\xd0\\xb5\\xd0\\xb9-->\\n\\n\\n<guarantees>\\n`
a = strconv.Quote(a)
a = strings.ReplaceAll(a, `\\\\`, `\`)
unquoted, err := strconv.Unquote(a)
if err != nil {
println(err)
}
str := []byte(unquoted)
for len(str) > 0 {
r, size := utf8.DecodeLastRune(str)
out = string(r) + out
str = str[:len(str)-size]
}
fmt.Printf("%s", out)
I'm not sure what #melpomene's criteria for "knowing what they are doing" are, but the following solution has worked previously, for example for decoding broken Hebrew text:
("\\u00c3\\u00a4"
.encode('latin-1')
.decode('unicode_escape')
.encode('latin-1')
.decode('utf-8')
)
outputs
'ä'
This works as follows:
The string that contains only ascii-characters '\', 'u', '0', '0', 'c', etc. is converted to bytes using some not-too-crazy 8-bit encoding (doesn't really matter which one, as long as it treats ASCII characters properly)
Use a decoder that interprets the '\u00c3' escapes as unicode code point U+00C3 (LATIN CAPITAL LETTER A WITH TILDE, 'Ã'). From the point of view of your code, it's nonsense, but this unicode code point has the right byte representation when again encoded with ISO-8859-1/'latin-1', so...
encode it again with 'latin-1'
Decode it "properly" this time, as UTF-8
Again, same remark as in the linked post: before investing too much energy trying to repair the broken text, you might want to try to repair the part of the code that is doing the encoding in such a strange way.

Golang Typecasting

I have specific questions for my project
input = "3d6"
I want to convert this string some parts to integer. For instance I want to use input[0] like integer.
How can I do this?
There's two problems here:
How to convert a string to an integer
The most straightforward method is the Atoi (ASCII to integer) function in the strconv package., which will take a string of numeric characters and coerce them into an integer for you.
How to extract meaningful components of a known string pattern
In order to use strconv.Atoi, we need the numeric characters of the input by themselves. There's lots of ways to slice and dice a string.
You can just grab the first and last characters directly - input[:1] and input[2:] are the ticket.
You could split the string into two strings on the character "d". Look at the split method, a member of the strings package.
For more complex problems in this space, regular expressions are used. They're a way to define a pattern the computer can look for. For example, the regular expression ^x(\d+)$ will match on any string that starts with the character x and is followed by one or more numeric characters. It will provide direct access to the numeric characters it found by themselves.
Go has first class support for regular expressions via its regexp package.
For example,
package main
import (
"fmt"
)
func main() {
input := "3d6"
i := int(input[0] - '0')
fmt.Println(i)
}
Playground: https://play.golang.org/p/061miKcXdIF
Output:
3

How to use protobuf to deserialize string between c# and java?

In c#,i use protobuf-net to serialize a string to byte array and send it through network
var bytes = Serializer.SerializeObject("Hello,world")
this byte array contains 13 elements, includes 2 prefix tags, start with 0x10, then 0x0b for string length.
i tried to deserialize in java, I use ByteString to convert that byte array to string, i got an error string: \n Hello,world!
this means that java does not ignore the prefix tags.
anybody knows why? thx!
The protobuf format doesn't allow for naked data, so protobuf-net interprets Serializer.Serialize("Hello, world") as though it were a message of the form:
message Foo {
optional value = 1;
}
and as if you had - in C# terms - used:
Serializer.Serialize(new Foo { value = "Hello, world") });
The 0x10 is the field marker for field 1, etc.
If you ever want to check the internals of an encoded message without knowing the schema, this tool may help: https://protogen.marcgravell.com/decode

Ruby: What does unpack("C") actually do?

From the docs, unpack does:
Decodes str (which may contain binary data) according to the format
string, returning an array of each value extracted.
And the "C" format means 8-bit unsigned (unsigned char).
But what does this actually end up doing to the string I input? What does the result mean, and if I had to do it by hand, how would I go about doing that?
It converts each subsequent char to it’s integer ordinal as String#ord does. That said,
string.unpack 'C*'
is an exact equivalent of
string.each_char.map(&:ord)
But what does this actually end up doing to the string I input
It doesn't do anything to the input. And the input is not really a string here. It's typed as a string, but it is really a buffer of binary data, such as you might receive by networking, and your goal is to extract that data into an array of integers. Example:
s = "\01\00\02\03"
arr = s.unpack("C*")
p(arr) # [1,0,2,3]
That "string" would be meaningless as a string of text, but it is quite viable as a data buffer. Unpacking it allows you examine the data.

Resources