Repository navigation
Expand file tree
/
Copy pathasc2sjis.cpp
More file actions
155 lines (136 loc) · 2.93 KB
/
Copy pathasc2sjis.cpp
File metadata and controls
155 lines (136 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* ASCII code to Shift-JIS code transfer table
*/
static unsigned short ascii_table[3][2] = {
{0x824f, 0x30}, /* 0-9 */
{0x8260, 0x41}, /* A-Z */
{0x8281, 0x61}, /* a-z */
};
/*
* ASCII code to Shift-JIS code transfer table (kigou)
*/
static unsigned short ascii_k_table[] = {
0x8140, /* */
0x8149, /* ! */
0x8168, /* " */
0x8194, /* # */
0x8190, /* $ */
0x8193, /* % */
0x8195, /* & */
0x8166, /* ' */
0x8169, /* ( */
0x816a, /* ) */
0x8196, /* * */
0x817b, /* + */
0x8143, /* , */
0x817c, /* - */
0x8144, /* . */
0x815e, /* / */
0x8146, /* : */
0x8147, /* ; */
0x8171, /* < */
0x8181, /* = */
0x8172, /* > */
0x8148, /* ? */
0x8197, /* @ */
0x816d, /* [ */
0x818f, /* \ */
0x816e, /* ] */
0x814f, /* ^ */
0x8151, /* _ */
0x8165, /* ` */
0x816f, /* { */
0x8162, /* | */
0x8170, /* } */
0x8150, /* ~ */
};
/*
* ASCII code to Shift-JIS code transfer function
*/
unsigned short
ascii2sjis(unsigned char ascii_code)
{
unsigned short sjis_code = 0;
unsigned char stmp;
unsigned char stmp2 = 0;
if ((ascii_code >= 0x20) && (ascii_code <= 0x2f))
stmp2 = 1;
else
if ((ascii_code >= 0x30) && (ascii_code <= 0x39))
stmp = 0;
else
if ((ascii_code >= 0x3a) && (ascii_code <= 0x40))
stmp2 = 11;
else
if ((ascii_code >= 0x41) && (ascii_code <= 0x5a))
stmp = 1;
else
if ((ascii_code >= 0x5b) && (ascii_code <= 0x60))
stmp2 = 37;
else
if ((ascii_code >= 0x61) && (ascii_code <= 0x7a))
stmp = 2;
else
if ((ascii_code >= 0x7b) && (ascii_code <= 0x7e))
stmp2 = 63;
else {
printf("bad ASCII code 0x%x\n", ascii_code);
return(0);
}
if (stmp2)
sjis_code = ascii_k_table[ascii_code - 0x20 - (stmp2 - 1)];
else
sjis_code = ascii_table[stmp][0] + ascii_code - ascii_table[stmp][1];
return(sjis_code);
}
// Convert the Ascii Text String to SJIS.
void AsciiStringToSJIS(unsigned char *string, unsigned char *dest)
{
int i;
int sjis_code;
int ascii_code;
unsigned char stmp;
unsigned char stmp2;
unsigned char *dest2;
dest2 = dest;
for( i=0; i<32; i++ )
{
*dest2++ = 0x81;
*dest2++ = 0x40;
}
while(*string) {
stmp2 = 0;
ascii_code = *string++;
if ((ascii_code >= 0x20) && (ascii_code <= 0x2f))
stmp2 = 1;
else
if ((ascii_code >= 0x30) && (ascii_code <= 0x39))
stmp = 0;
else
if ((ascii_code >= 0x3a) && (ascii_code <= 0x40))
stmp2 = 11;
else
if ((ascii_code >= 0x41) && (ascii_code <= 0x5a))
stmp = 1;
else
if ((ascii_code >= 0x5b) && (ascii_code <= 0x60))
stmp2 = 37;
else
if ((ascii_code >= 0x61) && (ascii_code <= 0x7a))
stmp = 2;
else
if ((ascii_code >= 0x7b) && (ascii_code <= 0x7e))
stmp2 = 63;
else {
printf("bad ASCII code 0x%x\n", ascii_code);
while(1);
}
if(stmp2)
sjis_code = ascii_k_table[ascii_code - 0x20 - (stmp2 - 1)];
else
sjis_code = ascii_table[stmp][0] + ascii_code - ascii_table[stmp][1];
/* Write sjis */
*dest++ = (sjis_code&0xff00)>>8;
*dest++ = (sjis_code&0xff);
}
}