convert Character string into binary

Your source for all things Atmel.

Moderator: phalanx

Post Reply
caltex88
Posts: 1
Joined: Mon Oct 03, 2011 3:12 am

convert Character string into binary

Post by caltex88 » Mon Oct 03, 2011 3:24 am

Hi i had a ASCII string, how do i convert it into binary?

I was told to use bit masking to do it.

thanks in advance

buxtronix
Posts: 2
Joined: Thu Oct 06, 2011 12:52 am

Re: convert Character string into binary

Post by buxtronix » Thu Oct 06, 2011 3:00 am

For Arduino (you can adapt to your need if pure avr):

Code: Select all

void setup() {
  Serial.begin(9600);
  char *str = "0123";
  for (char *c = str ; *c ; c++)
    for (char b = 7 ; b > -1 ; b--)
      Serial.print((*c >> b) & 0x1 ? "1" : "0");
}

void loop() {
}

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Re: convert Character string into binary

Post by stevech » Thu Oct 06, 2011 8:55 pm

caltex88 wrote:Hi i had a ASCII string, how do i convert it into binary?

I was told to use bit masking to do it.

thanks in advance
Is the GPS NMEA data question again?

Post Reply