P
P
Philip Polikarenkov2016-08-24 16:01:53
go
Philip Polikarenkov, 2016-08-24 16:01:53

How to convert string to string from ASC II codes?

In the following, I have a certain string, I want to take each character from this string, convert it to ASCII code
and write the code itself into a string, instead of a character. In Python, this was done simply, in go I can’t think of it.
What I did:
There is a string word := "something"
If you execute fmt.Println(word[0]), then the code for the letter s will be printed, namely 115.
I tried this code

package main

import "fmt"

func main() {

  var new_word string = ""
  word:= "something"
  for _, val := range word {
    new_word += string(val)
  }
  fmt.Println(new_word)
  }

naturally, it prints "something", instead of 115111109101116104105110103
Which way to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2016-08-24
@Vilibb

Towards the strconv module

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var new_word string = ""
    word: = "something"
    for _, val: = range word {
        new_word += strconv.Itoa(int(val))
    }
    fmt.Println(new_word)
}

V
Vladimir Martyanov, 2016-08-24
@vilgeforce

stackoverflow.com/questions/31239330/go-langs-equi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question