Re: [go-nuts] How to query mysql hexadecimal data

43 views
Skip to first unread message

burak serdar

unread,
Sep 4, 2019, 9:49:46 AM9/4/19
to abraham...@cumucore.com, golang-nuts
On Wed, Sep 4, 2019 at 6:41 AM <abraham...@cumucore.com> 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 (...,358507777001/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(&msisdn, &k, &opc)
> 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(358507777001)
>
> fmt.Println(data)
>
> output:
> [{0 0 0 358507777001 [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 358507777001 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 golang-nuts...@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.

afriyie...@gmail.com

unread,
Sep 4, 2019, 10:57:31 AM9/4/19
to golang-nuts
Hi

hex.EncodeString works!
The problem was the struct field types ([]byte) instead of string.

Thanks!
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages