Login
Username:

Password:

Remember me



Lost Password?

Register now!
Main Menu
Who is Online
12 user(s) are online (10 user(s) are browsing Forum)

Members: 0
Guests: 12

more...


Browsing this Thread:   1 Anonymous Users






Re: Decoding and encoding strings into numbers
#6
Just can't stay away
Just can't stay away


See User information
OK, I will try it. Maybe I was lucky because my numbers was <127 so is no matter if that signed or not :)

Posted on: 2011/6/5 19:15
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: Decoding and encoding strings into numbers
#5
Home away from home
Home away from home


See User information
(bumping this thread, so you don't miss it!)

Posted on: 2011/6/5 19:12
ChrisH
--
Author of the PortablE programming language.
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: Decoding and encoding strings into numbers
#4
Home away from home
Home away from home


See User information
@mrdarek
What I told you is not quite correct. Please use the CharToUnsigned() and UnsignedToChar() procedures. For example:

<pre>PROC example1()
DEF mystring1[3]:STRING
DEF mytable[3]:ARRAY OF INT

StringF(mystring1, '\s', '1Ab')

mytable[0] := CharToUnsigned(mystring1[0]) !!INT
mytable[1] := CharToUnsigned(mystring1[1]) !!INT
mytable[2] := CharToUnsigned(mystring1[2]) !!INT
ENDPROC

PROC example2()
DEF mystring1[3]:STRING
DEF mytable[3]:ARRAY OF INT, i

mytable[0] := 49
mytable[1] := 65
mytable[2] := 98

FOR i := 0 TO 2
mystring1[i] := UnsignedToChar(mytable[i])
ENDFOR
ENDPROC</pre>

The reason is that PortablE does not guarantee whether a CHAR is "RANGE -128 TO 127" or "RANGE 0 TO 255". So you need to use these procedures to ensure that it is always "RANGE 0 TO 255".

Posted on: 2011/6/5 18:54
ChrisH
--
Author of the PortablE programming language.
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: Decoding and encoding strings into numbers
#3
Just can't stay away
Just can't stay away


See User information
OK thanks. Here is my working code:
<pre>

DEF mystring1[3]:STRING
DEF mytable[3]: ARRAY OF INT
StringF(mystring1,'\s','1Ab')
mytable[0]:=mystring1[0]!!INT
mytable[1]:=mystring1[1]!!INT
mytable[2]:=mystring1[2]!!INT

reverse:

DEF mystring1[3]:STRING
DEF mytable[3]: ARRAY OF INT
mytable[0]:=49
mytable[1]:=65
mytable[2]:=98
FOR i:=0 TO 2
mystring1[i]:=mytable[i]!!CHAR
ENDFOR

</pre>

Posted on: 2011/6/5 13:25
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: Decoding and encoding strings into numbers
#2
Home away from home
Home away from home


See User information
@mrdarek
If you want to get ASCII values for each character in the string, then you do not need to do anything! mystring[0] already has the value 65. They only appear as characters if you treat them as such (say Print('first letter="\c"\n', mystring[0])), or as a string if you treat it as such (say Print('string="\s"\n', mystring)). Hope that answers your question?

Oh, and the length of the string is StrLen(mystring) .

Posted on: 2011/6/5 11:31
ChrisH
--
Author of the PortablE programming language.
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Decoding and encoding strings into numbers
#1
Just can't stay away
Just can't stay away


See User information
I need encoding string into numbers and I don't know how to do.
For example I have:

mystring[3]:STRING
mytable[3]:ARRAY OF INT
mystring:='ABC'

After procedure mytable[0] should be 65 (because A=65)
mytable[1] should be 66 and mytable[2] should be 67
I think that procedure should measure lenght of string and convert it using FOR LOOP...
But how do it?

And I need reverse procedure from this numeric table to 1 string....
Please help me???

Posted on: 2011/6/5 11:17
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 







You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[Advanced Search]


Search
Top Posters
1 paolone
paolone
4462
2 nikolaos
nikolaos
4206
3 magorium
magorium
4095
4 phoenixkonsole
phoenixkonsole
3942
5 deadwood
deadwood
2917
6 ncafferkey
ncafferkey
2810
7 mazze
mazze
2222
8 Kalamatee
Kalamatee
2212
9 clusteruk
clusteruk
2114
Powered by XOOPS © 2001-2025 The XOOPS Project