Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to print Runes as individual characters?

Program's Purpose: Rune Cipher

Edit:

Note - I am linking to my Own Github page below (it is only for purpose-purpose (no joke intended; it is only for the purpose of showing the purpose of it - what I needed help with (and got help, thanks once again to all of you!)


Final Edit:

I have now (thanks to the Extremely Useful answers provided by the Extremely Amazing People) Completed the project I've been working on; and - for future readers I am also providing the full code.

Again, This wouldn't have been possible without all the help I got from the guys below, thanks to them - once again!

Original code on GitHub

Code

(Shortened down a bit)

#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#define UNICODE_BLOCK_START 0x16A0
#define UUICODE_BLOCK_END   0x16F1

int main(){
  setlocale(LC_ALL, "");
  wchar_t SUBALPHA[]=L"ᛠᚣᚫᛞᛟᛝᛚᛗᛖᛒᛏᛋᛉᛈᛇᛂᛁᚾᚻᚹᚷᚳᚱᚩᚦᚢ";
  wchar_t DATA[]=L"hello";
  
    int lenofData=0;
    int i=0;

    while(DATA[i]!='\0'){
          lenofData++;  i++;
          }

  for(int i=0; i<lenofData; i++) {
      printf("DATA[%d]=%lc",i,DATA[i]);
      DATA[i]=SUBALPHA[i];
      printf(" is now Replaced by %lc\n",DATA[i]); 
      }        printf("%ls",DATA);

return 0;
}

Output:

DATA[0]=h is now Replaced by ᛠ
DATA[1]=e is now Replaced by ᚣ
DATA[2]=l is now Replaced by ᚫ
DATA[3]=l is now Replaced by ᛞ
DATA[4]=o is now Replaced by ᛟ
ᛠᚣᚫᛞᛟ

Question continues below

(Note that it's solved, see Accepted answer!)

In Python3 it is easy to print runes:

for i in range(5794,5855):
print(chr(i))

outputs

ᚢ ᚣ (..) ᛝ ᛞ

How to do that in C ?

  • using variables (char, char arrays[], int, ...)

Is there a way to e.g print ᛘᛙᛚᛛᛜᛝᛞ as individual characters?

When I try it, it just prints out both warnings about multi-character character constant 'ᛟ'.

I have tried having them as an array of char, a "string" (e.g char s1 = "ᛟᛒᛓ";)

  • And then print out the first (ᛟ) char of s1: printf("%c", s1[0]); Now, this might seem very wrong to others.

One Example of how I thought of going with this:

Print a rune as "a individual character":

To print e.g 'A'

  • printf("%c", 65); // 'A'

How do I do that, (if possible) but with a Rune ?

I have as well as tried printing it's digit value to char, which results in question marks, and - other, "undefined" results.

As I do not really remember exactly all the things I've tried so far, I will try my best to formulate this post.

If someone spots a a very easy (maybe, to him/her - even plain-obvious) solution(or trick/workaround) -

I would be super happy if you could point it out! Thanks!

This has bugged me for quite a while now, It works in python though - and it works (as far as I know) in c if you just "print" it (not trough any variable) but, e.g: printf("ᛟ"); this works, but as I said I want to do the same thing but, trough variables. (like, char runes[]="ᛋᛟ";) and then: printf("%c", runes[0]); // to get 'ᛋ' as the output

(Or similar, it does not need to be %c, as well as it does not need to be a char array/char variable) I am just trying to understand how to - do the above, (hopefully not too unreadable)

I am on Linux, and using GCC.

External Links

Python3 Cypher's - At GitHub

Viewing Runes - At Unix&Linux SE

junicode - At Sourceforge.io

like image 650
William Martens Avatar asked Feb 27 '21 16:02

William Martens


People also ask

How do you turn a rune into a string?

If you just want to convert a single rune to string , use a simple type conversion. rune is alias for int32 , and converting integer numbers to string : Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.

What is rune datatype?

A rune is an alias to the int32 data type. It represents a Unicode code point. A Unicode code point or code position is a numerical value that is usually used to represent a Unicode character. The int32 is big enough to represent the current volume of 140,000 unicode characters.

What is [] rune Golang?

It represents a Rune constant, where an integer value recognizes a Unicode code point. In Go language, a Rune Literal is expressed as one or more characters enclosed in single quotes like 'g', '\t', etc. In between single quotes, you are allowed to place any character except a newline and an unescaped single quote.

What are Golang code points?

Code points, characters, and runes The Unicode standard uses the term “code point” to refer to the item represented by a single value. The code point U+2318, with hexadecimal value 2318, represents the symbol ⌘.


1 Answers

To hold a character outside of the 8-bit range, you need a wchar_t (which isn't necessarily Unicode). Although wchar_t is a fundamental C type, you need to #include <wchar.h> to use it, and to use the wide character versions of string and I/O functions (such as putwc shown below).

You also need to ensure that you have activated a locale which supports wide characters, which should be the same locale as is being used by your terminal emulator (if you are writing to a terminal). Normally, that will be the default locale, selected with the string "".

Here's a simple equivalent to your Python code:

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main(void) {
  setlocale(LC_ALL, "");
  /* As indicated in a comment, I should have checked the
   * return value from `putwc`; if it returns EOF and errno
   * is set to EILSEQ, then the current locale can't handle
   * runic characters.
   */
  for (wchar_t wc = 5794; wc < 5855; ++wc)
    putwc(wc, stdout);
  putwc(L'\n', stdout);
  return 0;
}

(Live on ideone.)

like image 141
rici Avatar answered Nov 08 '22 22:11

rici