{"id":1650,"date":"2017-06-27T09:17:07","date_gmt":"2017-06-27T09:17:07","guid":{"rendered":"https:\/\/www.botreetechnologies.com\/blog\/?p=1650"},"modified":"2020-12-02T10:24:54","modified_gmt":"2020-12-02T04:54:54","slug":"react-native-convert-integer-bytes","status":"publish","type":"post","link":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/","title":{"rendered":"React Native &#8211; How to Convert Integer to Bytes"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">What is&nbsp;it?<\/h3>\n\n\n\n<p>We are working on a <a href=\"https:\/\/www.botreetechnologies.com\/react-native-development\" target=\"_blank\" rel=\"noreferrer noopener\">React Native app<\/a> for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets. As its UDP, the time it can take to receive a response is not fixed. The response is a binary stream where each data point could be unsigned short, single, unsigned int etc. We are expected to read the data and calculate a checksum which is to be validated against the response checksum provided to ensure response&#8217; correctness.<\/p>\n\n\n\n<p>The issue is, Javascript (we are using&nbsp;<a href=\"https:\/\/www.botreetechnologies.com\/blog\/integrating-reactjs-with-rails\" target=\"_blank\" rel=\"noopener noreferrer\">React-native<\/a>&nbsp;to build the app) does not have unsigned short and when we calculate the checksum it generates a 32 bit integer! So, how do we convert this 32bit integer to two bytes?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to convert 32bit integer to two&nbsp;bytes?<\/h3>\n\n\n\n<p>The UDP socket response has two bytes of checksum value, we need to calculate the checksum and then convert it to two bytes and then validate against the response checksum. We need to follow the steps below,<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>First of all make sure that the checksum message is perfect, if it contains a single less \/ more values than expected then the calculated value will not match.<\/li><li>For example, the calculated checksum is 1513135. To convert it to two bytes, see the below code with explanation.<\/li><\/ol>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/ParthBarot-BoTreeConsulting\/8e9555cf0e26485738d5c5d1d9c85cd9.js\"><\/script><\/p>\n\n\n\n<p>This way, we got the two bytes from the calculated checksum integer and now able to validate this. Seems the real programming&nbsp;\ud83d\ude42&nbsp;!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.botreetechnologies.com\/contact\" target=\"_blank\" rel=\"noopener noreferrer\">Consulting is free<\/a>&nbsp;\u2013 let us help you grow!<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>What is&nbsp;it? We are working on a React Native app&#8230;<\/p>\n","protected":false},"author":6,"featured_media":12977,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72,10],"tags":[],"class_list":["post-1650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-native","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>React Native - How to Convert Integer to Bytes<\/title>\n<meta name=\"description\" content=\"We are working on a React-native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Native - How to Convert Integer to Bytes\" \/>\n<meta property=\"og:description\" content=\"We are working on a React-native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\" \/>\n<meta property=\"og:site_name\" content=\"BoTree Technologies\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BoTreeTechnologies\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-27T09:17:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-02T04:54:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"682\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Parth Barot\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:site\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Parth Barot\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\"},\"author\":{\"name\":\"Parth Barot\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/1952b3c6679346e5a2792581a67b1b0f\"},\"headline\":\"React Native &#8211; How to Convert Integer to Bytes\",\"datePublished\":\"2017-06-27T09:17:07+00:00\",\"dateModified\":\"2020-12-02T04:54:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\"},\"wordCount\":263,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg\",\"articleSection\":[\"React Native\",\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\",\"name\":\"React Native - How to Convert Integer to Bytes\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg\",\"datePublished\":\"2017-06-27T09:17:07+00:00\",\"dateModified\":\"2020-12-02T04:54:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/1952b3c6679346e5a2792581a67b1b0f\"},\"description\":\"We are working on a React-native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg\",\"width\":1024,\"height\":682,\"caption\":\"How to Convert Integer to Bytes\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.botreetechnologies.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Native &#8211; How to Convert Integer to Bytes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/\",\"name\":\"BoTree Technologies\",\"description\":\"Committed to inspire generation.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/1952b3c6679346e5a2792581a67b1b0f\",\"name\":\"Parth Barot\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/parth-barot-150x150.png\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/parth-barot-150x150.png\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/parth-barot-150x150.png\",\"caption\":\"Parth Barot\"},\"description\":\"Parth is the co-founder and CTO at BoTree Technologies. He has worked on building products in different domains and technologies. He is now managing research and pre-sales by supporting it with his problem-solving approach.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React Native - How to Convert Integer to Bytes","description":"We are working on a React-native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/","og_locale":"en_US","og_type":"article","og_title":"React Native - How to Convert Integer to Bytes","og_description":"We are working on a React-native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets.","og_url":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/","og_site_name":"BoTree Technologies","article_publisher":"https:\/\/www.facebook.com\/BoTreeTechnologies\/","article_published_time":"2017-06-27T09:17:07+00:00","article_modified_time":"2020-12-02T04:54:54+00:00","og_image":[{"width":1024,"height":682,"url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg","type":"image\/jpeg"}],"author":"Parth Barot","twitter_card":"summary_large_image","twitter_creator":"@BoTreeTech","twitter_site":"@BoTreeTech","twitter_misc":{"Written by":"Parth Barot","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#article","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/"},"author":{"name":"Parth Barot","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/1952b3c6679346e5a2792581a67b1b0f"},"headline":"React Native &#8211; How to Convert Integer to Bytes","datePublished":"2017-06-27T09:17:07+00:00","dateModified":"2020-12-02T04:54:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/"},"wordCount":263,"commentCount":0,"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg","articleSection":["React Native","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/","url":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/","name":"React Native - How to Convert Integer to Bytes","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage"},"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg","datePublished":"2017-06-27T09:17:07+00:00","dateModified":"2020-12-02T04:54:54+00:00","author":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/1952b3c6679346e5a2792581a67b1b0f"},"description":"We are working on a React-native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets.","breadcrumb":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#primaryimage","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2017\/06\/react-native-convert-integer-bytes.jpg","width":1024,"height":682,"caption":"How to Convert Integer to Bytes"},{"@type":"BreadcrumbList","@id":"https:\/\/www.botreetechnologies.com\/blog\/react-native-convert-integer-bytes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.botreetechnologies.com\/blog\/"},{"@type":"ListItem","position":2,"name":"React Native &#8211; How to Convert Integer to Bytes"}]},{"@type":"WebSite","@id":"https:\/\/www.botreetechnologies.com\/blog\/#website","url":"https:\/\/www.botreetechnologies.com\/blog\/","name":"BoTree Technologies","description":"Committed to inspire generation.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/1952b3c6679346e5a2792581a67b1b0f","name":"Parth Barot","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/parth-barot-150x150.png","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/parth-barot-150x150.png","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/parth-barot-150x150.png","caption":"Parth Barot"},"description":"Parth is the co-founder and CTO at BoTree Technologies. He has worked on building products in different domains and technologies. He is now managing research and pre-sales by supporting it with his problem-solving approach."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/1650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=1650"}],"version-history":[{"count":1,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/1650\/revisions"}],"predecessor-version":[{"id":12978,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/1650\/revisions\/12978"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media\/12977"}],"wp:attachment":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=1650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=1650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=1650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}