开发者

Storing hex values in a map .. java?

I am currently doing it like this.. but it gets actually stored as integer.. How can i do it?

commandMap = new HashMap();

commandMap.put("SET_DISPLAY", 0xD0);

commandMap.put("READ_ADC", 0xD1);

commandMap.put("GET_PARAM", 0xD2);

commandMap.put("SET_PARAM", 0xD3);

commandMap.put("GET_IOVALUE", 0xD4)开发者_Go百科;

commandMap.put("SET_IOVALUE", 0xD5);


Decimal, hex, octal and so on are just notations; i.e. different ways of rendering an integer in characters. They are not special kinds of numbers.

So ...

commandMap = new HashMap(); 
commandMap.put("SET_DISPLAY", 0xD0);
int value = commandMap.get("SET_DISPLAY");
System.err.println("0x" + Integer.toHexString(value));


There is no separate "hex" data type. If you want to display values as hexidecimal you can use the static method Integer.toHexString(int i).

Even though they're stored as integers, you can still do things like

if (commandMap.get(command) == 0xD2) {
    ...
}

so there's really no need to have a separate data type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