Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C convert a string into ascii codes.


  • Please log in to reply
4 replies to this topic

#1 Darktune

Darktune

    Very Purple


  • Members
  • 1,139 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Wales
  • Local time:07:13 AM

Posted 04 February 2015 - 04:30 PM

Hey everyone.

I'm new to the coding community and I am loving it so far. I'm learning C as part of my degree. Anyway I'm working on a project to encrypt a message using my own basic and I mean basic algorithm. Howler I've tried this using numbers and it works but if I enter text to be converted it doesn't work

This is because I need to convert the text that had been entered into ascii code which can then be changed by my encryption and then back again into the characters.

As I'm new I'm stil using scanf / printf commands so

scanf("%s", Wordtobeencrypted);

Would be the input method

Any help would be great.

Kind regards

It's very hard to imagine all the crazy things that things really are like. 

Electrons act like waves.. no they don't exactly, they act like particles.. no they don't exactly.

Words and ideas can change the world.


BC AdBot (Login to Remove)

 


#2 Sirawit

Sirawit

    Bleepin' Brony


  • Malware Response Team
  • 4,158 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Thailand
  • Local time:01:13 PM

Posted 04 February 2015 - 11:32 PM

int=char; format should work.

 

Like this:

#include<stdio.h>

int main()
{
int test[25]={},i=0,j;
char a[25]="This is a test string.";
while(a[i]!='\0') {test[i]=a[i]; i++;}
for(j=0;j<i-1;j++) printf("%d ",test[j]);
return 0;
}

results in:

84 104 105 115 32 105 115 32 97 32 116 101 115 116 32 115 116 114 105 110 103 

Hopes this helps.

 

Thank you.


If I don't reply back to you in 2 days, feel free to send me a PM.

 

“You’re lying… just like you were lying to me before. You have to hate me. I’ve been the worst daughter in the world… you should hate me.”

“But I don’t, Nyx. Because, Nyx, I’m your mother, and a mother will always love her daughter, no matter what.” -Past sins by Pen stroke.


#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:13 PM

Posted 05 February 2015 - 03:14 PM

Of course if you actually want to encrypt a message I hope you use one of the many good commercially available encryption tools (or at least implement/use a well regarded cipher like AES).

If this thing is just a toy, have you considered ROT13? :)

Billly3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 Darktune

Darktune

    Very Purple

  • Topic Starter

  • Members
  • 1,139 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Wales
  • Local time:07:13 AM

Posted 06 February 2015 - 09:17 AM

Thanks for your help!

Hey Billy,

 

Yeah If I was to do real encryption I'd never send anything using a method of my own, I'm not the best at advanced Math! :)

It's a college project were we are learning C and procedural programming so it's not going to be implemented in any way.

I have considered using a rotational encryption method but I'd still have to change the character to ascii before hand right?

 

Kind regards,


It's very hard to imagine all the crazy things that things really are like. 

Electrons act like waves.. no they don't exactly, they act like particles.. no they don't exactly.

Words and ideas can change the world.


#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:13 PM

Posted 06 February 2015 - 04:21 PM

I have considered using a rotational encryption method but I'd still have to change the character to ascii before hand right?

On most platforms it will already be in ASCII. char is a integral type -- it stores a number that represents the character. Sirawit's example above just takes that and asks printf to print it as a number instead of as a character.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users