Convert Hex to Byte array in Swift - ios

I've a hardware which sends me a hex data and i need to convert it to byte array in swift. Here's example data;
01 01 02 0A 12 14 14 11 10 0D 0B 0A 09 09 08 08 08 07 06 06 06 06 06 06 05 05 06 05 05 04 04 04 04 03 03 03 04
03 03 02 03 02 03 02 02 03 02 02 02 01 02 01 01 01 01 01
Can you help me please?

If you got a bunch of hexes (UInt8), then you can create NSData like this:
NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2)

use this method it gives Byte Array
func getByte(data:NSData) -> Array<UInt8> {
let count = data.length / sizeof(Int8)
var array = [UInt8](count: count, repeatedValue: 0)
data.getBytes(&array, length:count * sizeof(UInt32))
return array
}

Related

COBOL 6.3 JSON-GENERATE SUPPRESS is suppressing all table occurrences

I am working with the COBOL Enterprise 6.3 JSON-GENERATE, using the SUPPRESS phrase. I have the following COBOL data area:
05 contactInfo.
07 postalAddresses OCCURS 0 TO 10
DEPENDING ON WIMBCST-POSTAL-COUNT.
09 POSTAL-ID PIC X(40).
09 addressCategory PIC X(30).
09 startDate PIC X(10).
09 legalAddressStartDate PIC X(10).
09 endDate PIC X(10).
09 perpetualEndDate PIC X(10).
09 perpetualStartDate PIC X(10).
09 addressFormatName PIC X(10).
09 POSTAL-SEQ PIC 99.
09 postalAddressType PIC X(20).
09 addressLines OCCURS 10 TIMES.
11 addressLineType PIC X(20).
11 addressLineText PIC X(60).
09 locationLines OCCURS 10 TIMES.
11 locationType PIC X(20).
11 locationText PIC X(60).
The data is initially LOW-VALUES. When the JSON is generated, if any occurrence of addressLines or locationLines is eligible for suppression, the entire table is suppressed as follows:
{
"postalAddresses": [
{
"addressCategory": "Master",
"addressFormatName": "US",
"sequenceNumber": 1,
"postalAddressType": "Unknown"
}
]
}
Even though occurrence 1 of addressLines has data, and locationLines has 3 occurrences of data.
I am really expecting this:
{
"postalAddresses": [
{
"addressCategory": "Master",
"addressFormatName": "US",
"sequenceNumber": 1,
"postalAddressType": "Unknown",
"addressLines": [
{
"addressLineType": "A",
"addressLineText": "123 MAIN"
}
],
"locationLines": [
{
"locationType": "City",
"locationText": "TEST CITY"
},
{
"locationType": "State",
"locationText": "CA"
},
{
"locationType": "Postal Code",
"locationText": "99999"
}
]
}
]
}
Any tips/advice on how I can use the SUPPRESS phrase and get the addressLines and locationLines items generated?
this is a known problem that should be fixed by APAR PH40288

Convert raw image data to base64 encoded string

