Преглед изворни кода

QByteArray/Integer conversions

Neal Wilson пре 10 година
родитељ
комит
01ee351db7
1 измењених фајлова са 44 додато и 0 уклоњено
  1. 44
    0
      src/typica.w

+ 44
- 0
src/typica.w Прегледај датотеку

@@ -2096,6 +2096,9 @@ QScriptValue QByteArray_right(QScriptContext *context, QScriptEngine *engine);
2096 2096
 QScriptValue QByteArray_mid(QScriptContext *context, QScriptEngine *engine);
2097 2097
 QScriptValue QByteArray_chop(QScriptContext *context, QScriptEngine *engine);
2098 2098
 QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine);
2099
+QScriptValue QByteArray_toInt8(QScriptContext *context, QScriptEngine *engine);
2100
+QScriptValue QByteArray_toInt16(QScriptContext *context, QScriptEngine *engine);
2101
+QScriptValue QByteArray_toInt32(QScriptContext *context, QScriptEngine *engine);
2099 2102
 
2100 2103
 @ First, we provide some functionns for moving array data across the
2101 2104
 language barrier.
@@ -2148,6 +2151,9 @@ void setQByteArrayProperties(QScriptValue value, QScriptEngine *engine)
2148 2151
 	value.setProperty("mid", engine->newFunction(QByteArray_mid));
2149 2152
 	value.setProperty("chop", engine->newFunction(QByteArray_chop));
2150 2153
 	value.setProperty("remove", engine->newFunction(QByteArray_remove));
2154
+	value.setProperty("toInt8", engine->newFunction(QByteArray_toInt8));
2155
+	value.setProperty("toInt16", engine->newFunction(QByteArray_toInt16));
2156
+	value.setProperty("toInt32", engine->newFunction(QByteArray_toInt32));
2151 2157
 }
2152 2158
 
2153 2159
 @ Perhaps the easiest way to deal with fixed byte strings for serial
@@ -2269,6 +2275,44 @@ QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine)
2269 2275
 	return value;
2270 2276
 }
2271 2277
 
2278
+@ When receiving data in a byte array, bytes are sometimes intended to
2279
+represent 8, 16, or 32 bit integers. In such cases we often want to perform
2280
+some computation on these values so having the ability to split off that
2281
+portion of the array (for example, with |mid()|) and convert to a Number is
2282
+useful.
2283
+
2284
+@<Functions for scripting@>=
2285
+QScriptValue QByteArray_toInt8(QScriptContext *context, QScriptEngine *)
2286
+{
2287
+	QByteArray self = getself<QByteArray>(context);
2288
+	int value = 0;
2289
+	char *bytes = (char *)&value;
2290
+	bytes[0] = self[0];
2291
+	return QScriptValue(value);
2292
+}
2293
+
2294
+QScriptValue QByteArray_toInt16(QScriptContext *context, QScriptEngine *)
2295
+{
2296
+	QByteArray self = getself<QByteArray>(context);
2297
+	int value = 0;
2298
+	char *bytes = (char *)&value;
2299
+	bytes[0] = self[0];
2300
+	bytes[1] = self[1];
2301
+	return QScriptValue(value);
2302
+}
2303
+
2304
+QScriptValue QByteArray_toInt32(QScriptContext *context, QScriptEngine *)
2305
+{
2306
+	QByteArray self = getself<QByteArray>(context);
2307
+	int value = 0;
2308
+	char *bytes = (char *)&value;
2309
+	bytes[0] = self[0];
2310
+	bytes[1] = self[1];
2311
+	bytes[2] = self[2];
2312
+	bytes[3] = self[3];
2313
+	return QScriptValue(value);
2314
+}
2315
+
2272 2316
 @ Some protocols require manipulating larger than 8 bit numbers as a sequence
2273 2317
 of bytes. To facilitate this, methods are provided to construct a |QByteArray|
2274 2318
 from different sized numbers. 8 bit numbers are provided for uniformity.

Loading…
Откажи
Сачувај