LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2016, 02:31 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Convert ASCII To Characters Manually?


Is there a method or manual way to convert ASCII to characters without using a table?
 
Old 08-12-2016, 04:03 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You mean the numeric ASCII codes?

In C or C++, you just assign the number to a char variable.
 
Old 08-12-2016, 06:00 PM   #3
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
This is in reference to a blind sql injection attack example, where the sql statement is trying to determine if the first character of the database user is less than or equal to the letter B.

Code:
if (ascii(substring(user,1,1))<=66) waitfor delay '00:00:15'
Without looking at a chart like the following:

http://www.asciitable.com/

How can someone determine that the 66 correlates to the letter B?
 
Old 08-12-2016, 06:50 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Some ideas:

http://unix.stackexchange.com/a/92448
http://stackoverflow.com/q/890262/240515

The search term you want to use is "ordinal value to character" or something similar.

Or, just use the following Python 2 script:

Code:
#!/usr/bin/env python
import sys
print chr(int(sys.argv[1]))
Save it as "chr" and make it executable:

Then:

Code:
> chr 66
B

Last edited by dugan; 08-12-2016 at 06:58 PM.
 
Old 08-12-2016, 07:25 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by JockVSJock View Post
Without looking at a chart like the following:

http://www.asciitable.com/

How can someone determine that the 66 correlates to the letter B?
You can memorize the chart, or automate the lookup with a program as dugan suggested.
 
Old 08-15-2016, 07:52 AM   #6
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by ntubski View Post
You can memorize the chart
That is what I'm after. Is there a way to work it out, say like convert ip address to binary and vice versa?

thanks
 
Old 08-15-2016, 08:47 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
If you're talking about converting ASCII numbers to binary, why not:
Code:
input &= 0xdf; // optional for small letters
input -= 0x30;
if(input > 15) { input -= 7; }
I don't see any point to converting ASCII other values to binary. SPACE is 0x20, the value is "the value", what conversion would you do to that? Same thing for ASCII G, that is 0x47, what does G represent otherwise though? Meanwhile things like AF could be HEX AF which are ASCII { 0x41, 0x46 }, or in binary as decimal values they could be { A, F } or { 10, 15 }.

The other issue is what language? Because strictly C is one thing, and yes there are library functions, there are also very many library functions for C++, or C#, or Objective-C, or Java, ...

My example is me thinking from the perspective the code running without library support, no OS.

Last edited by rtmistler; 08-15-2016 at 08:49 AM.
 
Old 08-15-2016, 09:36 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by JockVSJock View Post
That is what I'm after. Is there a way to work it out, say like convert ip address to binary and vice versa?

thanks
Paste them into a web app that does the conversion?

BTW, your "that's what I'm after" is completely different from what you were asking up until now. If you're trying to steer the answers by strategically omitting information, you should know that never works.

Last edited by dugan; 08-15-2016 at 10:21 AM.
 
Old 08-15-2016, 10:01 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My first question would be like some of the others, what language?
 
Old 08-15-2016, 10:05 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by JockVSJock View Post
That is what I'm after. Is there a way to work it out, say like convert ip address to binary and vice versa?

thanks
You mean decompose the numbers into powers of two?
 
Old 08-15-2016, 10:37 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by JockVSJock View Post
That is what I'm after. Is there a way to work it out, say like convert ip address to binary and vice versa?

thanks
Missed this from earlier. I thought that was already in the inet library. For instance the sin_addr_t or addr_t structures I forget and not bothering to look it up, but one version requires a long which means you need it in binary, and another version takes is in w.x.y.z form. I recall something like inet_ntoa() and stuff like that. Just searching for example network code and viewing different examples will show these conversions or how to use a version that starts with an ASCII IP address and accepts it in that form. And I may be wrong about it being inet, if you just look at the various include files you need to add to do networking, it's in one or more of those include files.
 
Old 08-15-2016, 01:44 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by JockVSJock View Post
Quote:
You can memorize the chart
That is what I'm after. Is there a way to work it out, say like convert ip address to binary and vice versa?
Converting IP addresses is essentially decimal <-> binary conversion. You have to memorize the meaning of 10 digits for that.

ASCII encoding is pretty much base-128, so you have to memorize 128 "digits". There are some patterns in the chart that can help you memorize it.
 
Old 08-19-2016, 05:37 PM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Of course most modern system use UFT-8 encoding, not ASCII. So there are several different ways to represent, e.g., "B"

And your proposed test, even in an ASCII world, would not accomplish your stated goal. A test for N < 66 would accept a "B" and anything that followed B in the ASCII code table.

If you're really in an ASCII world, and the computer language you're using permits it, consider just using 'B' to represent 'B', and ignore the binary (or decimal, or ...) equivalents to that symbol. (I.e., What's the point in switching from one encoding scheme to some other one if the meaning of the symbol is all you care about?)
 
Old 08-20-2016, 07:16 AM   #14
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by dugan View Post

BTW, your "that's what I'm after" is completely different from what you were asking up until now. If you're trying to steer the answers by strategically omitting information, you should know that never works.
I'm not trying to omit any information, it my lack of experience and understanding of the topic, which is why I'm not able to ask the question clearly.

At one time you were new to learning something too, so you probably weren't sure how to go about asking the right question either.
 
Old 08-20-2016, 07:29 AM   #15
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by grail View Post
My first question would be like some of the others, what language?
One of the examples I'm seeing online pertain to using SQL, like the following:

Code:
1 and  
(
  select top 1 ascii(lower(substring(name, 1, 1))) from sysobjects where id=(
    select top 1 id from (
      select top 1 id from sysobjects where xtype='u' order by id
    ) sq order by id desc
  )
) > 109
https://www.troyhunt.com/everything-...now-about-sql/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert encoding between ASCII text and ASCII English text astanton Linux - General 2 08-03-2011 03:55 PM
Print all ASCII characters in C#? michaelinux Programming 2 03-10-2011 07:44 PM
ASCII characters in my script... Firebar Programming 9 10-27-2008 04:59 PM
ascii characters lakshman Linux - General 1 03-14-2003 11:28 AM
Deleting non ASCII characters Thinkgeekness Linux - Networking 4 03-04-2003 01:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration