uint16

How to convert HEX string to UInt16?

徘徊边缘 提交于 2019-12-08 02:03:52
问题 I have a HEX string d285 and I want to convert it UInt16, please guide me how I can convert it. I tried this let buffer = UInt16("\(UInt8(text, radix: 16)!)") return Data(bytes: (buffer?.bigEndian.toBytes)!) but it's not working 回答1: UInt16 has an initializer that takes a string and radix value. This can be used to create UInt16 from string. let hexString = "d285" let hexToInt = UInt16(hexString, radix: 16) // prints 53893 回答2: let hexStr = "d285" var byteArr = [UInt8]() byteArr += hexStr

Uint16Array to Uint8Array

吃可爱长大的小学妹 提交于 2019-12-05 23:09:33
I have a basic question. Say I have a Uint16Array and I have number 4 in it. data_16=new Uint16Array([4]); Now I have a length 1 and byteLength 2; how do i convert this to Uint8Array. I do not want to create a new view. data_8 = new Uint8Array(data_16) If I do this I get array length 1 and byteLength 1. This is not what I want. I need to stretch that 16 bit value in 16array into 8 bit values so that 8bit array so it would end up with 2 values int 8 bit array. I can just create a funtion which converts with shif and to all that stuff. But Can it be done with Array manipulation only? You can use

C UINT16 How to get it right?

好久不见. 提交于 2019-12-04 02:46:49
问题 I'm new on C programming and I'm testing some code where I receive and process an UDP packet formatted as follow: UINT16 port1 UINT16 port2 The corresponding values on this test are: 6005 5555 If I print the whole packet buffer I get something like this: u^W³^U><9e>^D So I thought that I would just have to break it and process as an unsigned int of 16 bytes. So I've tried something like this: int l = 0; unsigned int *primaryPort = *(unsigned int) &buffer[l]; AddToLog(logInfo, "PrimaryPort: %u

How do I printf() a uint16_t?

吃可爱长大的小学妹 提交于 2019-12-04 00:24:37
I need to use printf() to print a uint16_t. This SO answer ( How to print uint32_t and uint16_t variables value? ) says I need to use inttypes.h. However, I'm working on an embedded system and inttypes.h is not available. How do I print a uint16_t when the format specifier for a uint16_t is not available? An obvious way is: printf("%u\n", (unsigned int)x); The unsigned int is guaranteed to be at least 16 bits, so this is not a lossy conversion. You should use the style of inttypes.h but define the symbols yourself. For example: #define PRIu8 "hu" #define PRId8 "hd" #define PRIx8 "hx" #define

C UINT16 How to get it right?

大憨熊 提交于 2019-12-01 14:13:42
I'm new on C programming and I'm testing some code where I receive and process an UDP packet formatted as follow: UINT16 port1 UINT16 port2 The corresponding values on this test are: 6005 5555 If I print the whole packet buffer I get something like this: u^W³^U><9e>^D So I thought that I would just have to break it and process as an unsigned int of 16 bytes. So I've tried something like this: int l = 0; unsigned int *primaryPort = *(unsigned int) &buffer[l]; AddToLog(logInfo, "PrimaryPort: %u\n", primaryPort); l += sizeof(primaryPort); unsigned int *secondaryPort = *(unsigned int) &buffer[l];

Swift: Convert Int16 to Int32 (or NSInteger)

我怕爱的太早我们不能终老 提交于 2019-12-01 02:02:12
问题 I'm really stuck! I'm not an expert at ObjC, and now I am trying to use Swift. I thought it would be much simpler, but it wasn't. I remember Craig said that they call Swift “Objective-C without C”, but there are too many C types in OS X's foundation. Documents said that many ObjC types will automatically convert, possibly bidirectionally, to Swift types. I'm curious: how about C types? Here's where I'm stuck: //array1:[String?], event.KeyCode.value:Int16 let s = array1[event.keyCode.value]; /