# Get char codes or generate an integer from string

**URL:** <https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119>\
**Category:** support\
**Created:** [February 12, 2022, 8:55am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119 "2022-02-12T08:55:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![donggas90](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/donggas90/32/16230_2.png) [@donggas90](https://discourse.gohugo.io/u/donggas90)\
**Post date:** [February 12, 2022, 8:55am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/1 "2022-02-12T08:55:30Z")

</div>

I want to generate an integer from a given string for select an item from an array as index.  
The integer must be the same if given strings are the same.  
But no matter what if the strings are different.

This can be done with hash code generation algorithms but I cannot find proper function in Hugo.  
`md5`, `sha` and `base64` are generating a hash string instead of an integer.

On the other hands, if can get char code from the given string, can make an integer by SUM or XOR.  
However, it seems there is no function to get char code.

I’ve tried `int` but runtime failed for non-digit characters.

```auto
{{ $str := `Test String Sequence` }}
{{ $len := (len $str) }}
{{ range (seq $len)}}
    {{ $charStr := (substr $str . 1)}}
    <p>{{ $charStr }}{{ int $charStr }}</p>
{{ end }}

```

Is there any way to figure out?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 9:43am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/2 "2022-02-12T09:43:58Z")

</div>

```auto
{{ $s := "This is nuts!" }}

{{ $s = split $s "" }}
{{ $s = apply $s "printf" "%#x" "." }}
{{ $s = apply $s "int" "." }}
{{ $s = delimit $s "" }}

{{ $s }} --> 84104105115321051153211011711611533

```

---

<div class="post-metadata">

**Author:** ![donggas90](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/donggas90/32/16230_2.png) [@donggas90](https://discourse.gohugo.io/u/donggas90)\
**Post date:** [February 12, 2022, 10:20am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/3 "2022-02-12T10:20:26Z")

</div>

```auto
{{ $codeTable := newScratch }}
{{ $codeTable.Set `$` 36 }}
{{ $codeTable.Set `+` 43 }}
{{ $codeTable.Set `0` 48 }}
...
{{ $codeTable.Set `u` 252 }}
{{ $codeTable.Set `y` 253 }}
{{ $codeTable.Set `þ` 254 }}

```

```auto
{{ $itemCount := 16 }}
{{ $scratch := newScratch }}
{{ $scratch.Set "Sum" 0 }}
{{ $len := (len $str) }}
{{ range (seq 0 $len) }}
    {{ $char := (substr $str . 1) }}
    {{ $code := (int ($codeTable.Get $char)) }}
    {{ $sum := (int ($scratch.Get "Sum")) }}
    {{ $add := (int (add $sum $code)) }}
    {{ $scratch.Set "Sum" $add }}
{{ end }}
{{ $sum := (int ($scratch.Get "Sum")) }}
{{ $itemIndex := (int (mod $sum $itemCount)) }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 10:42am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/4 "2022-02-12T10:42:00Z")

</div>

Your method:

```auto
{{ $str := "abc" }} --> 6
{{ $str := "cba" }} --> 6

```

---

<div class="post-metadata">

**Author:** ![donggas90](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/donggas90/32/16230_2.png) [@donggas90](https://discourse.gohugo.io/u/donggas90)\
**Post date:** [February 12, 2022, 10:53am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/5 "2022-02-12T10:53:55Z")

</div>

You’re right.  
In my case, the hash has not to be good quality because just selecting random color for distinting UIs.

XOR is good for that case. but there is not, again.  
Even though really basic bit operation in computer systems.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 11:14am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/6 "2022-02-12T11:14:55Z")

</div>

You can implement the same thing with a lot less code.

```auto
{{ $s = split $s "" }}
{{ $s = apply $s "printf" "%#x" "." }}
{{ $s = apply $s "int" "." }}
{{ $s = apply $s "mod" "." 16 }}
{{ $index := 0 }}
{{ range $s }}
  {{ $index = add $index . }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![donggas90](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/donggas90/32/16230_2.png) [@donggas90](https://discourse.gohugo.io/u/donggas90)\
**Post date:** [February 12, 2022, 11:16am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/7 "2022-02-12T11:16:33Z")

</div>

Can you explain about the `%#x`?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 11:17am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/8 "2022-02-12T11:17:11Z")

</div>

[https://pkg.go.dev/fmt](https://pkg.go.dev/fmt)

---

<div class="post-metadata">

**Author:** ![donggas90](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/donggas90/32/16230_2.png) [@donggas90](https://discourse.gohugo.io/u/donggas90)\
**Post date:** [February 12, 2022, 11:28am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/9 "2022-02-12T11:28:09Z")

</div>

```auto
{{ $itemCount := 16 }}
{{ $scratch := newScratch }}
{{ $scratch.Set "Sum" 0 }}
{{ $split := split $str "" }}
{{ range $split }}
    {{ $code := (int (printf "%#x" .)) }}
    {{ $sum := (int ($scratch.Get "Sum")) }}
    {{ $add := (int (add $sum $code)) }}
    {{ $scratch.Set "Sum" $add }}
{{ end }}
{{ $sum := (int ($scratch.Get "Sum")) }}
{{ $itemIndex := (int (mod $sum $itemCount)) }}

```

It looks more stable.  
Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 14, 2022, 11:29am UTC](https://discourse.gohugo.io/t/get-char-codes-or-generate-an-integer-from-string/37119/10 "2022-02-14T11:29:00Z")

</div>

Closed on February 14, 2022, 11:29am UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
