"hello world" to binary representation

A

Andrea

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
 
S

Salvatore Di Fazio

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
 
A

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???
 
F

fdmfdmfdm

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);
 
M

Michal Nazarewicz

Andrea said:
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-
 
M

Michal Nazarewicz

Michal Nazarewicz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top