473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

"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 9072
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.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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
6
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
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
13
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....
8
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.