MCPcopy
hub / github.com/skip2/go-qrcode / encodedLength

Method encodedLength

encoder.go:416–447  ·  view source on GitHub ↗

encodedLength returns the number of bits required to encode n symbols in dataMode. The number of bits required is affected by: - QR code type - Mode Indicator length. - Data mode - number of bits used to represent data length. - Data mode - how the data is encoded. - Number of symbols encoded.

(dataMode dataMode, n int)

Source from the content-addressed store, hash-verified

414// An error is returned if the mode is not supported, or the length requested is
415// too long to be represented.
416func (d *dataEncoder) encodedLength(dataMode dataMode, n int) (int, error) {
417 modeIndicator := d.modeIndicator(dataMode)
418 charCountBits := d.charCountBits(dataMode)
419
420 if modeIndicator == nil {
421 return 0, errors.New("mode not supported")
422 }
423
424 maxLength := (1 << uint8(charCountBits)) - 1
425
426 if n > maxLength {
427 return 0, errors.New("length too long to be represented")
428 }
429
430 length := modeIndicator.Len() + charCountBits
431
432 switch dataMode {
433 case dataModeNumeric:
434 length += 10 * (n / 3)
435
436 if n%3 != 0 {
437 length += 1 + 3*(n%3)
438 }
439 case dataModeAlphanumeric:
440 length += 11 * (n / 2)
441 length += 6 * (n % 2)
442 case dataModeByte:
443 length += 8 * n
444 }
445
446 return length, nil
447}
448
449// encodeAlphanumericChar returns the QR Code encoded value of v.
450//

Callers 3

encodeMethod · 0.95
optimiseDataModesMethod · 0.95

Calls 3

modeIndicatorMethod · 0.95
charCountBitsMethod · 0.95
LenMethod · 0.80

Tested by 1