+static
+void uprv_appendByteToHexString(char *dst, uint8_t val) {
+ uint32_t len = (uint32_t)uprv_strlen(dst);
+ *(dst+len) = T_CString_itosOffset((val >> 4));
+ *(dst+len+1) = T_CString_itosOffset((val & 0xF));
+ *(dst+len+2) = 0;
+}
+
+/* this function makes a string with representation of a sortkey */
+static char* U_EXPORT2 sortKeyToString(const UCollator *coll, const uint8_t *sortkey, char *buffer, uint32_t *len) {
+ int32_t strength = UCOL_PRIMARY;
+ uint32_t res_size = 0;
+ UBool doneCase = FALSE;
+ UErrorCode errorCode = U_ZERO_ERROR;
+
+ char *current = buffer;
+ const uint8_t *currentSk = sortkey;
+
+ uprv_strcpy(current, "[");
+
+ while(strength <= UCOL_QUATERNARY && strength <= ucol_getStrength(coll)) {
+ if(strength > UCOL_PRIMARY) {
+ uprv_strcat(current, " . ");
+ }
+ while(*currentSk != 0x01 && *currentSk != 0x00) { /* print a level */
+ uprv_appendByteToHexString(current, *currentSk++);
+ uprv_strcat(current, " ");
+ }
+ if(ucol_getAttribute(coll, UCOL_CASE_LEVEL, &errorCode) == UCOL_ON && strength == UCOL_SECONDARY && doneCase == FALSE) {
+ doneCase = TRUE;
+ } else if(ucol_getAttribute(coll, UCOL_CASE_LEVEL, &errorCode) == UCOL_OFF || doneCase == TRUE || strength != UCOL_SECONDARY) {
+ strength ++;
+ }
+ if (*currentSk) {
+ uprv_appendByteToHexString(current, *currentSk++); /* This should print '01' */
+ }
+ if(strength == UCOL_QUATERNARY && ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &errorCode) == UCOL_NON_IGNORABLE) {
+ break;
+ }
+ }
+
+ if(ucol_getStrength(coll) == UCOL_IDENTICAL) {
+ uprv_strcat(current, " . ");
+ while(*currentSk != 0) {
+ uprv_appendByteToHexString(current, *currentSk++);
+ uprv_strcat(current, " ");
+ }
+
+ uprv_appendByteToHexString(current, *currentSk++);
+ }
+ uprv_strcat(current, "]");
+
+ if(res_size > *len) {
+ return NULL;
+ }
+
+ return buffer;
+}
+