QRCodeSegment Class

Represents a run of data encoded in one mode, with its own mode indicator and character count indicator, to choose the segments of a QR code yourself.

public sealed class QRCodeSegment

Remarks

QRCode.Create splits a text into segments by itself. Pass segments to QRCode.Create to choose them: to encode a text that the automatic segmentation keeps in one byte segment, to control the bytes of a byte segment, or to declare their encoding with an ECI designator.

Each segment costs a mode indicator and a character count indicator: 4 + 8 to 4 + 16 bits in a standard QR code, fewer in Micro QR and rMQR codes. A short segment can take more bits than the characters it saves.

Properties

NameDescription
BytesGets the bytes of a byte segment, or an empty value for the other modes.
EciAssignmentNumberGets the assignment number of an ECI designator, or null for the other modes.
ModeGets the mode of the segment.
TextGets the characters of a numeric, alphanumeric or Kanji segment, or null for a byte segment and an ECI designator.

Gets the bytes of a byte segment, or an empty value for the other modes.

public ReadOnlyMemory<byte> Bytes { get; }

Property value

ReadOnlyMemory<byte>

Remarks

A segment created from a text by QRCodeSegment.CreateByte holds the UTF-8 bytes of the text.

EciAssignmentNumber

QRCodeSegment.cs

Gets the assignment number of an ECI designator, or null for the other modes.

public int? EciAssignmentNumber { get; }

Property value

int?

Gets the mode of the segment.

public QRCodeSegmentMode Mode { get; }

Property value

QRCodeSegmentMode

Gets the characters of a numeric, alphanumeric or Kanji segment, or null for a byte segment and an ECI designator.

public string? Text { get; }

Property value

string?

Methods

NameDescription
CreateAlphanumeric(string)Creates a segment of digits, uppercase letters, and the characters $%*+-./:, in alphanumeric mode.
CreateByte(ReadOnlySpan<byte>)Creates a segment of bytes, in byte mode.
CreateByte(string)Creates a segment of a text encoded as UTF-8, in byte mode.
CreateEci(int)Creates an ECI (Extended Channel Interpretation) designator, which declares the character encoding of the byte segments that follow it, such as QRCodeOptions.Utf8EciAssignmentNumber for UTF-8.
CreateKanji(string)Creates a segment of kanji and kana, in Kanji mode (their Shift JIS codes).
CreateNumeric(string)Creates a segment of digits, in numeric mode.

CreateAlphanumeric(string)

QRCodeSegment.cs

Creates a segment of digits, uppercase letters, and the characters $%*+-./:, in alphanumeric mode.

public static QRCodeSegment CreateAlphanumeric(string text)

Parameters

text string
The characters.

Returns

QRCodeSegment A new QRCodeSegment instance.

Exceptions

ArgumentException
text is empty or has a character that alphanumeric mode cannot encode.

CreateByte overloads

CreateByte(ReadOnlySpan<byte>)

QRCodeSegment.cs

Creates a segment of bytes, in byte mode.

public static QRCodeSegment CreateByte(ReadOnlySpan<byte> data)
Parameters
data ReadOnlySpan<byte>
The bytes, copied.
Returns

QRCodeSegment A new QRCodeSegment instance.

Exceptions
ArgumentException
data is empty.

CreateByte(string)

QRCodeSegment.cs

Creates a segment of a text encoded as UTF-8, in byte mode.

public static QRCodeSegment CreateByte(string text)
Parameters
text string
The text.
Returns

QRCodeSegment A new QRCodeSegment instance.

Exceptions
ArgumentException
text is empty.
Remarks

Readers guess the encoding of the bytes unless an ECI designator declares it: precede the segment with CreateEci(QRCodeOptions.Utf8EciAssignmentNumber) when the text is not ASCII.

CreateEci(int)

QRCodeSegment.cs

Creates an ECI (Extended Channel Interpretation) designator, which declares the character encoding of the byte segments that follow it, such as QRCodeOptions.Utf8EciAssignmentNumber for UTF-8.

public static QRCodeSegment CreateEci(int assignmentNumber)

Parameters

assignmentNumber int
The ECI assignment number, from 0 to 999999.

Returns

QRCodeSegment A new QRCodeSegment instance.

Exceptions

ArgumentOutOfRangeException
assignmentNumber is negative or greater than 999999.

Remarks

Only standard QR codes have ECI designators: Micro QR and rMQR codes reject them.

CreateKanji(string)

QRCodeSegment.cs

Creates a segment of kanji and kana, in Kanji mode (their Shift JIS codes).

public static QRCodeSegment CreateKanji(string text)

Parameters

text string
The characters.

Returns

QRCodeSegment A new QRCodeSegment instance.

Exceptions

ArgumentException
text is empty or has a character that Kanji mode cannot encode.

Remarks

Kanji mode encodes the characters of the JIS X 0208 character set that every reader decodes the same way. It does not encode the half-width katakana, nor the characters that the variants of Shift JIS decode differently, such as ~ and -: encode them in byte mode.

CreateNumeric(string)

QRCodeSegment.cs

Creates a segment of digits, in numeric mode.

public static QRCodeSegment CreateNumeric(string digits)

Parameters

digits string
The digits, from 0 to 9.

Returns

QRCodeSegment A new QRCodeSegment instance.

Exceptions

ArgumentException
digits is empty or has a character that is not a digit.