2015-01-27 3 views
1

TL : 4 자리 숫자를 더하고 16 진수 값을 나타내는 문자열 (예 : 10 + 5를 "F"로 저장하거나 2 + 1을 " 3 ")작은 이야기 : int를 16 진수 문자열로 변환하는 방법은 무엇입니까? (비주얼 작품)

.

여기 한 번 전에 물어 본 적이 있지만 답변이 부족하여 신청할 수 없습니다. 나는 아래 운영하고 있는데 제가 의견 좀하고 싶습니다 방법을 보여줍니다 : 여기

| 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). 

내가 문자열로 addedBits를 개최 objStatus 필요해 (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.  

답변

3
(10 + 5) asBigEndianByteArray asHexString 
=> '0F' 

는 충분합니다. 숫자 자체에 해당하는 숫자가 asHexString 인 것 같지 않으므로 먼저 숫자를 ByteArray으로 변환해야합니다.

당신은 최고의 0의 손질해야하는 경우 다음과 같이 할 수있는 :

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

(하지만 같은 일을 할 수있는 수많은 방법이 있습니다 ...)

+0

감사합니다! 내 작품 '프랑켄슈타인'을 잘 해결해. 4 가지 가능한 결과를 확인하기 위해 일련의 네 가지 중첩 if 문을 수행했습니다. 이것은 분명히 개선이다. – DarthSheldon

0
anInteger printStringRadix: 16. 

을 예 :

15 printStringRadix: 16 ==> 'F'