33
\$\begingroup\$

Output the device's screen resolution in the specific format of [width]x[height](without the brackets). For example, an output could be 1440x900.

Here's an online tester that you can use to check your own screen resolution.

\$\endgroup\$
  • 17
    \$\begingroup\$ The specific output format is no fun, but it's probably too late to change now \$\endgroup\$ – Luis Mendo May 2 '17 at 13:29
  • 3
    \$\begingroup\$ What should the behaviour be if multiple displays are connected? \$\endgroup\$ – Jonathan Allan May 2 '17 at 13:40
  • 4
    \$\begingroup\$ I suppose we're not allowed to first change your resolution and then tell you those values, right? \$\endgroup\$ – Engineer Toast May 2 '17 at 15:12
  • 3
    \$\begingroup\$ APL\360 (can only be run on IBM/360 typewriter environment), 5 bytes: '0x0' \$\endgroup\$ – Adám May 2 '17 at 16:14
  • 4
    \$\begingroup\$ I like that this one disqualifies most golfing languages and encourages people to explore the limits of practical ones. \$\endgroup\$ – robbie May 4 '17 at 14:31

46 Answers 46

1
\$\begingroup\$

Bash + xdotool + tr, 35 bytes

-1 thanks to @manatwork

xdotool getdisplaygeometry|tr \  x

There are two spaces after the \

Gets geometry, then uses tr to replace spaces with x

\$\endgroup\$
  • \$\begingroup\$ Is enough to just escape the space, is shorter than quoting it: tr␠\␠␠x. (Used ␠ to mark spaces here just because HTML rendering collapses consecutive whitespaces.) \$\endgroup\$ – manatwork May 4 '17 at 8:31
1
\$\begingroup\$

Javascript, 42 bytes

console.log((k=screen).width+'x'+k.height)
\$\endgroup\$
  • 1
    \$\begingroup\$ Hello and welcome to our site! Make sure to include a byte count in your title for code-golf questions. \$\endgroup\$ – Wheat Wizard May 4 '17 at 5:25
  • \$\begingroup\$ Okay, thank you! Sorry, I didn't know it was a requirement. \$\endgroup\$ – Will Brickner May 5 '17 at 7:55
1
\$\begingroup\$

Python 3, 54 bytes

Python 2, 52 51 bytes

from PIL.ImageGrab import*;print'%dx%d'%grab().size

Saved 2 bytes by executing in Python 2

Saved 1 byte by changing import

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! I don't have PIL installed, so I can't test, but could you just do i.grab().size instead of the string formatting? I don't think it has to be exactly in the WxH format \$\endgroup\$ – DJMcMayhem May 3 '17 at 21:23
  • \$\begingroup\$ @DJMcMayhem Hello! I thought the same thing, but OP says specific format of [width]x[height]. i.grab().size prints out a tuple as (1920, 1080). Granite that would save me 8 bytes, I'd rather go for accuracy to OPs requirements \$\endgroup\$ – Wondercricket May 3 '17 at 21:26
1
\$\begingroup\$

Batch, 128 bytes

@for /f "tokens=1,2delims=x " %%A in ('"wmic path Win32_VideoController get VideoModeDescription|findstr "l""') do @echo %%Ax%%B

Basically the same thing as SteveFest's answer, except I grab a slightly shorter chunk of Wim32_VideoController and then I use something vaguely resembling regex to get the one line that contains the data that I want.

I have no idea why the wmic string needs to be inside of double quotes to work, and I can't believe that I can't shorten the string at all.

How It Works

Ordinarily, wmic path Win32_VideoController get VideoModeDescription will display something like this:

VideoModeDescription
3440 x 1440 x 4294967296 colors
 

I can use findstr's super rudimentary attempt at regex to find the letter "l," which only matches the line with the word "colors." From there, that line is delimited on spaces and the letter "x." %%A contains the first token and %%B contains the second token. After that, I just display the values.

\$\endgroup\$
  • \$\begingroup\$ Aw.. I've been looking for ways to shorten my code... Nice answer anyways \$\endgroup\$ – stevefestl May 6 '17 at 4:12
1
\$\begingroup\$

Batch, 218 208 206 bytes

I can't golf....

@for /f %%# in ('"@wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"')do @set %%#>nul
@echo %CurrentHorizontalResolution%x%CurrentVerticalResolution%
\$\endgroup\$
1
\$\begingroup\$

Racket, 73 bytes

