Andreas Raab uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ar.310.mcz ==================== Summary ==================== Name: Collections-ar.310 Author: ar Time: 23 February 2010, 7:16:34.191 am UUID: eb685ed0-dba7-d546-b63e-08f76154d027 Ancestors: Collections-ul.309 Conversions from/to hex representations in ByteArray, for example: #[122 43 213 7] hex => '7a2bd507' ByteArray readHexFrom: '7a2bd507' => #[122 43 213 7] Similarly these can be used for subclasses, e.g., UUID new hex => '97c1f2ddf9209948b329319a30c16386' UUID readHexFrom: '97c1f2ddf9209948b329319a30c16386' => 97c1f2dd-f920-9948-b329-319a30c16386 =============== Diff against Collections-ul.309 =============== Item was added: + ----- Method: ByteArray>>hex (in category 'converting') ----- + hex + "Answer a hexa decimal representation of the receiver" + | string v index map | + map := '0123456789abcdef'. + string := String new: self size * 2. "hex" + index := 0. + 1 to: self size do:[:i| + v := self at: i. + string at: (index := index + 1) put: (map at: (v bitShift: -4) + 1). + string at: (index := index + 1) put: (map at: (v bitAnd: 15) + 1). + ]. + ^string! Item was added: + ----- Method: ByteArray>>readHexFrom: (in category 'initialize') ----- + readHexFrom: aStream + "Initialize the receiver from a hexadecimal string representation" + | map v ch value | + map := '0123456789abcdef'. + 1 to: self size do:[:i| + ch := aStream next. + v := (map indexOf: ch) - 1. + (v between: 0 and: 15) ifFalse:[^self error: 'Hex digit expected']. + value := v bitShift: 4. + ch := aStream next. + v := (map indexOf: ch) - 1. + (v between: 0 and: 15) ifFalse:[^self error: 'Hex digit expected']. + value := value + v. + self at: i put: value. + ]. + ! Item was added: + ----- Method: ByteArray class>>readHexFrom: (in category 'instance creation') ----- + readHexFrom: aString + "Create a byte array from a hexadecimal representation" + ^(self new: aString size // 2) readHexFrom: aString readStream! |
Good, but why lowercase letters ?
Nicolas 2010/2/23 <[hidden email]>: > Andreas Raab uploaded a new version of Collections to project The Trunk: > http://source.squeak.org/trunk/Collections-ar.310.mcz > > ==================== Summary ==================== > > Name: Collections-ar.310 > Author: ar > Time: 23 February 2010, 7:16:34.191 am > UUID: eb685ed0-dba7-d546-b63e-08f76154d027 > Ancestors: Collections-ul.309 > > Conversions from/to hex representations in ByteArray, for example: > > #[122 43 213 7] hex > => '7a2bd507' > > ByteArray readHexFrom: '7a2bd507' > => #[122 43 213 7] > > Similarly these can be used for subclasses, e.g., > > UUID new hex > => '97c1f2ddf9209948b329319a30c16386' > > UUID readHexFrom: '97c1f2ddf9209948b329319a30c16386' > => 97c1f2dd-f920-9948-b329-319a30c16386 > > > =============== Diff against Collections-ul.309 =============== > > Item was added: > + ----- Method: ByteArray>>hex (in category 'converting') ----- > + hex > + "Answer a hexa decimal representation of the receiver" > + | string v index map | > + map := '0123456789abcdef'. > + string := String new: self size * 2. "hex" > + index := 0. > + 1 to: self size do:[:i| > + v := self at: i. > + string at: (index := index + 1) put: (map at: (v bitShift: -4) + 1). > + string at: (index := index + 1) put: (map at: (v bitAnd: 15) + 1). > + ]. > + ^string! > > Item was added: > + ----- Method: ByteArray>>readHexFrom: (in category 'initialize') ----- > + readHexFrom: aStream > + "Initialize the receiver from a hexadecimal string representation" > + | map v ch value | > + map := '0123456789abcdef'. > + 1 to: self size do:[:i| > + ch := aStream next. > + v := (map indexOf: ch) - 1. > + (v between: 0 and: 15) ifFalse:[^self error: 'Hex digit expected']. > + value := v bitShift: 4. > + ch := aStream next. > + v := (map indexOf: ch) - 1. > + (v between: 0 and: 15) ifFalse:[^self error: 'Hex digit expected']. > + value := value + v. > + self at: i put: value. > + ]. > + ! > > Item was added: > + ----- Method: ByteArray class>>readHexFrom: (in category 'instance creation') ----- > + readHexFrom: aString > + "Create a byte array from a hexadecimal representation" > + ^(self new: aString size // 2) readHexFrom: aString readStream! > > > |
On 23.02.2010, at 16:58, Nicolas Cellier wrote:
> > Good, but why lowercase letters ? > > Nicolas Right, I'd prefer uppercase too. The UUID spec (RFC 4122) allows both upper and lower case hex digits, so I don't see a reason not to. - Bert - > > 2010/2/23 <[hidden email]>: >> Andreas Raab uploaded a new version of Collections to project The Trunk: >> http://source.squeak.org/trunk/Collections-ar.310.mcz >> >> ==================== Summary ==================== >> >> Name: Collections-ar.310 >> Author: ar >> Time: 23 February 2010, 7:16:34.191 am >> UUID: eb685ed0-dba7-d546-b63e-08f76154d027 >> Ancestors: Collections-ul.309 >> >> Conversions from/to hex representations in ByteArray, for example: >> >> #[122 43 213 7] hex >> => '7a2bd507' >> >> ByteArray readHexFrom: '7a2bd507' >> => #[122 43 213 7] >> >> Similarly these can be used for subclasses, e.g., >> >> UUID new hex >> => '97c1f2ddf9209948b329319a30c16386' >> >> UUID readHexFrom: '97c1f2ddf9209948b329319a30c16386' >> => 97c1f2dd-f920-9948-b329-319a30c16386 >> |
In reply to this post by Nicolas Cellier
Nicolas Cellier wrote:
> Good, but why lowercase letters ? Because my use cases all required lower case (say converting an MD5 hash into a string that can be used for digest auth). We could go uppercase too; it would just mean that I'd have to rewrite all my code to use asUpper/LowerCase accordingly. But since I've never had the use case where I needed uppercase hex I didn't bother. Cheers, - Andreas > 2010/2/23 <[hidden email]>: >> Andreas Raab uploaded a new version of Collections to project The Trunk: >> http://source.squeak.org/trunk/Collections-ar.310.mcz >> >> ==================== Summary ==================== >> >> Name: Collections-ar.310 >> Author: ar >> Time: 23 February 2010, 7:16:34.191 am >> UUID: eb685ed0-dba7-d546-b63e-08f76154d027 >> Ancestors: Collections-ul.309 >> >> Conversions from/to hex representations in ByteArray, for example: >> >> #[122 43 213 7] hex >> => '7a2bd507' >> >> ByteArray readHexFrom: '7a2bd507' >> => #[122 43 213 7] >> >> Similarly these can be used for subclasses, e.g., >> >> UUID new hex >> => '97c1f2ddf9209948b329319a30c16386' >> >> UUID readHexFrom: '97c1f2ddf9209948b329319a30c16386' >> => 97c1f2dd-f920-9948-b329-319a30c16386 >> >> >> =============== Diff against Collections-ul.309 =============== >> >> Item was added: >> + ----- Method: ByteArray>>hex (in category 'converting') ----- >> + hex >> + "Answer a hexa decimal representation of the receiver" >> + | string v index map | >> + map := '0123456789abcdef'. >> + string := String new: self size * 2. "hex" >> + index := 0. >> + 1 to: self size do:[:i| >> + v := self at: i. >> + string at: (index := index + 1) put: (map at: (v bitShift: -4) + 1). >> + string at: (index := index + 1) put: (map at: (v bitAnd: 15) + 1). >> + ]. >> + ^string! >> >> Item was added: >> + ----- Method: ByteArray>>readHexFrom: (in category 'initialize') ----- >> + readHexFrom: aStream >> + "Initialize the receiver from a hexadecimal string representation" >> + | map v ch value | >> + map := '0123456789abcdef'. >> + 1 to: self size do:[:i| >> + ch := aStream next. >> + v := (map indexOf: ch) - 1. >> + (v between: 0 and: 15) ifFalse:[^self error: 'Hex digit expected']. >> + value := v bitShift: 4. >> + ch := aStream next. >> + v := (map indexOf: ch) - 1. >> + (v between: 0 and: 15) ifFalse:[^self error: 'Hex digit expected']. >> + value := value + v. >> + self at: i put: value. >> + ]. >> + ! >> >> Item was added: >> + ----- Method: ByteArray class>>readHexFrom: (in category 'instance creation') ----- >> + readHexFrom: aString >> + "Create a byte array from a hexadecimal representation" >> + ^(self new: aString size // 2) readHexFrom: aString readStream! >> >> >> > > |
Free forum by Nabble | Edit this page |