+ 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

15th Jan 2019, 1:19 PM
Bryant
Bryant - avatar
36 Réponses
+ 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
15th Jan 2019, 4:43 PM
Zen
Zen - avatar
+ 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
16th Jan 2019, 3:52 PM
Zen
Zen - avatar
+ 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.
16th Jan 2019, 3:02 PM
Zen
Zen - avatar
+ 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.
18th Jan 2019, 2:13 AM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
16th Jan 2019, 12:36 PM
Nah Larweh Felix
Nah Larweh Felix - avatar
+ 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.
18th Jan 2019, 4:40 AM
Vents Lauva
Vents Lauva - avatar
+ 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.
18th Jan 2019, 5:49 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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!
17th Jan 2019, 10:23 PM
sky_blue02
sky_blue02 - avatar
+ 5
Next question is why this code prints "Hello World!" 🤷‍♂️ https://code.sololearn.com/cwqkWYdRRgdk/?ref=app
18th Jan 2019, 6:36 AM
Gordon
Gordon - avatar
18th Jan 2019, 7:53 AM
Da2
Da2 - avatar
+ 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.
18th Jan 2019, 11:42 AM
Zen
Zen - avatar
+ 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
18th Jan 2019, 12:16 PM
Zen
Zen - avatar
+ 4
Actually its about conversion.
15th Jan 2019, 1:42 PM
ShortCode
+ 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.
18th Jan 2019, 7:45 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Thanks Zen
16th Jan 2019, 3:57 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 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.
18th Jan 2019, 5:04 AM
Da2
Da2 - avatar
+ 3
So what does Ascii mean😃
18th Jan 2019, 12:55 PM
Da2
Da2 - avatar
+ 3
Sk Tahir Faraz, start slow, master one or two languages. Then you might get a sense of this quite interesting exchange of ideas
18th Jan 2019, 5:07 PM
Da2
Da2 - avatar
+ 2
Zen what i don't understand about it is that how does that number make up a character
16th Jan 2019, 3:42 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
Zen hell of explanation code....😂👊👏
16th Jan 2019, 5:15 PM
Akib
Akib - avatar