+ 50
Can someone explain why this code prints "Hello World!"?
Code: ``` #include <stdio.h> int main() { float buf[] = { 1.14313912243758250594e+27, 6.657848692049645644e+28, 7.73929309656277981375e-19, 0.0f }; puts((char*)buf); return 0; } ``` https://code.sololearn.com/c76xcLN2ege0/?ref=app
36 Antworten
+ 52
Basically, the floats in your array translate to "Hell", "o Wo", and "rld!" when the array is cast as a string.
https://code.sololearn.com/cn7hwBPTAMxT/#c
+ 20
@*Asterisk*: I don't really want to delve into the representation of floats in memory, but if you're interested in that, see here:
https://softwareengineering.stackexchange.com/questions/215065/can-anyone-explain-representation-of-float-in-memory
And if you don't understand how what is in memory is interpreted as a character, see the ascii character encoding:
https://en.wikipedia.org/wiki/ASCII
+ 13
@Nah Larweh Felix, @*Asterisk*: Basically, same data, different interpretation. As an array of floats it looks random, but when interpreted as a string you can see the hidden message.
+ 8
[union method]
I have some old codes where I was fiddling with internal bytes of floats/doubles using unions...and I adapted one for this question:
https://code.sololearn.com/cJDia7WpEXh5/?ref=app
This should be reviewed in combination with the other answers.
+ 7
Well I actually don't understand anything here can someone please help me make a tail of everything here? U really wanna learn but this is my first attempt attempt and I don't understand anything to be frank.
+ 7
Internal machine storage of any type is just a sequence of 1 and 0. In this case both float and char is representation of the same sequence. Just store as float and show as char[]. this is why compiler typecast usually raise warnings - you should understand what you do.
+ 6
[meta: ASCII] Da2 American Standard Code for Information Interchange
It's the standard chart of numbers representing the standard character glyphs, named by who was there to set those standards.
When the world wanted _their_ language glyphs we couldn't just stay with 1 byte for that...so ASCII -> Unicode.
+ 5
Zen It’s interesting that you call it a “hidden message.” It’s true that it is, in its broken-up form. This methodology reminds me of historical steganography (message hiding) methods, like breaking messages into strips of paper you’d have to roll into a tube to read. It could make for an interesting data-hiding program, or a way to send secret messages to other friends who program. In other words: sounds like fun! 😊
Bryant Great question!
+ 5
Next question is why this code prints "Hello World!" 🤷♂️
https://code.sololearn.com/cwqkWYdRRgdk/?ref=app
+ 5
Here is a more concise explanation of pointers
https://stackoverflow.com/questions/897366/how-do-pointer-to-pointers-work-in-c
+ 5
@sky_blue02: Yup. I would call it obfuscation in this case. If you want to exchange secret messages, I would use cryptography instead.
@Kirk Schafer: Union is definitely relevant here, thanks for your input.
+ 5
@Gordon: The code builds character by character the string "print('Hello World!')" and then evaluates it, printing "Hello World!". The function used to build the characters takes 3 strings, get their length, use the values to build a decimal number with the first length as the hundreds, the second as the tens, the third as the units, and then get the corresponding ascii character.
For example:
_("_","_","__") is chr(112), or 'p'
_("_","_","____") is chr(114), or 'r'
_("_","","_____") is chr(105), or 'i'
_("_","_","") is chr(110), or 'n'
_("_","_","______") is chr(116), or 't'
etc.
Again, you can refer to an ascii table to get the corresponding characters.
http://www.asciitable.com/
As the SL forums aren't great for references to other posts, here is the code I'm talking about:
https://code.sololearn.com/cwqkWYdRRgdk/?ref=app
+ 4
Actually its about conversion.
+ 4
Not sure this would be very interesting as a challenge, but it should be pretty easy to reverse the "union" in my program...to generate your own float array, as sky_blue02 hints.
I added a feed post if anyone feels like trying that.
+ 3
Thanks Zen
+ 3
The puzzle deals with deep concepts that high level language coders take for granted. ASCII codes is used to store character. Interpreters and compilers just translate the code.
+ 3
So what does Ascii mean😃
+ 3
Sk Tahir Faraz, start slow, master one or two languages. Then you might get a sense of this quite interesting exchange of ideas
+ 2
Zen what i don't understand about it is that how does that number make up a character
+ 2
Zen hell of explanation code....😂👊👏