jpdec

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 8 Imported by: 0

README

English | 日本語

Overview

Detecting and Decode for Japanese encoding for Go lang.

Supported Encoding

  • Shift_JIS
  • EUC-JP
  • JIS(ISO-2022-JP)
  • UTF-8
  • UTF-8(with BOM)
  • UTF-16
  • UTF-16(with BOM)
  • UTF-16(Big Endian)
  • UTF-16(Big Endian with BOM)

To detect encoding exactly, use follow encoding:

  • JIS
  • UTF-8(with BOM)
  • UTF-16(with BOM)
  • UTF-16(Big Endian with BOM)

Other encodings are not exact, and the one that is more likely to be chosen.

Samples

Decode:
byteArray := []byte{0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF, 0x82, 0xCD}
str, _ := jpdec.Decode(byteArray)
fmt.Println(str)

output:

こんにちは
Detect encoding:
byteArray := []byte{0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF, 0x82, 0xCD}
enc, _ := jpdec.DetectEncoding(byteArray)
fmt.Println(enc)

output:

Shift_JIS

Documentation

Overview

Detecting and Decode for Japanese encoding

Supported Encoding

  • Shift_JIS
  • EUC-JP
  • JIS(ISO-2022-JP)
  • UTF-8
  • UTF-8(with BOM)
  • UTF-16
  • UTF-16(with BOM)
  • UTF-16(Big Endian)
  • UTF-16(Big Endian with BOM)

To detect encoding exactly, use follow encoding:

  • JIS
  • UTF-8(with BOM)
  • UTF-16(with BOM)
  • UTF-16(Big Endian with BOM)
  • UTF-16(Little Endian containing ASCII char)
  • UTF-16(Big Endian containing ASCII char)

Other encodings are not accurate, just pick the one that is more likely.

Index

Examples

Constants

View Source
const (
	// 不明
	EncodingUnknown = ""

	// Shift_JIS
	EncodingShiftJIS = "Shift_JIS"

	// JIS
	EncodingJIS = "ISO-2022-JP"

	// EUC
	EncodingEUCJP = "EUC-JP"

	// UTF-8
	EncodingUTF8 = "UTF-8"

	// UTF-8(BOM付)
	EncodingUTF8BOM = "UTF-8BOM"

	// UTF-16(BOM無し:リトルエンディアン)
	EncodingUTF16LE = "UTF-16LE"

	// UTF-16(BOM無し:リトルエンディアン)
	EncodingUTF16BE = "UTF-16BE"

	// UTF-16(BOM有り:リトルエンディアン)
	EncodingUTF16LEBOM = "UTF-16LEBOM"

	// UTF-16(BOM有り:ビッグエンディアン)
	EncodingUTF16BEBOM = "UTF-16BEBOM"
)

エンコーディング

Variables

View Source
var (
	// エンコーディングの検出に失敗
	ErrUnknown = errors.New("unknown encoding")

	// バイナリファイルっぽい
	ErrPossiblyBinary = errors.New("possibly binary")
)

エラー定義

Functions

func Decode

func Decode(byteArray []byte) (string, error)

エンコーディングの自動判別とデコード

SJIS, EUC-JP などの文字列のバイト配列 byteArray を文字列に変換します。 失敗した場合、string(byteArray) と ErrUnknown/ErrPossiblyBinary エラーを返します。

Example

Decode() のサンプル

package main

import (
	"fmt"

	"github.com/juneysec/jpdec"
)

func main() {
	byteArray := []byte{0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF, 0x82, 0xCD}
	str, _ := jpdec.Decode(byteArray)
	fmt.Println(str)

}
Output:

こんにちは

func DetectEncoding

func DetectEncoding(bytes []byte) (string, error)

エンコーディングの検出

エンコーディングを示す文字列である EncodingXxx 定数のいずれかを返します。 検出に失敗した場合は EncodingUnknown と ErrUnknown/ErrPossiblyBinary エラーを返します。

Example

DetectEncoding() のサンプル

package main

import (
	"fmt"

	"github.com/juneysec/jpdec"
)

func main() {
	byteArray := []byte{0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF, 0x82, 0xCD}
	enc, _ := jpdec.DetectEncoding(byteArray)
	fmt.Println(enc)

}
Output:

Shift_JIS

func ReadFile

func ReadFile(filePath string) (string, error)

指定されたファイルを読み込んで、文字列として返します。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL