Search code examples
powershellpowershell-ise

Odd school assignment, about displaying emojis in powershell


I've had the pleasure to get the assignment of posting emojis in Powershell, the only problem is they have to be on the same line, and there are three. This is, my first assignment, and we have no prior teaching in this subject so after googling and searching YouTube, my best shot was this below, however, it came with some error saying something about either too high value, or too low value.

Full error text: Exception calling "ToInt32" with "2" argument (s): "The value was either too large or too small to a UInt32. " At C: \ Users \ EG \ Downloads \ Herningsholm \ Powershell H1 \ Hardware Information.ps1: 3 char: 5 $ UnicodeInt = [System.Convert] :: toInt32 ($ StrippedUnicode, 16) CategoryInfo: NotSpecified: (:) [], MethodInvocationException FullyQualifiedErrorId: OverflowException

$FullUnicode = ('U+1F60E') + ('U+1F436') + ('U+1F642')
$StrippedUnicode = $FullUnicode -replace 'U\+',''
$UnicodeInt = [System.Convert]::toInt32($StrippedUnicode,16)
[System.Char]::ConvertFromUtf32($UnicodeInt)

Solution

  • Try this out: Full emoji list > here

    # saves unicode for each emoji https://unicode.org/emoji/charts/full-emoji-list.html
    $FullUnicode0 = 'U+1F606'
    $FullUnicode1 = 'U+1F605'
    $FullUnicode2 = 'U+1F605'
    
    # removes the U+ bit
    $StrippedUnicode0 = $FullUnicode0 -replace 'U\+',''
    $StrippedUnicode1 = $FullUnicode1 -replace 'U\+',''
    $StrippedUnicode2 = $FullUnicode2 -replace 'U\+',''
    
    # Converts the value of the specified object to a 32-bit signed integer
    $UnicodeInt0 = [System.Convert]::toInt32($StrippedUnicode0,16)
    $UnicodeInt1 = [System.Convert]::toInt32($StrippedUnicode1,16)
    $UnicodeInt2 = [System.Convert]::toInt32($StrippedUnicode2,16)
    
    # Converts the specified Unicode code point into a UTF-16 encoded string so that you have an emoji
    $Emoji0 = [System.Char]::ConvertFromUtf32($UnicodeInt0)
    $Emoji1 = [System.Char]::ConvertFromUtf32($UnicodeInt1)
    $Emoji2 = [System.Char]::ConvertFromUtf32($UnicodeInt2)
    
    write-host "$($Emoji0), $($Emoji1), $($Emoji2)" 
    

    enter image description here