-6
\$\begingroup\$

Given a unicode character C (or the first character of a string input), output that character's hex representation. Case does not matter. The output ranges from \u0000 to \u9999.

Test cases

A => 41  (65_10 === 41_16)
ʣ => 2A3 (675_10 === 2A3_16)
Ω => 3A9 (937_10 === 3A9_16)
7 => 37  (55_10 === 37_16)
(null byte) => 0 (0_10 === 0_16)

-50% byte bonus if you output the javascript escape character \uXXXX of the character C, e.g. g => \u0067 and ~ => \u007E

\$\endgroup\$
  • 4
    \$\begingroup\$ -1. I think by the standards of this community this is a valid question and I don't see any grounds to close it. However it is especially trivial as evidenced by all the very quick and very short answers. IMO. YMMV. \$\endgroup\$ – Digital Trauma Jan 8 '16 at 1:34
  • 2
    \$\begingroup\$ @DigitalTrauma IMO... YMMV... what!? \$\endgroup\$ – Seadrus Jan 8 '16 at 1:36
  • \$\begingroup\$ In My Opinion. Your Mileage May Vary. Knowing the things work around here, this question will end up hitting the HNQ and get lots of votes anyway. \$\endgroup\$ – Digital Trauma Jan 8 '16 at 1:38
  • \$\begingroup\$ @DigitalTrauma +3/-6? \$\endgroup\$ – Conor O'Brien Jan 8 '16 at 1:46

11 Answers 11

3
\$\begingroup\$

Jolf, 3 bytes

Try it here!

~Ti

Compiles to var i=prompt("i = ");(i).charCodeAt().toString(16);

Bonus, 13 - 50% = 6.5 bytes

Try it here!

"\\u%"pq~Ti40
"\\u "        prefix
    %         interpolate with
        ~Ti    you know what this is
      pq   40  pad with 4 0's
\$\endgroup\$
  • 5
    \$\begingroup\$ It's rare to see a -50% bonus score be more than double the non-bonus version. :P \$\endgroup\$ – El'endia Starman Jan 8 '16 at 1:32
  • \$\begingroup\$ @El'endiaStarman Yes, that's a tidbit embarrassing ^^" \$\endgroup\$ – Conor O'Brien Jan 8 '16 at 1:33
3
\$\begingroup\$

Julia, 14 bytes

Interestingly, this is 14 bytes with and without the bonus.

No bonus:

c->hex(Int(c))

Bonus:

c->"\\u"lpad(hex(Int(c)),4,0)

Both are lambda functions that accept Chars and return strings. To call a lambda, assign it to a variable.

We get the code point associated with the Char using Int(), then convert to hexadecimal using hex(). For the bonus, we left pad to 4 characters with zeros and prepend \\u. The backslash needs to be escaped in the string.

\$\endgroup\$
  • \$\begingroup\$ Julia has automatic string concatenation? o-O \$\endgroup\$ – Conor O'Brien Jan 8 '16 at 1:35
  • 1
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Here it's actually a combination of two features: Julia uses multiplication to concatenate strings, and in some cases the * can be omitted for multiplication since Julia has coefficient literals (or whatever you want to call it when 2x means 2*x). \$\endgroup\$ – Alex A. Jan 8 '16 at 1:38
  • \$\begingroup\$ ... I remember why I got Julia. Cool language! :D \$\endgroup\$ – Conor O'Brien Jan 8 '16 at 1:39
3
\$\begingroup\$

CJam, 5.5 bytes

"\u%04X"qe%

The code is 11 bytes long and qualifies for the -50% bonus. Try it online in the CJam interpreter.

How it works

"\u%04X"     e# Push that format string, i.e., a literal "\u", followed by
             e# an integer in hexadecimal, 0-padded to 4 characters.
        q    e# Read all input.
         e%  e# Apply string formatting.

The online interpreter will happily treat the input string as the code point of its sole character. This does not work in the Java interpreter, so it might be a feature or a bug.

\$\endgroup\$
3
\$\begingroup\$

CJam, 5 20 18 bytes

{iGb{A,6,'Af++=}%}

i converts to integer, G pushes 16, b is base conversion.

... and CJam has no way to use 0-9A-Z instead of base-converting to numbers? That's not annoying at all.

{          }%   map over number-digits
 A,             generate the digits 0-9
   6,'Af+       generate the "digits" A-F
         +      concat
          =     array-index into the newly generated array

Thanks to @MartinBüttner for saving 2 bytes!

\$\endgroup\$
  • \$\begingroup\$ {iGb{A,6,'Af++=}%} \$\endgroup\$ – Martin Ender Jan 12 '16 at 15:49
2
\$\begingroup\$

Pyth - 4 bytes

.HCz

Test Suite.

Pyth doesn't do unicode for string conversions, instead uses ISO-8859, so cost one more byte.

.H            Hex representation
 C            Unicode code point
  z           Input string
\$\endgroup\$
2
\$\begingroup\$

JavaScript ES6, 30 54 / 2 = 27 bytes

s=>"\\u"+("000"+s.charCodeAt().toString(16)).slice(-4)

The bonus helped a bit

Try it online at ESFiddle (all browsers work)

\$\endgroup\$
  • \$\begingroup\$ At least we know that this is the absolute hard minimum. I'm sure the bonus will help out here. \$\endgroup\$ – Conor O'Brien Jan 8 '16 at 1:41
2
\$\begingroup\$

Mathematica, 50 - 50% = 25 bytes

"\u"<>IntegerString[First@ToCharacterCode@#,16,4]&

Ignore the error. Mathematica does not recognize the escape code \u, so just escapes the \ and gives a Syntax:stresc message.

\$\endgroup\$
2
\$\begingroup\$

TeaScript, 5 bytes

xcT16

I'm very sure I have a unicode shortcut for 16

Try it online

\$\endgroup\$
  • \$\begingroup\$ Isn't m=16 from the docs? \$\endgroup\$ – ev3commander Jan 9 '16 at 1:00
  • \$\begingroup\$ @BlockCoder1392 yeah but I'd still have to replace 16 with (m which isn't any shorter \$\endgroup\$ – Downgoat Jan 9 '16 at 1:01
1
\$\begingroup\$

Python 2, 27/2 = 13.5 bytes

lambda x:r'\u%04X'%(ord(x))

No online link because apparently ideone's Python 2 version doesn't support Unicode string literals.

\$\endgroup\$
1
\$\begingroup\$

Japt, 5 bytes

Uc sG

Very simple. Try it online!

How it works

Uc sG  // Implicit: U = input, G = 16
Uc     // Take the char code of U.
   sG  // Turn it into a base-16 string.
       // Implicit: output last expression

Bonus version, 20 bytes * 0.5 = 10

"\\u{'0³+Uc sG)t0-4 

Not as simple. Try it online!

How it works

          // Implicit: U = input, G = 16
"\\u{     // Take the string "\u", and add:
 '0³+     //  Take 3 "0"s, plus
 Uc sG)   //  the hex char code of the input.
 t0-4     //  Take the last 4 chars.
          // Implicit: output last expression
\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 3 chars / 8 bytes

ï⒞ⓧ

Try it here (Firefox only).

Superbly simple. Convert to charcode, then to hex.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.