0

I'm trying to create a 22x22 datamatrix. It has a max length of 43 alphanumeric characters in each position.

I have a url string with a length of 60. Is there a way to encode the string into another shorter string to reduce it's length so it will fit in the 43 character limit?

I can't use url shorteners because the app is designed to work without internet. Enter the string into a database and get a short id that references it's also not an option.

I tried base64 and hexadecimal encodings, but they return longer strings than the initial. Any ideas?

Update: (try to explain better the problem)

The problem is cause I have the size for the datamatrix limited to 22x22 (43 alphanumeric characters, 55 numeric) and if i add more doesn't fit in the print desing reserved size. I have a local app where the datamatrix is generated, and that need to works without internet (downloading server information on certain dates) and print the datamatrix. A server will respond when the url inside the datamatrix is readed by a qr reader app.

  • 3
    I think you'll need a bigger datamatrix. I might be able to fix this for you today, but next week you'll need a 75 character URL. And if you make that work soon it'll be 80. And then 100. You say this needs to work without internet... but these are URLs after all. They're not useful without some kind of network access. What about your own URL shortener that runs on the local network? – Joel Coehoorn Jun 13 '19 at 16:01
  • You might want to read [What is the maximum length of a URL in different browsers?](https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers) - basically, urls are not limited by standard, but should be up to 2000 chars in length. So even If you could use some zipping algorithm on a string to get it shorter, I seriously doubt you can use something to get from 2000 chars to 43. – Zohar Peled Jun 13 '19 at 16:06
  • Yes, the problem is that, cause I have the size for the datamatrix limited to 22x22 (43 characters) and if i add more doesn't fit in the print desing reserved size. I have a local app where the datamatrix is generated, and that need to works without internet (downloading server information on certain dates) and print the datamatrix. A server will respond when the url inside the datamatrix is readed by a qr reader app. – Dani Hernandez Jun 13 '19 at 16:10
  • 22x22 should be able to handle exactly 60 characters, unless that includes the border, in which case you still get 55 characters. – Joel Coehoorn Jun 13 '19 at 16:12
  • 1
    do you only need the ability to encode URLs? a reduced alphabet could reduce the number of bits required per character. – Matthew Whited Jun 13 '19 at 16:15
  • Also, can you assume case insensitive – Matthew Whited Jun 13 '19 at 16:25
  • Can you provide some sample URLs? – NetMage Jun 13 '19 at 22:33
  • Example url: https://exampleurl.com/reader?q=AXG567ER&st=1005&c=34500 – Dani Hernandez Jun 14 '19 at 07:16
  • my problem is that the q parameter needs to be length variable and maybe it has 8 digits like the example or maybe it has 16(2x8) or 24(3x8) etc.. – Dani Hernandez Jun 14 '19 at 07:18

1 Answers1

-1

Characters in a string are just bytes and one character could be represented by 8bits to 32bits (or theoretically longer). You should work with bytes instead of strings.

JackSojourn
  • 324
  • 2
  • 15
  • Thank you for your answer. I tried it, but the revert method doesn't return the same value that the initial input if there are numbers in the string. Also when i read the datamatrix with an qr code reader app, it doesn't recognize the encoded string. – Dani Hernandez Jun 13 '19 at 16:03
  • what string with numbers? – JackSojourn Jun 13 '19 at 16:07
  • This code doesn't work and will actually *lose* data. It will mangle the data in any case. `iso-8859-1` is the Latin1 codepage so it *can't* represent some characters. Those characters will be replaced by `?`. The bytes it returns correspond to that codepage only. UTF16 on the other hand requires *two* bytes. Using `Encoding.Unicode.GetString()` here can easily create garbage as some combinations will be invalid. Those will be replaced by `�` – Panagiotis Kanavos Jun 13 '19 at 16:07
  • @PanagiotisKanavos you are correct. the utf-16 will lose the character if the two byte combination is not a part of the codepage – JackSojourn Jun 13 '19 at 16:24
  • @JackSojourn in the example I posted `m`, something that is part of US-ASCII and clearly an alphabet character war lost. – Panagiotis Kanavos Jun 14 '19 at 07:22