charCodeAt() equivalent?

Hi everyone,

Is there an oF equivalent of charCodeAt() in JS? I am trying to return a unicode value for characters in a string. Similar to this JS…

//Return the Unicode of the first character in a string (the Unicode value for "H"):

var str = "HELLO WORLD";
var n = str.charCodeAt(0);
// returns 72

Thanks for any help.

if you have only ascii characters it’s as simple as

char c = str[2]; // third character

if you have utf8 characters you can’t really tell where in the string a character is, and you’ll have to iterate from the beginning while keeping track where in the string you are, something along the lines of:

static string utf8charAt( const string & str, int pos ){
    int i = 0;
    for( const uint32_t & c : ofUTF8Iterator(str)){
        if( i == pos ){
            string result = "";
            ofAppendUTF8(result, c);
            return result;
        }
        i++;
    }
    
    return "";
}

here’s an example to show how the basic myString[index] method fails with utf8:

string simpleStr = "abc";
cout << "str=" << simpleStr << endl;
cout << "  utf8charAt(1): " << utf8charAt(simpleStr, 1) << endl;
cout << "  charAt(1): " << simpleStr[1] << endl;

string uniStr = u8"\u2192\U0001F34C\u2190";
cout << "str=" << uniStr << endl;
cout << "  utf8charAt(1): " << utf8charAt(uniStr, 1) << endl;
cout << "  charAt(1): " << uniStr[1] << endl;

output: 
str=abc
  utf8charAt(1): b
  charAt(1): b
str=→🍌←
  utf8charAt(1): 🍌
  charAt(1): \206

there is some info on this in the last OF 0.9 blog post

Thank you @kritzikratzi but am not trying to find the location in the string I was hoping to return the characters unicode. In my example above …the JS returns 72 for H.

http://www.codetable.net/decimal/72

Basically I am trying to translate this JS function into oF.

var stringToColour = function(str) {
    var hash = 0;
    for (var i = 0; i < str.length; i++) {
        hash = str.charCodeAt(i) + ((hash << 5) - hash);
    }
    var colour = '#';
    for (var i = 0; i < 3; i++) {
        var value = (hash >> (i * 8)) & 0xFF;
        colour += ('00' + value.toString(16)).substr(-2);
    }
    return colour;
}

$('body').css('background-color', stringToColour("greenish"));

Apologies this thread may have wandered off my original ask. Basically I was trying to change a string into colour. Using a hash and converting to RGB worked perfectly.

void ofApp::stringtocolor(string convertstring){

    int hash = std::hash<std::string>()(convertstring);
    int r = (hash & 0xFF0000) >> 16;
    int g = (hash & 0x00FF00) >> 8;
    int b = hash & 0x0000FF;
    
    cout << r << "| "<< g << "| " << b << endl;

}


stringtocolor("thisstring");

ah, glad you got it working anyways.

btw., the method i sent you does return the character at a specific place, if you want to have just the codepoint (not the character) you can return without converting the codepoint back into a string:

static uint32_t utf8charCodeAt( const string & str, int pos ){
    int i = 0;
    for( const uint32_t & c : ofUTF8Iterator(str)){
        if( i == pos ){
            return c; 
        }
        i++;
    }
    
    return 0;
}
1 Like

Thank you! I will no doubt run into this problem soon. I appreciate the help.