Help with strings
BlitzMax Forums/BlitzMax Beginners Area/Help with strings
| ||
I need some help dealing with strings. I want to be able to take a string and extract the letters as an ascii byte sequence, four at a time, and make the byte sequence an int... that is, the leftmost letter as a byte occupies bits 31-23, the second letter as a byte occupies bits 23-15 etc. I tried creating a bank, making a bank stream and using readint from the stream, but that seems horribly inefficient. Can someone direct me as to a far better and more direct method, preferably without bit shifts and without messing with arrays? Not that I am against any method that may use bit shifts or arrays, but I was just hoping there was a slicing and recasting method I am overlooking, or something like that. |
| ||
The simplest way is to use pointers:Local p:Int Ptr = Int Ptr "abcd1234".ToCString() Print p[0] ' abcd Print p[1] ' 1234 MemFree p |
| ||
Thanks grable, that worked nicely. I forgot to ask how do I reverse the process in the same sort of way? Is it some sort of CString related instruction too? |
| ||
how do I reverse the process in the same sort of way? Is it some sort of CString related instruction too? In a way, you must allocate enough space ahead of time and make it a string when done writing to it: Const LENGTH:Int = 8 Local p:Int Ptr = Int Ptr MemAlloc(LENGTH) p[0] = 1684234849 ' abcd p[1] = 875770417 ' 1234 Print String.FromBytes(p, LENGTH) MemFree p |