Hi
hex.EncodeString works!
The problem was the struct field types ([]byte) instead of string.
Thanks!
On Wednesday, September 4, 2019 at 4:49:46 PM UTC+3, burak serdar wrote:
>
> On Wed, Sep 4, 2019 at 6:41 AM >
> wrote:
> >
> >
> > I have tried to query mysql database table data but am getting the byte
> data after trying many instances to convert the byte data to the
> hexadecimal values. Any help about how can i get the hexadecimal values
> from the database.
> >
> > I created the table below as
> >
> >
> > CREATE TABLE `subscriber_profile` (
> >
> > `msin` binary(5) NOT NULL,
> > `msisdn` bigint(16) NOT NULL,
> > `k` binary(16) DEFAULT NULL,
> > `opc` binary(16) DEFAULT NULL,
> > `sqn` binary(6) DEFAULT NULL,
> >...
> > PRIMARY KEY (`mcc`,`mnc`,`msin`)
> > ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
> >
> > And the data in the database as
> >
> >
> > INSERT INTO subscriber_profile VALUES
> (...,35850001/msisdn/,0x000102030405060708090A0B0C0D0E0F/k/,0xBC2BCE2A23BE2FAE32E4F1B4546004F7/opc/,...);
>
>
> >
> > Am querying the table using msisdn as parameter. Also is using byte in
> struct field right?
> >
> >
> > type SubscriberProfile struct {
> > ...
> > Msisdn int`json:"msisdn"`
> > K []byte `json:"k"`
> > Opc[]byte `json:"opc"`
> > }
> >
> > func GetPara(msisdn int) []SubscriberProfile {
> > db := dbConn()
> > selDB, err := db.Query("SELECT msisdn, k, opc FROM
> subscriber_profile WHERE msisdn=?;", msisdn)
> > if err != nil {
> > panic(err.Error())
> > }
> > av := SubscriberProfile{}
> > res := []SubscriberProfile{}
> > for selDB.Next() {
> > var msisdn int
> > var k, opc []byte
> > err = selDB.Scan(, , )
> > if err != nil {
> > panic(err.Error())
> > }
> >
> > av.Msisdn = msisdn
> > av.K = k
> > av.Opc = opc
> > res = append(res, av)
> > }
> > return res
> > }
> >
> > I have tried to use hex.EncodeToString(k) but could not get the right
> result.
> >
> > var data []SubscriberProfile
> > data = GetPara(35850001)
> >
> > fmt.Println(data)
> >
> > output:
> > [{0 0 0 35850001 [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] [188 43 206
> 42 35 190 47 174 50 228 241 180 84 96 4 247]}]
> >
> >
> > Am expecting an output [{0 0 0 35850001
> 000102030405060708090A0B0C0D0E0F BC2BCE2A23BE2FAE32E4F1B4546004F7 0 ...}]
> >
> > Any help?
>
> The output you got is already the same as your expected output, with
> the difference that the byte arrays are printed as decimal values
> enclosed with [ ]. Instead of fmt.Println, print the struct fields one
> by one while hex-encoding the contents of the byte arrays to get what
> you need. What was the problem with hex.EncodeString?
>
>
> >
> >
> >
> > --
> > 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 golan...@googlegroups.com .
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/fd3a967e-2b02-4658-a27b-ccfd40aa125d%40googlegroups.com.
>
>
>
--
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/2d4d7200-979c-4105-b35d-bfd84117fb0b%40googlegroups.com.