I am downloading a thumbnail from dropbox using the following API:
https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
This is part of my function from cloud code:
}).then(function(oauthToken){
/* Retrieve thumbnail image from dropbox */
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://content.dropboxapi.com/2/files/get_thumbnail',
headers: {
'Authorization': 'Bearer ' + oauthToken.get("accessToken"),
'Dropbox-API-Arg': JSON.stringify({"path": dropboxFileId})
}
});
}).then(function(imageResponse){
if (log) {console.log(imageResponse);}
var imageBuffer = imageResponse.buffer;
var image = imageBuffer.toString("base64");
response.success(image);
}, function(error){
response.error(error.text);
});
Following is my imageResponse:
{ status: 200,
headers:
{ server: 'nginx',
date: 'Wed, 16 Mar 2016 04:30:54 GMT',
'content-type': 'application/octet-stream',
'content-length': '906',
connection: 'close',
'accept-ranges': 'bytes',
pragma: 'no-cache',
'cache-control': 'no-cache',
'original-content-length': '906',
'dropbox-api-result': '{"name": "HTS-logo-low-res.png", "path_lower": "/hts-logo-low-res.png", "path_display": "/HTS-logo-low-res.png", "id": "id:hrkr_mV5FYAAAAAAAAAAHw", "client_modified": "2015-04-05T04:16:15Z", "server_modified": "2016-02-13T06:08:02Z", "rev": "2a043d0c726", "size": 10506}',
'x-server-response-time': '190',
'x-dropbox-request-id': '04bb8d6b45511f2b2d7a278e230ea572',
'x-robots-tag': 'noindex, nofollow, noimageindex' },
buffer: <Buffer ef bf bd ef bf bd ef bf bd ef bf bd 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ef bf bd ef bf bd 00 43 00 06 04 05 06 05 04 06 06 05 06 07 07 06 ... >,
cookies: undefined,
text: '����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000C\u0000\u0006\u0004\u0005\u0006\u0005\u0004\u0006\u0006\u0005\u0006\u0007\u0007\u0006\b\n\u0010\n\n\t\t\n\u0014\u000e\u000f\f\u0010\u0017\u0014\u0018\u0018\u0017\u0014\u0016\u0016\u001a\u001d%\u001f\u001a\u001b#\u001c\u0016\u0016 , #&\')*)\u0019\u001f-0-(0%()(��\u0000C\u0001\u0007\u0007\u0007\n\b\n\u0013\n\n\u0013(\u001a\u0016\u001a((((((((((((((((((((((((((((((((((((((((((((((((((��\u0000\u0011\b\u0000\u0010\u0000#\u0003\u0001"\u0000\u0002\u0011\u0001\u0003\u0011\u0001��\u0000\u001f\u0000\u0000\u0001\u0005\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b��\u0000�\u0010\u0000\u0002\u0001\u0003\u0003\u0002\u0004\u0003\u0005\u0005\u0004\u0004\u0000\u0000\u0001}\u0001\u0002\u0003\u0000\u0004\u0011\u0005\u0012!1A\u0006\u0013Qa\u0007"q\u00142���\b#B��\u0015R��$3br�\t\n\u0016\u0017\u0018\u0019\u001a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������\u0000\u001f\u0001\u0000\u0003\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b��\u0000�\u0011\u0000\u0002\u0001\u0002\u0004\u0004\u0003\u0004\u0007\u0005\u0004\u0004\u0000\u0001\u0002w\u0000\u0001\u0002\u0003\u0011\u0004\u0005!1\u0006\u0012AQ\u0007aq\u0013"2�\b\u0014B����\t#3R�\u0015br�\n\u0016$4�%�\u0017\u0018\u0019\u001a&\'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������\u0000\f\u0003\u0001\u0000\u0002\u0011\u0003\u0011\u0000?\u0000�;ŷ��鱘\'[s,�\u0013L�\n���{\f�g޹\u000f��I�\\.�w\u0013��H~̡���\u0002�\u001b���\u000f\u0001���#�%�&�O��\u0004z����6�Z:X�"G;�GB}M%N�3��ͧ]4��̪��9`�������0�֕�+\u000b�t \u0006P�\u0001%�z��\u0003�R=ި�G$.� �U��h]���d�\u001d9����u���*O����q��|�\u0007\u0005\\�N\u000e\u0017\u0003�\u0000C�q��;�K���1\u001e��HR��z��~�N+���uy��d�بS咕�0e��91�\u001fC�\u0014���}z����.u\u0019Y�I\u0012�T.�\u0019~�\u0007ߦ\b��+���ߝ��W���_q��'
}
My code in XCode:
if let imageInBase64 = response as? String {
print(imageInBase64)
let decodedImage = NSData(base64EncodedString: imageInBase64, options: NSDataBase64DecodingOptions())
imageView.image = UIImage(data: decodedImage!)
}
Following is the output for imageInBase64:
77+977+977+977+9ABBKRklGAAEBAAABAAEAAO+/ve+/vQBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSjvv73vv70AQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo77+977+9ABEIABAAQAMBIgACEQEDEQHvv73vv70AHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL77+977+9AO+/vRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDLvv73vv73vv70II0Lvv73vv70VUu+/ve+/vSQzYnLvv70JChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl677+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+977+9AB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC++/ve+/vQDvv70RAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjLvv70IFELvv73vv73vv73vv70JIzNS77+9FWJy77+9ChYkNO+/vSXvv70XGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eu+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/ve+/vQAMAwEAAhEDEQA/AO+/vTvFt++/ve+/vemxmCdbcyzvv70TTO+/vQrvv73vv73vv717DO+/vWfeuX8P77+977+9Se+/vVwu77+9dxPvv73vv71Ifsyh77+977+977+9Au+/vRvvv73vv73vv70PAe+/ve+/ve+/vUDvv70l77+9Ju+/vU/vv73vv70Eeu+/ve+/ve+/ve+/vTbvv71aOljvv70iRzvvv71HQn1NJU7vv70z77+977+9zaddNO+/ve+/vcyq77+977+9OWDvv73vv73vv73vv73vv73vv73vv70w77+91pXvv70rC++/vXQgBlDvv70BJe+/vXrvv73vv70D77+9Uj3eqO+/vUckLu+/vSDvv71V77+977+9aF3vv73vv73vv71k77+9HTnvv73vv73vv73vv71177+977+977+9Kk/vv73vv73vv73vv71x77+977+9fO+/vQcFXO+/vU4OFwPvv70AQ++/vXHvv73vv70777+9S++/ve+/ve+/vTEe77+977+9SFLvv73vv71677+977+9fu+/vU4r77+977+977+9dXnvv73vv71k77+92KhT5ZKV77+9MGXvv73vv705Me+/vR9D77+9FO+/ve+/ve+/vX1677+977+977+977+9LnUZWe+/vUkS77+9VC7vv70Zfu+/vQffpgjvv73vv70r77+977+977+9353vv73vv71X77+977+977+9X3Hvv73vv70=
When I run this, I do not get an image displayed. I suspect that I am not properly handling the response from Dropbox before converting it to base64 string. Dropbox state that their responses are raw image data.
Any help will be much appreciated. Thanks.
Figured it out & thanks for those who answered. The issue was with Parse.com module Parse.Cloud.httpRequest which works slightly different when you are self-hosting parse-server vs Parse.com. I just needed to include encoding: null as an option in the module and everything works fine. Code below:
}).then(function(oauthToken){
/* Retrieve thumbnail image from dropbox */
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://content.dropboxapi.com/2/files/get_thumbnail',
headers: {
'Authorization': 'Bearer ' + oauthToken.get("accessToken"),
'Dropbox-API-Arg': JSON.stringify({"path": dropboxFileId})
},
encoding: null
});
}).then(function(imageResponse){
if (log) {console.log(imageResponse);}
var imageBuffer = imageResponse.buffer;
var image = imageBuffer.toString("base64");
response.success(image);
}, function(error){
response.error(error.text);
});

Constant TAO CORBA IOR

How to configure TAO corba server so that IOR string of this server generated from object_to_string is constant?
Each time the IOR string generated from object_to_string changes once server restarts. This is inconvenient since client has to update its cached server IOR string via reloading IOR file or namingservice accessing. As a result, it would be useful if server can generate a constant IOR string, no matter how many times it restarts.
My corba server is based on ACE+TAO and i do remember TAO supports constant IOR string: the IOR string each time it generates are same, and the solution is add some configurations for server. But i could not remember these configurations now.
=============================================
UPDATE:
In ACE_wrappers/TAO/tests/POA/Persistent_ID/server.cpp, i added a new function named testUniqe() which is similiar to creatPOA method. And the update file content is:
void testUniqu(CORBA::ORB_ptr orb_, PortableServer::POA_ptr poa_){
CORBA::PolicyList policies (2);
policies.length (2);
//IOR is the same even it is SYSTEM_ID
policies[0] = poa_->create_id_assignment_policy (PortableServer::USER_ID);
policies[1] = poa_->create_lifespan_policy (PortableServer::PERSISTENT);
PortableServer::POAManager_var poa_manager = poa_->the_POAManager ();
PortableServer::POA_ptr child_poa_ = poa_->create_POA ("childPOA", poa_manager.in (), policies);
// Destroy the policies
for (CORBA::ULong i = 0; i < policies.length (); ++i) {
policies[i]->destroy ();
}
test_i *servant = new test_i (orb_, child_poa_);
PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId("xushijie");
child_poa_->activate_object_with_id (oid, servant);
PortableServer::ObjectId_var id = poa_->activate_object (servant);
CORBA::Object_var object = poa_->id_to_reference (id.in ());
test_var test = test::_narrow (object.in ());
CORBA::String_var ior = orb_->object_to_string(test.in());
std::cout<<ior.in()<<std::endl;
poa_->the_POAManager()->activate();
orb_->run();
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
try
{
CORBA::ORB_var orb =
CORBA::ORB_init (argc, argv);
int result = parse_args (argc, argv);
CORBA::Object_var obj =
orb->resolve_initial_references ("RootPOA");
PortableServer::POA_var root_poa =
PortableServer::POA::_narrow (obj.in ());
PortableServer::POAManager_var poa_manager =
root_poa->the_POAManager ();
testUniqu(orb.in(), root_poa.in());
orb->destroy ();
}
catch (const CORBA::Exception& ex)
{
ex._tao_print_exception ("Exception caught");
return -1;
}
return 0;
}
The problem is that the output server IORs are still different once restart. I also compared this code to the one in Page 412(Advance Corba Programming), but still fail..
///////////////////////////////////
UPDATE:
With "server -ORBListenEndpoints iiop://:1234 > /tmp/ior1", the generated two IORs are:
IOR:010000000d00000049444c3a746573743a312e300000000001000000000000007400000001010200150000007368696a69652d5468696e6b5061642d543431300000d2041b00000014010f0052535453f60054c6f80c000000000001000000010000000002000000000000000800000001000000004f41540100000018000000010000000100010001000000010001050901010000000000
IOR:010000000d00000049444c3a746573743a312e300000000001000000000000007400000001010200150000007368696a69652d5468696e6b5061642d543431300000d2041b00000014010f0052535468f60054da280a000000000001000000010000000002000000000000000800000001000000004f41540100000018000000010000000100010001000000010001050901010000000000
The result for tao_catior for ior1 and ior2:
ior1:
The Byte Order: Little Endian
The Type Id: "IDL:test:1.0"
Number of Profiles in IOR: 1
Profile number: 1
IIOP Version: 1.2
Host Name: **
Port Number: 1234
Object Key len: 27
Object Key as hex:
14 01 0f 00 52 53 54 53 f6 00 54 c6 f8 0c 00 00
00 00 00 01 00 00 00 01 00 00 00
The Object Key as string:
....RSTS..T................
The component <1> ID is 00 (TAG_ORB_TYPE)
ORB Type: 0x54414f00 (TAO)
The component <2> ID is 11 (TAG_CODE_SETS)
Component length: 24
Component byte order: Little Endian
Native CodeSet for char: Hex - 10001 Description - ISO8859_1
Number of CCS for char 1
Conversion Codesets for char are:
1) Hex - 5010001 Description - UTF-8
Native CodeSet for wchar: Hex - 10109 Description - UTF-16
Number of CCS for wchar 0
ecoding an IOR:
//ior2
The Byte Order: Little Endian
The Type Id: "IDL:test:1.0"
Number of Profiles in IOR: 1
Profile number: 1
IIOP Version: 1.2
Host Name: **
Port Number: 1234
Object Key len: 27
Object Key as hex:
14 01 0f 00 52 53 54 68 f6 00 54 da 28 0a 00 00
00 00 00 01 00 00 00 01 00 00 00
The Object Key as string:
....RSTh..T.(..............
The component <1> ID is 00 (TAG_ORB_TYPE)
ORB Type: 0x54414f00 (TAO)
The component <2> ID is 11 (TAG_CODE_SETS)
Component length: 24
Component byte order: Little Endian
Native CodeSet for char: Hex - 10001 Description - ISO8859_1
Number of CCS for char 1
Conversion Codesets for char are:
1) Hex - 5010001 Description - UTF-8
Native CodeSet for wchar: Hex - 10109 Description - UTF-16
Number of CCS for wchar 0
The diff result is:
< 14 01 0f 00 52 53 54 53 f6 00 54 c6 f8 0c 00 00
---
> 14 01 0f 00 52 53 54 68 f6 00 54 da 28 0a 00 00
19c19
< ....RSTS..T................
---
> ....RSTh..T.(..............
Similar diff result is:
< 14 01 0f 00 52 53 54 62 fd 00 54 2c 9a 0e 00 00
---
> 14 01 0f 00 52 53 54 02 fd 00 54 f9 a9 09 00 00
19c19
< ....RSTb..T,...............
---
> ....RST...T................
The difference is in ObjectKey.
============================================
update:
Instead of using above code, i find a better solution with helper TAO_ORB_Manager which is used NamingService and TAO/examples/Simple. TAO_ORB_Manager encapsulates API and generate persistent IORs, as example code in Simple.cpp:
if (this->orb_manager_.init_child_poa (argc, argv, "child_poa") == -1){
CORBA::String_var str =
this->orb_manager_.activate_under_child_poa (servant_name,
this->servant_.in ());
}
This is some description for TAO_ORB_Manager:
class TAO_UTILS_Export TAO_ORB_Manager
{
/**
* Creates a child poa under the root poa with PERSISTENT and
* USER_ID policies. Call this if you want a #a child_poa with the
* above policies, otherwise call init.
*
* #retval -1 Failure
* #retval 0 Success
*/
int init_child_poa (int &argc,
ACE_TCHAR *argv[],
const char *poa_name,
const char *orb_name = 0);
/**
* Precondition: init_child_poa has been called. Activate <servant>
* using the POA <activate_object_with_id> created from the string
* <object_name>. Users should call this to activate objects under
* the child_poa.
*
* #param object_name String name which will be used to create
* an Object ID for the servant.
* #param servant The servant to activate under the child POA.
*
* #return 0 on failure, a string representation of the object ID if
* successful. Caller of this method is responsible for
* memory deallocation of the string.
*/
char *activate_under_child_poa (const char *object_name,
PortableServer::Servant servant);
...................
}
After build, I can get what i want with -ORBListenEndpoints iiop://localhost:2809 option. Thanks #Johnny Willemsen help
You have to create the POA with a persistent lifespan policy, see ACE_wrappers/TAO/tests/POA/Persistent_ID as part of the TAO distribution.

SNMP trap via Indy plus russian letters

I send snmp traps from borland c++ application using indy components, (thanks Remy Lebeau for working example Implementing SNMP SendTrap using Indy components) But I have problem with russian letters.
void __fastcall TMainForm::QuickSendTrap()
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
String eventTitle1 = myEnterprise + _D(".3");
String eventTitle2 = myEnterprise + _D(".4");
String eventTitle3 = myEnterprise + _D(".5");
TStringList *names = new TStringList;
names->Add(eventType);
names->Add(eventDistance);
names->Add(eventTitle1);
names->Add(eventTitle2);
names->Add(eventTitle3);
TStringList *values = new TStringList;
values->AddObject(_D("ftCritical"), (TObject*)ASN1_OCTSTR);
values->AddObject(_D("2.357"), (TObject*)ASN1_OCTSTR);
values->AddObject(_D("АБВГД"), (TObject*)ASN1_OCTSTR);
values->AddObject(_D("абвгд"), (TObject*)ASN1_OCTSTR);
values->AddObject(_D("ОПРСТ"), (TObject*)ASN1_OCTSTR);
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->QuickSendTrap(edHost->Text, myEnterprise, _D("public"), 162, 6, 1, names, values);
delete idSnmp;
delete names;
delete values;
}
Instead of russian symbols Wireshark see "?"
0000 30 81 8b 02 01 00 04 06 70 75 62 6c 69 63 a4 7e 0.......public.~
0010 06 07 2d 05 05 05 05 05 05 40 04 c0 a8 64 56 02 ..-......#...dV.
0020 01 06 02 01 01 43 01 00 30 64 30 16 06 08 2d 05 .....C..0d0...-.
0030 05 05 05 05 05 01 04 0a 66 74 43 72 69 74 69 63 ........ftCritic
0040 61 6c 30 11 06 08 2d 05 05 05 05 05 05 02 04 05 al0...-.........
0050 32 2e 33 35 37 30 11 06 08 2d 05 05 05 05 05 05 2.3570...-......
0060 03 04 05 3f 3f 3f 3f 3f 30 11 06 08 2d 05 05 05 ...?????0...-...
0070 05 05 05 04 04 05 3f 3f 3f 3f 3f 30 11 06 08 2d ......?????0...-
0080 05 05 05 05 05 05 05 04 05 3f 3f 3f 3f 3f .........?????
The question is how to encode russian string for snmp properly?
Unfortunately, TIdSNMP does not support non-ASCII data at this time. This is because:
Indy's ASN.1 encode/decode functions have not been made encoding-aware yet so that strings can be converted to/from byte arrays using charsets.
TSNMPInfo encodes PDU data to a String first and then encodes that String to bytes using Indy's 8bit encoding when sending. Thus, any Unicode codepoints greater than #255 will get converted to ?. The Russian characters you are trying to send all have Unicode codepoint values greater than #255. There is no option to specify a different encoding, even though ASN.1 octet strings can technically carry character data encoded in any 8bit charset as long as both parties agree to the charset used.
In order to send cyrillic symbols via Indy's Snmp component I implemented functions which "hide" message in different encoding inside UnicodeString that used by TIdSnmp.
UnicodeString TMainForm::Windows1251ForSnmp(AnsiString str)
{
UnicodeString ustr = "";
for (int i = 0; i < str.Length(); i++)
{
unsigned char cc = *(str.c_str()+i);
wchar_t buff = 0;
memcpy((char*)&buff, &cc, 1);
ustr = ustr + buff;
}
return ustr;
}
UnicodeString TMainForm::Utf8ForSnmp(UTF8String str)
{
UnicodeString ustr = "";
for (int i = 0; i < str.Length(); i++)
{
unsigned char cc = *(str.c_str()+i);
wchar_t buff = 0;
memcpy((char*)&buff, &cc, 1);
ustr = ustr + buff;
}
return ustr;
}
UnicodeString TMainForm::Utf16ForSnmp(UnicodeString str)
{
UnicodeString ustr = "";
for (int i = 0; i < str.Length(); i++)
{
unsigned int cc = *(str.c_str()+i);
byte a = cc / 256;
byte b = cc % 256;
ustr = ustr + char(a);
ustr = ustr + char(b);
}
return ustr;
}
After message conversion by one of these functions I gave result to Indy component TIdSnmp as usually and it will be send without problem.
UnicodeString ustr = Windows1251ForSnmp("Русский");
UTF8String param = _D("Русский");
UnicodeString ustr2 = Utf8ForSnmp(param);
UnicodeString ustr3 = Utf16ForSnmp("Русский");
values->AddObject(ustr, (TObject*)ASN1_OCTSTR);
values->AddObject(ustr2, (TObject*)ASN1_OCTSTR);
values->AddObject(ustr3, (TObject*)ASN1_OCTSTR);
So, on the "other side" I recieved russian messages in Windows1251, Utf8 and Utf16 encoding.

