PDA

View Full Version : GetASCII, Get the ascii code of characters



R0b0t1
10-06-2007, 01:51 PM
Outputs basic ASCII in either decimal, hexadecimal, or octal.

I don't see why it should do extended ASCII, because those aren't even on the keyboard...

Character: A single character, I'm not sure if it will do the things like NAK or ACK or NUL, but it should do your keyboard.

Numerical: How you want the code to be outputted. Values are: 'Oct', 'Dec', and 'Hex' and are not case sensitive.


function GetASCII(Character, Numerical: String): String;
var
I: Integer;
Dec, Hex, Oct: Array [0..127] of String;
begin
Result:= '';

Dec[0]:= '0'; Hex[0]:= '$0'; Oct[0]:= '#0';
Dec[1]:= '1'; Hex[1]:= '$1'; Oct[1]:= '#1';
Dec[2]:= '2'; Hex[2]:= '$2'; Oct[2]:= '#2';
Dec[3]:= '3'; Hex[3]:= '$3'; Oct[3]:= '#3';
Dec[4]:= '4'; Hex[4]:= '$4'; Oct[4]:= '#4';
Dec[5]:= '5'; Hex[5]:= '$5'; Oct[5]:= '#5';
Dec[6]:= '6'; Hex[6]:= '$6'; Oct[6]:= '#6';
Dec[7]:= '7'; Hex[7]:= '$7'; Oct[7]:= '#7';
Dec[8]:= '8'; Hex[8]:= '$8'; Oct[8]:= '#10';
Dec[9]:= '9'; Hex[9]:= '$9'; Oct[9]:= '#11';
Dec[10]:= '10'; Hex[10]:= '$A'; Oct[10]:= '#12';
Dec[11]:= '11'; Hex[11]:= '$B'; Oct[11]:= '#13';
Dec[12]:= '12'; Hex[12]:= '$C'; Oct[12]:= '#14';
Dec[13]:= '13'; Hex[13]:= '$D'; Oct[13]:= '#15';
Dec[14]:= '14'; Hex[14]:= '$E'; Oct[14]:= '#16';
Dec[15]:= '15'; Hex[15]:= '$F'; Oct[15]:= '#17';
Dec[16]:= '16'; Hex[16]:= '$10'; Oct[16]:= '#20';
Dec[17]:= '17'; Hex[17]:= '$11'; Oct[17]:= '#21';
Dec[18]:= '18'; Hex[18]:= '$12'; Oct[18]:= '#22';
Dec[19]:= '19'; Hex[19]:= '$13'; Oct[19]:= '#23';
Dec[20]:= '20'; Hex[20]:= '$14'; Oct[20]:= '#24';
Dec[21]:= '21'; Hex[21]:= '$15'; Oct[21]:= '#25';
Dec[22]:= '22'; Hex[22]:= '$16'; Oct[22]:= '#26';
Dec[23]:= '23'; Hex[23]:= '$17'; Oct[23]:= '#27';
Dec[24]:= '24'; Hex[24]:= '$18'; Oct[24]:= '#30';
Dec[25]:= '25'; Hex[25]:= '$19'; Oct[25]:= '#31';
Dec[26]:= '26'; Hex[26]:= '$1A'; Oct[26]:= '#32';
Dec[27]:= '27'; Hex[27]:= '$1B'; Oct[27]:= '#33';
Dec[28]:= '28'; Hex[28]:= '$1C'; Oct[28]:= '#34';
Dec[29]:= '29'; Hex[29]:= '$1D'; Oct[29]:= '#35';
Dec[30]:= '30'; Hex[30]:= '$1E'; Oct[30]:= '#36';
Dec[31]:= '31'; Hex[31]:= '$1F'; Oct[31]:= '#37';
Dec[32]:= '32'; Hex[32]:= '$20'; Oct[32]:= '#40';
Dec[33]:= '33'; Hex[33]:= '$21'; Oct[33]:= '#41';
Dec[34]:= '34'; Hex[34]:= '$22'; Oct[34]:= '#42';
Dec[35]:= '35'; Hex[35]:= '$23'; Oct[35]:= '#43';
Dec[36]:= '36'; Hex[36]:= '$24'; Oct[36]:= '#44';
Dec[37]:= '37'; Hex[37]:= '$25'; Oct[37]:= '#45';
Dec[38]:= '38'; Hex[38]:= '$26'; Oct[38]:= '#46';
Dec[39]:= '39'; Hex[39]:= '$27'; Oct[39]:= '#47';
Dec[40]:= '40'; Hex[40]:= '$28'; Oct[40]:= '#50';
Dec[41]:= '41'; Hex[41]:= '$29'; Oct[41]:= '#51';
Dec[42]:= '42'; Hex[42]:= '$2A'; Oct[42]:= '#52';
Dec[43]:= '43'; Hex[43]:= '$2B'; Oct[43]:= '#53';
Dec[44]:= '44'; Hex[44]:= '$2C'; Oct[44]:= '#54';
Dec[45]:= '45'; Hex[45]:= '$2D'; Oct[45]:= '#55';
Dec[46]:= '46'; Hex[46]:= '$2E'; Oct[46]:= '#56';
Dec[47]:= '47'; Hex[47]:= '$2F'; Oct[47]:= '#57';
Dec[48]:= '48'; Hex[48]:= '$30'; Oct[48]:= '#60';
Dec[49]:= '49'; Hex[49]:= '$31'; Oct[49]:= '#61';
Dec[50]:= '50'; Hex[50]:= '$32'; Oct[50]:= '#62';
Dec[51]:= '51'; Hex[51]:= '$33'; Oct[51]:= '#63';
Dec[52]:= '52'; Hex[52]:= '$34'; Oct[52]:= '#64';
Dec[53]:= '53'; Hex[53]:= '$35'; Oct[53]:= '#65';
Dec[54]:= '54'; Hex[54]:= '$36'; Oct[54]:= '#66';
Dec[55]:= '55'; Hex[55]:= '$37'; Oct[55]:= '#67';
Dec[56]:= '56'; Hex[56]:= '$38'; Oct[56]:= '#70';
Dec[57]:= '57'; Hex[57]:= '$39'; Oct[57]:= '#71';
Dec[58]:= '58'; Hex[58]:= '$3A'; Oct[58]:= '#72';
Dec[59]:= '59'; Hex[59]:= '$3B'; Oct[59]:= '#73';
Dec[60]:= '60'; Hex[60]:= '$3C'; Oct[60]:= '#74';
Dec[61]:= '61'; Hex[61]:= '$3D'; Oct[61]:= '#75';
Dec[62]:= '62'; Hex[62]:= '$3E'; Oct[62]:= '#76';
Dec[63]:= '63'; Hex[63]:= '$3F'; Oct[63]:= '#77';
Dec[64]:= '64'; Hex[64]:= '$40'; Oct[64]:= '#100';
Dec[65]:= '65'; Hex[65]:= '$41'; Oct[65]:= '#101';
Dec[66]:= '66'; Hex[66]:= '$42'; Oct[66]:= '#102';
Dec[67]:= '67'; Hex[67]:= '$43'; Oct[67]:= '#103';
Dec[68]:= '68'; Hex[68]:= '$44'; Oct[68]:= '#104';
Dec[69]:= '69'; Hex[69]:= '$45'; Oct[69]:= '#105';
Dec[70]:= '70'; Hex[70]:= '$46'; Oct[70]:= '#106';
Dec[71]:= '71'; Hex[71]:= '$47'; Oct[71]:= '#107';
Dec[72]:= '72'; Hex[72]:= '$48'; Oct[72]:= '#110';
Dec[73]:= '73'; Hex[73]:= '$49'; Oct[73]:= '#111';
Dec[74]:= '74'; Hex[74]:= '$4A'; Oct[74]:= '#112';
Dec[75]:= '75'; Hex[75]:= '$4B'; Oct[75]:= '#113';
Dec[76]:= '76'; Hex[76]:= '$4C'; Oct[76]:= '#114';
Dec[77]:= '77'; Hex[77]:= '$4D'; Oct[77]:= '#115';
Dec[78]:= '78'; Hex[78]:= '$4E'; Oct[78]:= '#116';
Dec[79]:= '79'; Hex[79]:= '$4F'; Oct[79]:= '#117';
Dec[80]:= '80'; Hex[80]:= '$50'; Oct[80]:= '#120';
Dec[81]:= '81'; Hex[81]:= '$51'; Oct[81]:= '#121';
Dec[82]:= '82'; Hex[82]:= '$52'; Oct[82]:= '#122';
Dec[83]:= '83'; Hex[83]:= '$53'; Oct[83]:= '#123';
Dec[84]:= '84'; Hex[84]:= '$54'; Oct[84]:= '#124';
Dec[85]:= '85'; Hex[85]:= '$55'; Oct[85]:= '#125';
Dec[86]:= '86'; Hex[86]:= '$56'; Oct[86]:= '#126';
Dec[87]:= '87'; Hex[87]:= '$57'; Oct[87]:= '#127';
Dec[88]:= '88'; Hex[88]:= '$58'; Oct[88]:= '#130';
Dec[89]:= '89'; Hex[89]:= '$59'; Oct[89]:= '#131';
Dec[90]:= '90'; Hex[90]:= '$5A'; Oct[90]:= '#132';
Dec[91]:= '91'; Hex[91]:= '$5B'; Oct[91]:= '#133';
Dec[92]:= '92'; Hex[92]:= '$5C'; Oct[92]:= '#134';
Dec[93]:= '93'; Hex[93]:= '$5D'; Oct[93]:= '#135';
Dec[94]:= '94'; Hex[94]:= '$5E'; Oct[94]:= '#136';
Dec[95]:= '95'; Hex[95]:= '$5F'; Oct[95]:= '#137';
Dec[96]:= '96'; Hex[96]:= '$60'; Oct[96]:= '#140';
Dec[97]:= '97'; Hex[97]:= '$61'; Oct[97]:= '#141';
Dec[98]:= '98'; Hex[98]:= '$62'; Oct[98]:= '#142';
Dec[99]:= '99'; Hex[99]:= '$63'; Oct[99]:= '#143';
Dec[100]:= '100'; Hex[100]:= '$64'; Oct[100]:= '#144';
Dec[101]:= '101'; Hex[101]:= '$65'; Oct[101]:= '#145';
Dec[102]:= '102'; Hex[102]:= '$66'; Oct[102]:= '#146';
Dec[103]:= '103'; Hex[103]:= '$67'; Oct[103]:= '#147';
Dec[104]:= '104'; Hex[104]:= '$68'; Oct[104]:= '#150';
Dec[105]:= '105'; Hex[105]:= '$69'; Oct[105]:= '#151';
Dec[106]:= '106'; Hex[106]:= '$6A'; Oct[106]:= '#152';
Dec[107]:= '107'; Hex[107]:= '$6B'; Oct[107]:= '#153';
Dec[108]:= '108'; Hex[108]:= '$6C'; Oct[108]:= '#154';
Dec[109]:= '109'; Hex[109]:= '$6D'; Oct[109]:= '#155';
Dec[110]:= '110'; Hex[110]:= '$6E'; Oct[110]:= '#156';
Dec[111]:= '111'; Hex[111]:= '$6F'; Oct[111]:= '#157';
Dec[112]:= '112'; Hex[112]:= '$70'; Oct[112]:= '#160';
Dec[113]:= '113'; Hex[113]:= '$71'; Oct[113]:= '#161';
Dec[114]:= '114'; Hex[114]:= '$72'; Oct[114]:= '#162';
Dec[115]:= '115'; Hex[115]:= '$73'; Oct[115]:= '#163';
Dec[116]:= '116'; Hex[116]:= '$74'; Oct[116]:= '#164';
Dec[117]:= '117'; Hex[117]:= '$75'; Oct[117]:= '#165';
Dec[118]:= '118'; Hex[118]:= '$76'; Oct[118]:= '#166';
Dec[119]:= '119'; Hex[119]:= '$77'; Oct[119]:= '#167';
Dec[120]:= '120'; Hex[120]:= '$78'; Oct[120]:= '#170';
Dec[121]:= '121'; Hex[121]:= '$79'; Oct[121]:= '#171';
Dec[122]:= '122'; Hex[122]:= '$7A'; Oct[122]:= '#172';
Dec[123]:= '123'; Hex[123]:= '$7B'; Oct[123]:= '#173';
Dec[124]:= '124'; Hex[124]:= '$7C'; Oct[124]:= '#174';
Dec[125]:= '125'; Hex[125]:= '$7D'; Oct[125]:= '#175';
Dec[126]:= '126'; Hex[126]:= '$7E'; Oct[126]:= '#176';
Dec[127]:= '127'; Hex[127]:= '$7F'; Oct[127]:= '#177';

