{"id":24352,"date":"2022-12-02T08:42:56","date_gmt":"2022-12-02T03:12:56","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/"},"modified":"2022-12-02T08:42:56","modified_gmt":"2022-12-02T03:12:56","slug":"solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/","title":{"rendered":"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-35946899\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"35946899\" data-parentid=\"35946187\" data-score=\"0\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Yes, when you say<\/p>\n<pre><code>point3 = (char*)point\n<\/code><\/pre>\n<p>You are converting the integer pointer <code>point<\/code> to a character pointer <code>point3<\/code>.  <code>point<\/code> is designed for pointing at whole integers (which of course are multibyte quantities), while <code>point3<\/code> can point at individual bytes.<\/p>\n<p>So now <code>point3<\/code> points at the individual bytes that <code>point<\/code> points to, or in other words, the bytes making up the integer <code>f<\/code>.<\/p>\n<p>But the important point is that <em>the bytes themselves are not changed in any way<\/em>.  By using different kinds of pointers, we can look at the bytes differently, but again, the bytes do not change.<\/p>\n<p>If we say<\/p>\n<pre><code>printf(\"%d\\n\", *point);\n<\/code><\/pre>\n<p>we take the integer pointer <code>point<\/code>, fetch the int it points to, and print it.  So of course we see &#8220;<code>1025<\/code>&#8220;.<\/p>\n<p>If we say<\/p>\n<pre><code>printf(\"%02x %02x\\n\", *point3, *(point3 + 1));\n<\/code><\/pre>\n<p>we&#8217;re using <code>point3<\/code> to fetch and print two individual bytes.  We <em>might<\/em> see something like &#8220;<code>01 04<\/code>&#8220;.  Now, what does that mean?<br \/>\nWell, the number 1025 in hexadecimal (base 16) is <code>0x401<\/code>, so we have picked out the two bytes <code>0x01<\/code> and <code>0x04<\/code> which make it up.<\/p>\n<p>(You might wonder why the two bytes look like they&#8217;re in the &#8220;wrong order&#8221;, and this is because of the common &#8220;little endian&#8221; representation, which is a story for another day.  But if you were using a &#8220;big endian&#8221; machine, with 32-bit ints, you would have seen &#8220;<code>00 00<\/code>&#8220;, and you would have been even more confused.)<\/p>\n<p>So why did you get a smiley face and a diamond?  I&#8217;m not sure, but those are probably the characters with values 1 and 4 on your machine.  You were probably trying to print them as characters, not as hexadecimal values like I did.<\/p>\n<p>Finally, you might have thought that by &#8220;converting an int pointer to a char pointer&#8221; you could somehow convert an integer to a string.  But, no, you were not doing anything like that.<\/p>\n<p>Yes, in C, a character pointer (<code>char *<\/code>) is often used to refer to a string.  But this does not mean that by converting something to a <code>char *<\/code>, you are converting it to a string.  (Again, all you&#8217;re converting is the pointer.)<\/p>\n<p>The bytes which make up the value 1025 are always going to be <code>0x04<\/code> and <code>0x01<\/code>.  The only way to convert them to a decimal string (in C) is to call <code>printf<\/code> and use <code>%d<\/code>, or to call a nonstandard function like <code>itoa()<\/code>.  Or, in C++, you can use <code>&lt;&lt;<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Converting\/typecasting Int pointer to char pointer, but getting ascii characters? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Yes, when you say point3 = (char*)point You are converting the integer pointer point to a character pointer point3. point is designed for pointing at whole integers (which of course are multibyte quantities), while point3 can point at individual bytes. So now point3 points at the individual bytes that point points to, or in &#8230; <a title=\"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/\" aria-label=\"More on [Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,712],"class_list":["post-24352","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-pointers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Yes, when you say point3 = (char*)point You are converting the integer pointer point to a character pointer point3. point is designed for pointing at whole integers (which of course are multibyte quantities), while point3 can point at individual bytes. So now point3 points at the individual bytes that point points to, or in ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-02T03:12:56+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Converting\\\/typecasting Int pointer to char pointer, but getting ascii characters?\",\"datePublished\":\"2022-12-02T03:12:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/\"},\"wordCount\":394,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"pointers\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/\",\"name\":\"[Solved] Converting\\\/typecasting Int pointer to char pointer, but getting ascii characters? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-12-02T03:12:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Converting\\\/typecasting Int pointer to char pointer, but getting ascii characters?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters? - JassWeb","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:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters? - JassWeb","og_description":"[ad_1] Yes, when you say point3 = (char*)point You are converting the integer pointer point to a character pointer point3. point is designed for pointing at whole integers (which of course are multibyte quantities), while point3 can point at individual bytes. So now point3 points at the individual bytes that point points to, or in ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/","og_site_name":"JassWeb","article_published_time":"2022-12-02T03:12:56+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters?","datePublished":"2022-12-02T03:12:56+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/"},"wordCount":394,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","pointers"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/","url":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/","name":"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-02T03:12:56+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-converting-typecasting-int-pointer-to-char-pointer-but-getting-ascii-characters\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Converting\/typecasting Int pointer to char pointer, but getting ascii characters?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=24352"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24352\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=24352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=24352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=24352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}