473,756 Members | 6,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"hello world" to binary representation

Hi, I'm a newbie i want to know how can i convert string to its binary
representation, i want to write a program where an example string
"hello world" is transformed in his binary representation
"11010010101... ." and viceversa and then print on the stdout the
result of the two representation.

thanks in advance,
Andrea

Feb 19 '07 #1
5 9098
On 19 Feb, 11:34, "Andrea" <aciru...@gmail .comwrote:
Hi, I'm a newbie i want to know how can i convert string to its binary
representation, i want to write a program where an example string
"hello world" is transformed in his binary representation
"11010010101... ." and viceversa and then print on the stdout the
result of the two representation.

thanks in advance,
Andrea
You can convert the char in the equivalent hex or int value and write
the value in binary representation

Feb 19 '07 #2
On Feb 19, 12:06 pm, "Salvatore Di Fazio"
<salvatore.difa ...@gmail.comwr ote:
On 19 Feb, 11:34, "Andrea" <aciru...@gmail .comwrote:
Hi, I'm a newbie i want to know how can i convert string to its binary
representation, i want to write a program where an example string
"hello world" is transformed in his binary representation
"11010010101... ." and viceversa and then print on the stdout the
result of the two representation.
thanks in advance,
Andrea

You can convert the char in the equivalent hex or int value and write
the value in binary representation
ok i can use sprintf to transform a string to hexadecimal
representation bot how can i back to the original string, with the
hexadecimal representation? ??

Feb 19 '07 #3
On Feb 19, 4:17 am, "Andrea" <aciru...@gmail .comwrote:
On Feb 19, 12:06 pm, "Salvatore Di Fazio"

<salvatore.difa ...@gmail.comwr ote:
On 19 Feb, 11:34, "Andrea" <aciru...@gmail .comwrote:
Hi, I'm a newbie i want to know how can i convert string to its binary
representation, i want to write a program where an example string
"hello world" is transformed in his binary representation
"11010010101... ." and viceversa and then print on the stdout the
result of the two representation.
thanks in advance,
Andrea
You can convert the char in the equivalent hex or int value and write
the value in binary representation

ok i can use sprintf to transform a string to hexadecimal
representation bot how can i back to the original string, with the
hexadecimal representation? ??
I think what you can do is:

char c;
//assign your hex value to c here
printf("%c", c);

Feb 19 '07 #4
"Andrea" <ac******@gmail .comwrites:
On Feb 19, 12:06 pm, "Salvatore Di Fazio"
<salvatore.difa ...@gmail.comwr ote:
>On 19 Feb, 11:34, "Andrea" <aciru...@gmail .comwrote:
Hi, I'm a newbie i want to know how can i convert string to its binary
representation, i want to write a program where an example string
"hello world" is transformed in his binary representation
"11010010101... ." and viceversa and then print on the stdout the
result of the two representation.
thanks in advance,
Andrea

You can convert the char in the equivalent hex or int value and write
the value in binary representation

ok i can use sprintf to transform a string to hexadecimal
representation bot how can i back to the original string, with the
hexadecimal representation? ??
Maybe that's what you want:

#v+
#include <stdio.h>
#include <stdlib.h>

int main(void) {
const char *message = "hello, world", *cc;
char msg_hex[25], msg_org[13], *c, *c2;

/* string -hex */
for (cc = message, c = msg_hex; *cc; ++cc, c += 2) {
sprintf(c, "%02X", *cc);
}
*c = 0;

/* hex -string */
for (c2 = msg_org, c = msg_hex; *c; c += 2, ++c2) {
char tmp = c[2];
c[2] = 0;
*c2 = strtol(c, 0, 16);
c[2] = tmp;
}
*c2 = 0;

/* print */
printf("message = %s\nmsg_hex = %s\nmsg_org = %s\n",
message, msg_hex, msg_org);
return 0;
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Feb 22 '07 #5
"Andrea" <ac******@gmail .comwrites:
>ok i can use sprintf to transform a string to hexadecimal
representati on bot how can i back to the original string, with the
hexadecimal representation? ??
Michal Nazarewicz <mi****@tlen.pl writes:
Maybe that's what you want:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
const char *message = "hello, world", *cc;
char msg_hex[25], msg_org[13], *c, *c2;

/* string -hex */
for (cc = message, c = msg_hex; *cc; ++cc, c += 2) {
sprintf(c, "%02X", *cc);
}
*c = 0;

/* hex -string */
for (c2 = msg_org, c = msg_hex; *c; c += 2, ++c2) {
char tmp = c[2];
c[2] = 0;
*c2 = strtol(c, 0, 16);
c[2] = tmp;
I've just realised that in C99 you can replace those four lines with:

#v+
sscanf(c, "%2hhx", c2);
#v-

however %hhx requires unsigned char not char which may introduce bugs
on same implementations .
}
*c2 = 0;

/* print */
printf("message = %s\nmsg_hex = %s\nmsg_org = %s\n",
message, msg_hex, msg_org);
return 0;
}
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Feb 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2195
by: ME | last post by:
I am trying to find a prewritten method for converting a string like this:"Hello World" to this "Hello\sWorld". I plan to use it to build a regular expression. Specifically I am looking for a method that can convert not just space (\s) but also any character that needs to be escaped in a given string. Thanks, Matt
6
4132
by: Matthew | last post by:
How would I go about creating a simple "hello world" program that will run in Unix. I am using MS Visual C++.
23
6633
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
13
2502
by: Hans | last post by:
Hello, Thanks for all your response on my question about signed/unsigned chars. The problem is this: I want to use the char array for a ArrayLookup, so I need to convert char to unsigned int. But this doesn't work! If I try this:
8
7598
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
4
6288
by: arnuld | last post by:
i am learning C and doing the exercise 1-1 of K&R2, where K&R ask to remove some parts of programme and experiment with error, so here i go: #include <stdio.h> int main () { printf('hello world\n'); }
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.