For I:= 0 to 127 do
case Character of
chr(I): Break;
end;

case LowerCase(Numerical) of
'oct': Result:= Oct[I];
'dec': Result:= Dec[I];
'hex': Result:= Hex[I];
end;
end;

Santa_Clause
10-06-2007, 02:13 PM
Nice, nice.

Can you please shorten it?

Town
10-06-2007, 03:40 PM
Nice, nice.

Can you please shorten it?

function GetASCII(Character, Numerical: String): String;
var
p: String;
i: Integer;
begin
for i := 0 to 127 do
case Character of
chr(I): Break;
end;

case LowerCase(Numerical) of
'oct': begin
p := IntToStr(i - (i div 8) + ((i - (i mod 8)) div 8) * 3);
if (i < 8) then Result := ('#' + IntToStr(i)) else if (i > 63) then Result := ('#' + IntToStr(StrToInt(p) + 20)) else Result := ('#' + p);
// if (i < 8) then WriteLN('#' + IntToStr(i)) else if (i > 63) then WriteLN('#' + IntToStr(StrToInt(p) + 20)) else WriteLN('#' + p);
end;
'dec': Result := IntToStr(i);
'hex': begin
p := IntToStr(i - (6 * ((i - (i mod 16)) div 16)));
case (i mod 16) of
10: p := IntToStr(i div 16) + 'A';
11: p := IntToStr(i div 16) + 'B';
12: p := IntToStr(i div 16) + 'C';
13: p := IntToStr(i div 16) + 'D';
14: p := IntToStr(i div 16) + 'E';
15: p := IntToStr(i div 16) + 'F';
end;
Result := ('$' + p);
// WriteLN('$' + p);
end;
end;
end;

