On Wed, Mar 31, 2010 at 7:22 PM, Glen Rubin <rubing...@gmail.com> wrote: > I have a sequence of hex strings, e.g. > > "ff43" "0032" ... (you get the idea) > > I want to use clojure's short form on them, but short expects an > authentic hex input, e.g. > > (short 0xff43) > > it will not accept something like (short "0xff43")
Right. When you type (short 0xff43), the reader converts that into an actual number before the function (short) is ever actually called; all it ever sees is a number, and has no idea if you typed "0xff43", "653477", or "0177503", or "(+ 0xff40 3))". You could prepend "0x" to your strings and manually invoke the reader on them (after clearing the unsafe evaluation flags), but using parseInt as suggested by Richard is probably cleaner. However: On Wed, Mar 31, 2010 at 7:58 PM, Richard Newman <holyg...@gmail.com> wrote: > ur=> (map #(Integer/parseInt % 16) ["ff43" "0032"]) > (65347 50) ...that yields, not shorts. You could try this: (map #(short (Integer/parseInt % 16)) ["ff43" "0032"]) Which works fine in 1.1. But starting in 1.2 that runs into the range problem we talked about with respect to (byte) in another thread. (And that's true of literals as well; (short 0xff43) will throw an IllegalArgumentException. So manually invoking the reader is not a solution.) The full, future-proof solution is something like this: (map #(let [n (Integer/parseInt % 16)] (short (if (bit-test n 15) (bit-or n -65536) (bit-and n 65535)))) ["ff43" "0032"]) although of course factoring some of that out into named functions is probably not the worst idea. On a related note: if I have the symbol for a type, like "short" or "int" or "byte", is there a way to ask Clojure what size word that represents? -- Mark J. Reed <markjr...@gmail.com> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en To unsubscribe, reply using "remove me" as the subject.