Yoone.9461:

Hello!

I saw some messages about how to get the chat codes of the items we get from the API, so I thought a collection of snippets in multiple languages would be nice.

I’ll start with JavaScript and PHP, feel free to adapt it in other languages if you can.

As pointed out by zeeZ.5713, the following functions are designed for the item_id to be lower or equal than 65535 (or 65536, it depends on the first ID).

So if you know how, please improve the existing functions!

zeeZ.5713:

Python?

from base64 import b64encode
def genItemChatCode(item_id):
    return '[&' + b64encode('\x02\x01' + chr(item_id%256) + chr(item_id/256) + '\x00\x00') + ']'

There might be a better approach to this, though :P Also what happens once IDs exceed 65535?

Yoone.9461:

Thank you, I added it.

There might be a better approach to this, though :P Also what happens once IDs exceed 65535?

That’s why I said: “You also can improve the existing functions!”

I don’t really know what to do in that case.

Edit: first post updated with links to external pastes (for a more readable message).

Dr Ishmael.9685:

Here’s some VBA I wrote for Excel. The Base64 module is from http://www.source-code.biz/ , but it expected all input to be ASCII, so I had to modify it to accept integer/byte-array inputs.

Usage:

=EncodeGWID(12345)

=DecodeGWID(“[&AGXXXX]”)

Leylin.3901:

Hello there,

I don’t know if it is okay to revive this long dead thread. But the thing that you were afraid about happened: There are Item ID’s higher than 65535.

I used the python code snippet from zeeZ.5713 and modified it to fit into my environment:

def genItemChatCode(item_id):
a = b’\x02\x01’ + bytes([item_id % 256]) + bytes([int(item_id / 256)]) + b’\x00\x00’
return ‘[&’ + b64encode(a).decode(encoding=“UTF-8”) + ‘]’

It was working really well – until I reached “Item: 65536 Ascalonian Elementalist Loot Box”.

After a bit of searching I stumbled across a little python gem from “Guest”:
http://pastebin.com/Nkw5iZGv

I modified it and used it with Python 3.3.2 and Django 1.6 as a template filter.

@register.filter
def genItemChatCode(item_id):
– - hexa = b’\x02\x01’
– - for i in range(4):
– - – - hexa += bytes([item_id % 256])
– - – - item_id //= 256
– - return ‘[&’ + b64encode(hexa).decode(encoding=“UTF-8”) + ‘]’

I hope this helps

AysonCurrax.3254:

I too, noticed that exceeding 65535 ids borked up my previous code where only the 3rd and 4th byte were depending on the item id. The first byte indicates whether it is an item, skin etc. the second byte, atleast for items, indicates the amount of items (which is why its normally set to 1). the last 4 bytes consist of the item id in little endian order.
This is how i did it in Java :

private String encodeBase64(int itemId) {
byte[] temp = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(itemId).array();
StringBuilder result = new StringBuilder();
result.append(“[&”);
byte[ ] bytes = new byte[ 6 ];
bytes[ 0 ] = 0×02;
bytes[ 1 ] = 0×01;
bytes[ 2 ] = temp[ 0 ];
bytes[ 3 ] = temp[ 1 ];
bytes[ 4 ] = temp[ 2 ];
bytes[ 5 ] = temp[ 3 ];
result.append(Base64.encodeBase64String(bytes));
result.append(“]”);
return result.toString();
}

i noticed because i wanted to test my chat code for light of dwayna (infused) which resulted in some blue item. the cause was the fact that all chat codes with an item id below 65535 would end in AA, but since this one is above that limit, it should have ended in EA. I hope this helped.

smiley.1438:

So here’s a snippet for PHP which works with IDs > 16 bit:

https://gist.github.com/codemasher/47dea40f70f990480c5b