unit jisconv;
interface
uses
Sysutils;
function SJis2Jis(Str: String; Shift_Code: Boolean): String;
function Jis2SJis(Str: String; Shift_Code: Boolean): String;
implementation
function SJis2Jis(Str: String; Shift_Code: Boolean): String;
var
i, Count: Integer;
Code, Hi_Byte, Lo_Byte: Byte;
Src, Dst: PChar;
Sft_Flg: Boolean;
begin
Src := StrAlloc(1024);
Dst := StrAlloc(1024);
for i := 0 to StrBufSize(Src) - 1 do
begin
Src[i] := #$00;
Dst[i] := #$00;
end;
i := 0;
Count := 0;
Sft_Flg := False;
StrPCopy(Src, Str);
while (i < StrLen(Src)) do
begin
Hi_Byte := Ord(Src[i]);
Code := $00;
if (Hi_Byte >= $81) and (Hi_Byte <= $9F) then
Code := $71;
if (Hi_Byte >= $E0) and (Hi_Byte <= $FC) then
Code := $B1;
{ 全角文字だったら }
if (Code <> $00) then
begin
// SHIFT IN
if (not Sft_Flg) and Shift_Code then
begin
Dst[Count] := #$0f;
Inc(Count);
Sft_Flg := True;
end;
Inc(i);
Lo_Byte := Ord(Src[i]);
if (Lo_Byte <= $9E) then
begin
Dst[Count] := Chr((Hi_Byte - Code) * 2 + 1);
if (Lo_Byte >= $80) then
Dst[Count + 1] := Chr(Lo_Byte - $1F - 1)
else
Dst[Count + 1] := Chr(Lo_Byte - $1F);
end
else
begin
Dst[Count] := Chr((Hi_Byte - (Code - 1)) * 2);
Dst[Count + 1] := Chr(Lo_Byte - $7E);
end;
Count := Count + 2;
end
{ 半角文字だったら }
else
begin
// SHIFT OUT
if Sft_Flg and Shift_Code then
begin
Dst[Count] := #$0e;
Inc(Count);
Sft_Flg := False;
end;
Dst[Count] := Src[i];
Inc(Count);
end;
Inc(i);
end;
// 全角文字で終わっていたらSHIFT OUT
if Sft_Flg and Shift_Code then
Dst[Count] := #$0e;
result := StrPas(Dst);
StrDispose(Dst);
StrDispose(Src);
end;
function Jis2SJis(Str: String; Shift_Code: Boolean): String;
var
i, Count: Integer;
Hi_Byte, Lo_Byte: Byte;
Src, Dst: PChar;
Sft_Flg: Boolean;
begin
Src := StrAlloc(1024);
Dst := StrAlloc(1024);
for i := 0 to StrBufSize(Src) - 1 do
begin
Src[i] := #$00;
Dst[i] := #$00;
end;
i := 0;
Count := 0;
Sft_Flg := not Shift_Code;
StrPCopy(Src, Str);
while (i < StrLen(Src)) do
begin
Hi_Byte := Ord(Src[i]);
if Shift_Code then
begin
// SHIFT IN
if Hi_Byte = $0F then
begin
Sft_Flg := True;
Inc(i);
Hi_Byte := Ord(Src[i]);
end;
// SHIFT OUT
if Hi_Byte = $0E then
begin
Sft_Flg := False;
Inc(i);
Hi_Byte := Ord(Src[i]);
end;
end;
{ 全角文字だったら }
if Sft_Flg then
begin
Inc(i);
Lo_Byte := Ord(Src[i]);
if (Hi_Byte mod 2) = 1 then
Lo_Byte := Lo_Byte + $1F
else
Lo_Byte := Lo_Byte + $7D;
if (Lo_Byte >= $7F) then
Lo_Byte := Lo_Byte + 1;
Hi_Byte := ((Hi_Byte - $21) shr 1) + $81;
if (Hi_Byte > $9F) then
Hi_Byte := Hi_Byte + $40;
Dst[Count] := Chr(Hi_Byte);
Dst[Count + 1] := Chr(Lo_Byte);
Count := Count + 2;
end
{ 半角文字だったら }
else
begin
Dst[Count] := Src[i];
Inc(Count);
end;
Inc(i);
end;
result := StrPas(Dst);
StrDispose(Dst);
StrDispose(Src);
end;
end.
|