How to make a ulong array that can be used as a string?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
abcdef4bfd
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am

How to make a ulong array that can be used as a string?

Post by abcdef4bfd »

I'm today starting implementing UTF-8 for my OS, and got a problem. I'm shortly rewritten the vgaPutchar function declaration, and it's working fine. But when i'm trying to print strings, i don't know what to do. Simple char* string can't handle ulong numbers, and overflow is appearing. So, how to properly declare an unsigned long array or pointer to make it initializable with extended string literals, and, if it's not available all the way, how to just write Unicode strings?
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: How to make a ulong array that can be used as a string?

Post by jnc100 »

You need to decide how you wish to store Unicode strings internally. There are many formats varying on both the data type used and the actual encoding. There is an overview here of how the various string literal operators ("", L"", u"", U"", and u8"") map to data types.

Note that UTF-8 (which you want to use), is normally stored as a char[] array, with the difference from a normal string being that there is not a direct one-to-one mapping between array entries and code points.

I don't understand why you are trying to use a numeric type to store the characters. What exactly are you trying to do?

Regards,
John.
User avatar
araxestroy
Member
Member
Posts: 561
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: araxestroy
Location: Vancouver, Canada
Contact:

Re: How to make a ulong array that can be used as a string?

Post by araxestroy »

C11 includes char16_t and char32_t types for storing UTF-16 and UTF-32 encoded data. Prefixing character literals and string literals with u and U will respectively declare the literals as UTF-16 and UTF-32 respectively. In C11, these types aren't fundamental, but rather implemented in uchar.h, so you'll need to include them. In C++11, they're fundamental types.

uchar.h also contains function declarations for four functions for handling conversion between multibyte character sequences (UTF-8) and fixed-width Unicode characters (UTF-16 and UTF-32). You need to implement them yourself though.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to make a ulong array that can be used as a string?

Post by Combuster »

The slightly older way is to use wchar_t strings, which you can initialize just like char strings. This will let the compiler configuration dictate the character size though
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply