How to check if the the value in the map is an integer

Its again basic programming question which I am trying to understand the concept.

I have set of predefined key-values under Pkey and should be of this format
ie the Key is of string type and Value associated with that should be hex ‘0x’. I don’t know how to declare value as hex type .

Questions would be,

  1. How to assign hex value to a key in a dictionary?
  2. If there is a format of type “DS_256” : 124 , my program should read thru the Pkeys range and if it finds 0xValue, skip else convert the non-hex value into an hex and store in an array
    ie identify 124 as an integer (since no presence of 0x) and should convert to hex.

I have created a dict with values as shown below.
I want to check if the value of ES_128 is an integer and do some conversion.
Was able to check for the existence of key in the map but how to check if the value of the key is an integer.
(Was just testing the functionality of unicode and resulted in error)

    var Pkeys = map[string]int64 { 

    "ES_128" : 0x00AA, 
    "PS_256" : 0x00A8, 
    "DS_256" : 124,
    }
    type PCdecipher struct {
        defTab map[string]int
    }

    func (pc *PCdecipher) init(Pkeys map[string]int64) {
    	for _, value := range Pkeys {
    	  if unicode.IsNumber(value) {
    		    fmt.Printf("Found")
    }
    	fmt.Printf(" : 0x0%x",value)
    	}
    }

func main() {
        var pc PCdecipher
        pc.init(Pkeys)
}

You have defined Pkeys as a map from string to int64, so the values in the map must be int64s.

unicode.IsNumber checks if a rune value (e.g. 'a', '0', '$', etc.) is a numeric code point, but value in your (*PCdecipher).init function is an int64, so it cannot be passed into unicode.IsNumber and you get an error:

./prog.go:21:22: cannot use value (type int64) as type rune in argument to unicode.IsNumber

Go build failed.

I cannot tell what you’re trying to do based on your example. Can you add more description and then maybe someone can give you some suggestions?

Thank you @skillian
I modified the question and when I get into actual coding, am loaded with lots of doubts.

The XY problem is asking about your attempted solution rather than your actual problem: The XY Problem.

Tell us what your actual problem is.

There is no “hex” type in Go. Hexadecimal is a representation of a value. The following constants are all the same:

const (
    a = 123
    b = 0173
    c = 0x7b
)

They are all the same int values. Here’s a program that prints all of them as decimal, octal, and hexadecimal: Go Playground - The Go Programming Language

I think petrus’ point is valid: Can you restate the problem? Why are you trying to rewrite values in a map to appear as hexadecimal?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.