By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

"hello world" to binary representation

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
On Feb 19, 12:06 pm, "Salvatore Di Fazio"
<salvatore.difa...@gmail.comwrote:
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

P: n/a
On Feb 19, 4:17 am, "Andrea" <aciru...@gmail.comwrote:
On Feb 19, 12:06 pm, "Salvatore Di Fazio"

<salvatore.difa...@gmail.comwrote:
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

P: n/a
"Andrea" <ac******@gmail.comwrites:
On Feb 19, 12:06 pm, "Salvatore Di Fazio"
<salvatore.difa...@gmail.comwrote:
>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*chrome.pl>--ooO--(_)--Ooo--
Feb 22 '07 #5

P: n/a
"Andrea" <ac******@gmail.comwrites:
>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???
Michal Nazarewicz <mi****@tlen.plwrites:
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*chrome.pl>--ooO--(_)--Ooo--
Feb 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.