闲聊:如何将其十六进制值转换为字符串? (视觉作品)

small talk: how to turn int into a string of its hex value? (visual works)

TL;DR :如何将 4 个数字加在一起并将答案存储为表示十六进制值的字符串(即 10+5 存储为 "F", 或 2+1 存储为 "3")

.

这在 HERE 之前被问过一次,但 none 的答案足以满足我的申请。我将在下面展示我的操作方式以及我想在评论中获得的内容:

| response bit1 bit2 bit3 bit4 addedBits objStatus|
"are objects on station1?"
(self robot hasWaferAt: 1)
ifTrue:[bit1:=2r1000.bit3:=2r10.]
ifFalse:[bit1:=2r0000.bit3:=2r00.]. 
"are objects on station2?"
(self robot hasWaferAt:  2)
ifTrue:[bit2:=2r100.bit4:=2r1.]
ifFalse:[bit2:=2r000.bit4:=2r0.].
addedBits := (((bit1 bitOr: bit2)bitOr: bit3)bitOr: bit4).

这里我需要 objStatus 将 addedBits 保存为字符串(即,如果 addedBits 为 13,waferStatus 需要保存 "D" 或 'D') 因为我然后通过 TCPIP 传输这个字符串:

response := (myCommand getUnitNumberFromResponse: aCommandString),
(myCommand getSequenceNumberFromResponse: aCommandString),
'0000',     "Ack code"
'0000',     "error code:  0000 is success."
waferStatus, "which stations have objects"
'FFF'.       "no objects present = FFFF"
response := (myCommand commandResponsePrefix), 
             response,
             (myCommand computeChecksum: response).
self sendMessage: response.      
(10 + 5) asBigEndianByteArray asHexString
=> '0F'

应该够了。数字本身似乎没有 asHexString 等价物,因此我们必须先将数字转换为 ByteArray

如果您必须 trim 前导 0,您可以执行以下操作:

[result allButLast startsWith: '0'] whileTrue: [result := result allButFirst].

(但是有无数种方法可以做同样的事情...)

w=10=sh w=13=will.year: w=12=will w=11=sh