Shortened.

PwNZoRNooB
10-06-2007, 08:53 PM
I've googled some ASCII codes on how to make them into an text. Only one I remember Alt + 0169 = ©
:) Needed it for something ages ago :p

shaunthasheep
10-06-2007, 10:41 PM
for dec

program New;
begin
Writeln(inttostr(ord('a')));
Writeln('Alt + ' + inttostr(ord('©')));
end.

danrox2004
10-07-2007, 12:45 AM
for dec

program New;
begin
Writeln(inttostr(ord('a')));
Writeln('Alt + ' + inttostr(ord('©')));
end.


And I made a base converter a while ago... I will see if i can find it

Santa_Clause
10-07-2007, 12:53 AM
Dec := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127];

You can do the rest.

R0b0t1
10-08-2007, 09:35 PM
But I like big arrays!

Besides, although it could be shorter, it takes 10 MS, which is pretty small for SCAR. I don't feel like optimizing it.

Santa_Clause
10-09-2007, 02:30 AM
Suit yourself, sir...I always thought less lines was better...oh well...seems I was wrong.

Town
10-09-2007, 02:42 AM
Was mine not good enough for you? :(


function GetASCII(Character, Numerical: String): String;
var
p: String;
i: Integer;
begin
for i := 0 to 127 do
case Character of
chr(I): Break;
end;

case LowerCase(Numerical) of
'oct': begin
p := IntToStr(i - (i div 8) + ((i - (i mod 8)) div 8) * 3);
if (i < 8) then Result := ('#' + IntToStr(i)) else if (i > 63) then Result := ('#' + IntToStr(StrToInt(p) + 20)) else Result := ('#' + p);
// if (i < 8) then WriteLN('#' + IntToStr(i)) else if (i > 63) then WriteLN('#' + IntToStr(StrToInt(p) + 20)) else WriteLN('#' + p);
end;
'dec': Result := IntToStr(i);
'hex': begin
p := IntToStr(i - (6 * ((i - (i mod 16)) div 16)));
case (i mod 16) of
10: p := IntToStr(i div 16) + 'A';
11: p := IntToStr(i div 16) + 'B';
12: p := IntToStr(i div 16) + 'C';
13: p := IntToStr(i div 16) + 'D';
14: p := IntToStr(i div 16) + 'E';
15: p := IntToStr(i div 16) + 'F';
end;
Result := ('$' + p);
// WriteLN('$' + p);
end;
end;
end;

Shortened.

Santa_Clause
10-09-2007, 03:26 AM
Hah! Ofcourse it was...I was thinking that Robot could use the same method, arrays, but shorten it. Yours still owns though ;)

NewToAutoing
10-09-2007, 09:49 PM
Dec := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127];

You can do the rest.

So thats why you asked me to do that...I think xD

R0b0t1
10-09-2007, 10:12 PM
Meh, Town... Lol.


Also, when I make big arrays like that, I just make a script with a couple for loops and a writeline in it.

Santa_Clause
10-12-2007, 08:47 AM
So thats why you asked me to do that...I think xD

Self explanatory :p