how to write a string to a c pointer?

zhmt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 4 23:39:53 PST 2015


I am writing a asio binding. Objects need to be serialized into a 
buffer (void *),

for example, write utf8 string into buffer,
write int into buffer,
write long into buffer,

Here is my class

class Buffer
{
	private void *ptr;
	private int size;
	private int _cap;

	public this(int cap)
	{
		ptr = malloc(cap);
		this._cap = cap;
	}

	public ~this()
	{
		free(ptr);
	}

	public ubyte[] asArray()
	{
		ubyte[] ret = (cast(ubyte*)ptr)[0..cap];
		return ret;
	}

	public void* getPtr()
	{
		return ptr;
	}

	public int cap()
	{
		return _cap;
	}
}

how can i write a utf8 string into the buffer?


More information about the Digitalmars-d-learn mailing list