Delphi StrToInt on Hex String WORD, UShort, 16 Bit hex fails

I currently use.
Here is a few outputs I formatted them they were all together like this.
E14802000003FA00014C0000031501A8
currentAttackCount := StrToInt('$' + Copy(CurHex, 17, 4));
Log('Packet = ' + CurHex + ' Count = ' + IntToStr(currentAttackCount) + ' STR = ' + '$' + Copy(CurHex, 17, 4));
Formatted outputs
Packet = E1 48 02 00 00 03 FA 00 [01 4C] 00 00 03 15 01 A8 [Count = 76] [STR = $014C]
Packet = E1 48 02 00 00 03 FA 00 [01 4D] 00 00 03 15 02 26 [Count = 77] [STR = $014D]
Packet = E1 48 02 00 00 03 FA 00 [01 4F] 00 00 03 15 02 26 [Count = 79] [STR = $014F]
As you can see the STR
output which is STR = $014C
code: 'STR = ' + '$' + Copy(CurHex, 17, 4));
Now if you look at the StrToInt
currentAttackCount := StrToInt('$' + Copy(CurHex, 17, 4));
It's pretty much the same as STR so shouldn't $014C aka 0x014C be represented as 332 instead of 76
The 76 seems to come from the $4C aka 0x4C of the $014C why does it ignore the first 2 Hex Characters
Ah I think I figured it out.
`currentAttackCount: Byte;`
I increased it to
currentAttackCount: Word;
hopefully that would solve the problem.. missed it because it's a global variable and there is soo much code.

Resources