#!racket/gui
(let-values([(x y)(get-display-size #t)])(printf"~ax~a"x y))

Just discovered the (discouraged) shorthand for #lang. Saves a few bytes! Documentation for get-display-size.

\$\endgroup\$
0
\$\begingroup\$

Python 2, 60 bytes

from win32api import*
u=GetSystemMetrics
print u(0),'x',u(1)
\$\endgroup\$
0
\$\begingroup\$

C#, 89 bytes

_=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
\$\endgroup\$
0
\$\begingroup\$

PHP + GTK 2.0, 69 bytes

This whole answer was written based on http://php-gtk.eu/en/code-hints/grabbing-a-screenshot-with-gdk

<?list($W,$H)=Gdk::get_default_root_window()->get_size();echo$W,x,$H;

Basically, fetches the "root" window (entire screen) and gets it's size.


Another way (based on http://www.kksou.com/php-gtk2/sample-codes/get-the-size-of-display-screen.php):

<?$w=(new GtkWindow())->get_screen();echo$w->get_width(),x,$w->get_height();

Gets the screen where the window was created, displaying it's dimentions.

\$\endgroup\$
0
\$\begingroup\$

Javascript, 46 bytes

var s=screen;console.log(s.width+'x'+s.height)

I wanted to write it in the comment but I don't have reputation yet. Can someone explain to me why other solutions don't count bytes for console.log()? If they were counted then this solution would be the shortest.

\$\endgroup\$
0
\$\begingroup\$

MATLAB, 42 byte

a=get(0,'ScreenS');fprintf('%dx%d',a(3:4))

Uses property shortening. Other than that, this is a nice showcase of the inflexibility of MATLAB indexing, requiring a temporary variable a. fprintf prints to stdout by default.

\$\endgroup\$
0
\$\begingroup\$

Clojure, 86 bytes

Shameless port of this Java answer

(#(str(.width %1)"x"(.height %1))(.getScreenSize(java.awt.Toolkit/getDefaultToolkit)))
\$\endgroup\$
0
\$\begingroup\$

8th, 32 bytes

0 hw:displaysize? swap . "x" . .

Returns the device's screen resolution of the first display (0) in pixels in the required format.

Output:

1536x864
\$\endgroup\$
0
\$\begingroup\$

C#, 90 Bytes

()=>{var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return b.Width+"x"+b.Height;};

This actually is quite a similiar answer to that one, which already exists, however, I'd like to add a few things. This solution acutally needs a reference to System.Windows.Forms and one to System.Drawing. Those references are not normally added for a console application, so I'm not sure wether it's valid. Therefore I wrote a solution without references, however this has 580 287 Bytes:

using System;using System.Runtime.InteropServices;class P{static void Main()=>Console.Write(G().Item1+"x"+G().Item2);static(int,int)G()=>(GetSystemMetrics(0),GetSystemMetrics(1));[DllImport("User32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]static extern int GetSystemMetrics(int n);}

Here's the solution with line breaks:

using System;
using System.Runtime.InteropServices;

class P
{
    static void Main() => Console.Write(G().Item1 + "x" + G().Item2);
    static (int, int) G() => (GetSystemMetrics(0), GetSystemMetrics(1));

    [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    static extern int GetSystemMetrics(int n);
}

This solution was created with the huge help of AdamSchiavone on Stackoverflow.

I also wrote a C# Interactive programm with 123 Bytes, which automatically adds the references, so it can be executed on every machine without problems and without having the reference problems:

#r "System.Windows.Forms"
#r "System.Drawing"
var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;b.Width+"x"+b.Height

This basically is the same as the first one, but it adds the reference automatically and as it is executed in the interactive, you don't need the return statement, you can simply leave away the semicolon.

\$\endgroup\$
  • \$\begingroup\$ Your missing the ; on the end of your func. \$\endgroup\$ – TheLethalCoder May 4 '17 at 8:23
  • \$\begingroup\$ Side note: Those references aren't added normally for a Console application, they are for a Windows Forms one. Either way they don't need adding into the byte count. \$\endgroup\$ – TheLethalCoder May 4 '17 at 8:24
  • \$\begingroup\$ @TheLethalCoder You're right, edited my answer. \$\endgroup\$ – MetaColon May 4 '17 at 12:42
  • 1
    \$\begingroup\$ In your large solution you can change internal static class Program to just class P, you could probably remove static from the methods and use new P(), you can change IntPtr to int I believe, Move the i() out of the struct probably, change the struct to a class and probably a couple of other things. \$\endgroup\$ – TheLethalCoder May 4 '17 at 12:52
  • \$\begingroup\$ @TheLethalCoder I'll update my answer. Unfortunately, I won't have time to do this till tomorrow. \$\endgroup\$ – MetaColon May 4 '17 at 13:01
0
\$\begingroup\$

VBA (32-Bit), 110 Bytes

Locally declared windows function and anonymous VBE immediate window function that takes no input and outputs to the VBE Immediate window.

Note: The below may be made into a 64-bit compatible function by the addition of PtrSafe following Declare

Declare Function GetSystemMetrics Lib"user32.dll"(ByVal i&)As Long
?GetSystemMetrics(0)&"x"&GetSystemMetrics(1)
\$\endgroup\$
-1
\$\begingroup\$

Bash, 21 bytes

tr , x</*/*/*/fb0/v*

If the device has frame buffer you can query /sys/class/graphics/fb0/virtual_size to get resolution. Width and Height are delimited by , so tr translates to x. Path to the file is shortened by using * to the point there are no ambiguities.

\$\endgroup\$
  • \$\begingroup\$ On my machine that glob also matches 4 files + 1 symlink in /usr/share/games/fortunes/vi, so I have to specify …/*0/… instead of …/f*/… (not sure whether you can count on that “0”, I'm not framebuffer fan). \$\endgroup\$ – manatwork May 3 '17 at 13:22
  • \$\begingroup\$ That depends upon directories. \$\endgroup\$ – Pandya May 3 '17 at 14:04
  • \$\begingroup\$ several upvoted answers do it as well, edited for a less likely to hapen option \$\endgroup\$ – marcosm May 3 '17 at 14:10
  • 1
    \$\begingroup\$ Note that this gives the size of the console frame buffer, which for X11 is often quite different from the size of the screen you are sitting in front of when logged in over the network. \$\endgroup\$ – Jens May 3 '17 at 14:15
  • \$\begingroup\$ console framebuffer is fbcon, not fb0. This outputs resolution of screen connected as framebuffer device fb0. \$\endgroup\$ – marcosm May 3 '17 at 18:45

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.